Example usage for java.io Externalizable writeExternal

List of usage examples for java.io Externalizable writeExternal

Introduction

In this page you can find the example usage for java.io Externalizable writeExternal.

Prototype

void writeExternal(ObjectOutput out) throws IOException;

Source Link

Document

The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

Usage

From source file:org.codehaus.wadi.core.util.Utils.java

public static byte[] getContent(Externalizable object, Streamer streamer) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput oo = streamer.getOutputStream(baos);
    object.writeExternal(oo);
    oo.close();/*from   w w w.  j a va  2s  .co m*/
    return baos.toByteArray();
}

From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java

public static void sizeExternalizeSeveralWithoutTypes(Externalizable[] objects) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {//from ww w.ja va  2  s. c  o  m
        ObjectOutputStream out = new ObjectOutputStream(baos);

        for (Externalizable object : objects) {
            object.writeExternal(out);
        }

        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    int size = baos.size();
    int sizePerMessage = size / objects.length;
    System.out.println("   Size no indicating types " + objects.length + " messages - RMI: " + size + " bytes ("
            + sizePerMessage + " bytes/msg)");
}

From source file:org.openengsb.core.persistence.internal.SerializableChecker.java

private void internalCheck(Object obj) {
    if (obj == null) {
        return;/* w  w w.  j  a va  2  s .c om*/
    }
    Class<?> cls = obj.getClass();
    nameStack.add(simpleName);
    traceStack.add(new TraceSlot(obj, fieldDescription));
    if (!(obj instanceof Serializable) && (!Proxy.isProxyClass(cls))) {
        throw new ObjectDbNotSerializableException(toPrettyPrintedStack(obj.getClass().getName()), exception);
    }
    ObjectStreamClass desc;
    for (;;) {
        try {
            desc = (ObjectStreamClass) lookupMethod.invoke(null, cls, Boolean.TRUE);
            obj = invokeWriteReplaceMethod.invoke(desc, obj);
            Class<?> repCl = obj.getClass();
            if (!(Boolean) hasWriteReplaceMethodMetod.invoke(desc, (Object[]) null) || obj == null
                    || repCl == cls) {
                break;
            }
            cls = repCl;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    if (cls.isPrimitive()) {
        LOGGER.trace("skip primitive check");
    } else if (cls.isArray()) {
        checked.put(obj, null);
        Class<?> ccl = cls.getComponentType();
        if (!(ccl.isPrimitive())) {
            Object[] objs = (Object[]) obj;
            for (int i = 0; i < objs.length; i++) {
                String arrayPos = "[" + i + "]";
                simpleName = arrayPos;
                fieldDescription += arrayPos;
                check(objs[i]);
            }
        }
    } else if (obj instanceof Externalizable && (!Proxy.isProxyClass(cls))) {
        Externalizable extObj = (Externalizable) obj;
        try {
            extObj.writeExternal(new ObjectOutputAdaptor() {
                private int count = 0;

                @Override
                public void writeObject(Object streamObj) throws IOException {
                    if (checked.containsKey(streamObj)) {
                        return;
                    }

                    checked.put(streamObj, null);
                    String arrayPos = "[write:" + count++ + "]";
                    simpleName = arrayPos;
                    fieldDescription += arrayPos;

                    check(streamObj);
                }
            });
        } catch (Exception e) {
            if (e instanceof ObjectDbNotSerializableException) {
                throw (ObjectDbNotSerializableException) e;
            }
            LOGGER.warn("error delegating to Externalizable : " + e.getMessage() + ", path: " + currentPath());
        }
    } else {
        Method writeObjectMethod = null;
        if (!writeObjectMethodMissing.contains(cls)) {
            try {
                writeObjectMethod = cls.getDeclaredMethod("writeObject",
                        new Class[] { java.io.ObjectOutputStream.class });
            } catch (SecurityException e) {
                writeObjectMethodMissing.add(cls);
            } catch (NoSuchMethodException e) {
                writeObjectMethodMissing.add(cls);
            }
        }
        final Object original = obj;
        if (writeObjectMethod != null) {
            class InterceptingObjectOutputStream extends ObjectOutputStream {
                private int counter;

                InterceptingObjectOutputStream() throws IOException {
                    super(DUMMY_OUTPUT_STREAM);
                    enableReplaceObject(true);
                }

                @Override
                protected Object replaceObject(Object streamObj) throws IOException {
                    if (streamObj == original) {
                        return streamObj;
                    }
                    counter++;
                    if (checked.containsKey(streamObj)) {
                        return null;
                    }
                    checked.put(streamObj, null);
                    String arrayPos = "[write:" + counter + "]";
                    simpleName = arrayPos;
                    fieldDescription += arrayPos;
                    check(streamObj);
                    return streamObj;
                }
            }
            InterceptingObjectOutputStream ioos = null;
            try {
                ioos = new InterceptingObjectOutputStream();
                ioos.writeObject(obj);
            } catch (Exception e) {
                if (e instanceof ObjectDbNotSerializableException) {
                    throw (ObjectDbNotSerializableException) e;
                }
                LOGGER.warn("error delegating to writeObject : " + e.getMessage() + ", path: " + currentPath());
            } finally {
                IOUtils.closeQuietly(ioos);
            }
        } else {
            Object[] slots;
            try {
                slots = (Object[]) getClassDataLayoutMethod.invoke(desc, (Object[]) null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            for (Object slot : slots) {
                ObjectStreamClass slotDesc;
                try {
                    Field descField = slot.getClass().getDeclaredField("desc");
                    descField.setAccessible(true);
                    slotDesc = (ObjectStreamClass) descField.get(slot);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                checked.put(obj, null);
                checkFields(obj, slotDesc);
            }
        }
    }
    traceStack.removeLast();
    nameStack.removeLast();
}