protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
/** * Writes the source object to an output stream using Java serialization. * The source object must implement {@link Serializable}. * @see ObjectOutputStream#writeObject(Object) */ @Override public void serialize(Object object, OutputStream outputStream) throws IOException { if (!(object instanceof Serializable)) { throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " + "but received an object of type [" + object.getClass().getName() + "]"); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); }
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
private void sendCachedEvents(ObjectOutputStream stream) throws IOException { if (buffer != null) { for (int i = 0; i < buffer.length(); i++) { stream.writeObject(buffer.get(i)); } stream.flush(); stream.reset(); } }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
@Override public void writeObject(Object obj) throws IOException { ByteBuf buf = Unpooled.buffer(estimatedLength); try { ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf)); try { oout.writeObject(obj); oout.flush(); } finally { oout.close(); } int objectSize = buf.readableBytes(); writeInt(objectSize); buf.getBytes(0, this, objectSize); } finally { buf.release(); } }
/** * Writes the lambdas to a stream. */ public static void save_lambdas(DataOutputStream rf, double[] lambdas) { try { ObjectOutputStream oos = new ObjectOutputStream(rf); oos.writeObject(lambdas); oos.flush(); } catch (IOException e) { e.printStackTrace(); } }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); ObjectOutputStream oout = null; try { bout.write(LENGTH_PLACEHOLDER); oout = new CompactObjectOutputStream(bout); oout.writeObject(msg); oout.flush(); } finally { if (oout != null) { oout.close(); } else { bout.close(); } } int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); } }
/** Serialize the ExtractorFrames and ExtractorFramesRare to os. */ private void saveExtractors(OutputStream os) throws IOException { ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(extractors); out.writeObject(extractorsRare); out.flush(); }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
public static byte[] serializeObject(Object o) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(o); oos.flush(); return baos.toByteArray(); } }
@Override public void writeObject(Object obj) throws IOException { ByteBuf buf = Unpooled.buffer(estimatedLength); try { ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf)); try { oout.writeObject(obj); oout.flush(); } finally { oout.close(); } int objectSize = buf.readableBytes(); writeInt(objectSize); buf.getBytes(0, this, objectSize); } finally { buf.release(); } }
public static byte[] serializeObject(Object o) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(o); oos.flush(); return baos.toByteArray(); } }
@SneakyThrows protected InputStream getInputStream(final Object value) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(value); objectOutputStream.flush(); objectOutputStream.close(); return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); }
public static byte[] serializeKerberosTicket(KerberosTicket tgt) throws Exception { ByteArrayOutputStream bao = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bao); out.writeObject(tgt); out.flush(); out.close(); return bao.toByteArray(); }
@SneakyThrows private InputStream getInputStream(final Object value) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(value); objectOutputStream.flush(); objectOutputStream.close(); return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); }
/** * Serialize the given object to a byte array. * @param object the object to serialize * @return an array of bytes representing the object in a portable fashion */ @Nullable public static byte[] serialize(@Nullable Object object) { if (object == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); } catch (IOException ex) { throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex); } return baos.toByteArray(); }