@Override public void write(Externalizable externalizable, BufferOutput<?> buffer, Serializer serializer) { try { externalizable.writeExternal(new BufferObjectOutput(buffer, serializer)); } catch (IOException e) { throw new SerializationException( "failed to serialize externalizable type: " + externalizable.getClass(), e); } }
@Override public Externalizable read( Class<Externalizable> type, BufferInput<?> buffer, Serializer serializer) { try { Externalizable externalizable = type.newInstance(); externalizable.readExternal(new BufferObjectInput(buffer, serializer)); return externalizable; } catch (InstantiationException | IllegalAccessException e) { throw new SerializationException("failed to instantiate externalizable type: " + type, e); } catch (IOException | ClassNotFoundException e) { throw new SerializationException("failed to deserialize externalizable type: " + type, e); } }
private void externalize(final Externalizable original, final Externalizable copy) throws IOException, ClassNotFoundException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(baos); original.writeExternal(out); out.close(); final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ObjectInputStream in = new ObjectInputStream(bais); copy.readExternal(in); }
private static Object readRealObject(byte type, InputStream is, ClassLoader loader) throws IOException { try { if (type == 0) { return PDataStream.readUTF(is); } else if (type == 1) { return new Integer(PDataStream.readInt(is)); } else if (type == 2) { return new Long(PDataStream.readLong(is)); } else if (type == 3) { return new Byte((byte) is.read()); } else if (type == 4) { return PDataStream.readBoolean(is) ? Boolean.TRUE : Boolean.FALSE; } else if (type == 5) { return new Character(PDataStream.readChar(is)); } else if (type == 6) { return new Short(PDataStream.readShort(is)); } else if (type == 7) { return new Float(PDataStream.readFloat(is)); } else if (type == 8) { return new Double(PDataStream.readDouble(is)); } else if (type == 11) { String name = PDataStream.readUTF(is); Class c = loader == null ? Class.forName(name) : loader.loadClass(name); if (Externalizable.class.isAssignableFrom(c)) { Externalizable obj = (Externalizable) c.newInstance(); obj.readObject(is); return obj; } throw new IOException("Could not read object " + name); } else if (type == 12) { ObjectInputStream in = loader == null ? new ObjectInputStream(is) : (ObjectInputStream) new XObjectInputStream(loader, is); return in.readObject(); } } catch (ClassNotFoundException cnfe) { throw new IOException("Could not find class " + cnfe.toString()); } catch (Exception exc) { throw exc instanceof IOException ? (IOException) exc : new IOException("Could not read object " + exc.toString()); } throw new IllegalArgumentException("Unsupported Typed Object: " + type); }
// only if this is an object (not primitive) and non null! private static void writeRealObject(Object value, Class vClass, OutputStream os) throws IOException { try { if (vClass.equals(String.class)) { os.write(0); PDataStream.writeUTF((String) value, os); } else if (vClass.equals(Integer.class)) { os.write(1); PDataStream.writeInt(((Integer) value).intValue(), os); } else if (vClass.equals(Long.class)) { os.write(2); PDataStream.writeLong(((Long) value).longValue(), os); } else if (vClass.equals(Byte.class)) { os.write(3); os.write(((Byte) value).byteValue()); } else if (vClass.equals(Boolean.class)) { os.write(4); PDataStream.writeBoolean(((Boolean) value).booleanValue(), os); } else if (vClass.equals(Character.class)) { os.write(5); PDataStream.writeChar(((Character) value).charValue(), os); } else if (vClass.equals(Short.class)) { os.write(6); PDataStream.writeShort(((Short) value).shortValue(), os); } else if (vClass.equals(Float.class)) { os.write(7); PDataStream.writeFloat(((Float) value).floatValue(), os); } else if (vClass.equals(Double.class)) { os.write(8); PDataStream.writeDouble(((Double) value).doubleValue(), os); } else if (Externalizable.class.isAssignableFrom(vClass)) { os.write(11); String name = vClass.getName(); PDataStream.writeUTF(name, os); Externalizable tmp = (Externalizable) value; tmp.writeObject(os); } else { os.write(12); ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(value); } } catch (Exception exc) { throw new IOException(exc.toString()); } }
@Override public void write(final ObjectDataOutput out, final Externalizable obj) throws IOException { out.writeUTF(obj.getClass().getName()); final ObjectOutputStream objectOutputStream; final OutputStream outputStream = (OutputStream) out; GZIPOutputStream gzip = null; if (gzipEnabled) { gzip = new GZIPOutputStream(outputStream); objectOutputStream = new ObjectOutputStream(gzip); } else { objectOutputStream = new ObjectOutputStream(outputStream); } obj.writeExternal(objectOutputStream); // Force flush if not yet written due to internal behavior if pos < 1024 objectOutputStream.flush(); if (gzipEnabled) { gzip.finish(); } }
public void defaultWriteObject(Object toWrite, FSTClazzInfo serializationInfo) throws IOException { if (serializationInfo.isExternalizable()) { codec.ensureFree(writeExternalWriteAhead); ((Externalizable) toWrite).writeExternal(this); } else { FSTClazzInfo.FSTFieldInfo[] fieldInfo = serializationInfo.getFieldInfo(); writeObjectFields(toWrite, serializationInfo, fieldInfo, 0, 0); } }
@Override public void marshall( Object value, PropertyDescriptor propertyDescriptor, DataOutput dataOutput, SerializationContext serializationContext) throws IOException { ((Externalizable) value).writeExternal((ObjectOutput) dataOutput); }
@Override public Externalizable read(final ObjectDataInput in) throws IOException { final String className = in.readUTF(); try { final Externalizable ds = ClassLoaderUtil.newInstance(in.getClassLoader(), className); final ObjectInputStream objectInputStream; final InputStream inputStream = (InputStream) in; if (gzipEnabled) { objectInputStream = newObjectInputStream(in.getClassLoader(), new GZIPInputStream(inputStream)); } else { objectInputStream = newObjectInputStream(in.getClassLoader(), inputStream); } ds.readExternal(objectInputStream); return ds; } catch (final Exception e) { throw new HazelcastSerializationException( "Problem while reading Externalizable class : " + className + ", exception: " + e); } }
@Override public <V> V unmarshall( V value, PropertyDescriptor propertyDescriptor, DataInput dataInput, SerializationContext serializationContext) throws IOException { try { ((Externalizable) value).readExternal((ObjectInput) dataInput); return value; } catch (ClassNotFoundException e) { throw new IOException("Error while deserialization", e); } }
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { in.readByte(); // ignore version for now fieldNumber = in.readShort(); byte fieldType; ISOComponent c; try { while ((fieldType = in.readByte()) != 'E') { c = null; switch (fieldType) { case 'F': c = new ISOField(); break; case 'A': c = new ISOAmount(); break; case 'B': c = new ISOBinaryField(); break; case 'M': c = new ISOMsg(); break; case 'H': readHeader(in); break; case 'P': readPackager(in); break; case 'D': readDirection(in); break; default: throw new IOException("malformed ISOMsg"); } if (c != null) { ((Externalizable) c).readExternal(in); set(c); } } } catch (ISOException e) { throw new IOException(e.getMessage()); } }
private void writeExternal(ObjectOutput out, char b, ISOComponent c) throws IOException { out.writeByte(b); ((Externalizable) c).writeExternal(out); }
@Override protected void writeObject(ObjectOutput out, Object obj) throws IOException { ((Externalizable) obj).writeExternal(out); }
private void internalCheck(Object obj) { if (obj == null) { return; } Class<?> cls = obj.getClass(); nameStack.add(simpleName); traceStack.add(new TraceSlot(obj, fieldDescription)); if (!(obj instanceof Serializable) && (!Proxy.isProxyClass(cls))) { throw new WicketSerializableCheckException( toPrettyPrintedStack(obj.getClass(), "field that is not serializable")); } serializableCheck.inspect(obj, stackTracePrinter); ObjectStreamClass desc; for (; ; ) { try { desc = (ObjectStreamClass) LOOKUP_METHOD.invoke(null, cls, Boolean.TRUE); Class<?> repCl; if (!(Boolean) HAS_WRITE_REPLACE_METHOD_METHOD.invoke(desc, (Object[]) null) || (obj = INVOKE_WRITE_REPLACE_METHOD.invoke(desc, obj)) == null || (repCl = obj.getClass()) == cls) { break; } cls = repCl; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } if (cls.isPrimitive()) { // skip } 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; public void writeObject(Object streamObj) throws IOException { // Check for circular reference. 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 WicketSerializableCheckException) { throw (WicketSerializableCheckException) e; } log.warn( "error delegating to Externalizable : " + e.getMessage() + ", path: " + currentPath()); } } else { Method writeObjectMethod = null; if (writeObjectMethodMissing.contains(cls) == false) { try { writeObjectMethod = cls.getDeclaredMethod("writeObject", new Class[] {java.io.ObjectOutputStream.class}); } catch (SecurityException e) { // we can't access / set accessible to true writeObjectMethodMissing.add(cls); } catch (NoSuchMethodException e) { // cls doesn't have that method 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++; // Check for circular reference. if (checked.containsKey(streamObj)) { return null; } checked.put(streamObj, null); String arrayPos = "[write:" + counter + "]"; simpleName = arrayPos; fieldDescription += arrayPos; check(streamObj); return streamObj; } } try { InterceptingObjectOutputStream ioos = new InterceptingObjectOutputStream(); ioos.writeObject(obj); } catch (Exception e) { if (e instanceof WicketSerializableCheckException) { throw (WicketSerializableCheckException) e; } log.warn( "error delegating to writeObject : " + e.getMessage() + ", path: " + currentPath()); } } else { Object[] slots; try { slots = (Object[]) GET_CLASS_DATA_LAYOUT_METHOD.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(); }