/** * @param streamable the object to be serialized * @return an array of bytes representing the given streamable object. * @throws IOException if an I/O error occurs. */ public static byte[] bytes(Streamable streamable) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); streamable.write(stream); return stream.toByteArray(); }
public static void writeBlob(PreparedStatement ps, int parameterIndex, Streamable data) throws SQLException, IOException { if (data == null) { ps.setBytes(parameterIndex, null); } else { ByteArrayOutputStream os = new ByteArrayOutputStream(); data.write(os); ps.setBytes(parameterIndex, os.toByteArray()); } } }
/** * @param streamable the object to be serialized * @param padding the result will be padded such that its length is a multiple of <em>padding</em> * @return the bytes of the given {@link Streamable} object, 0-padded such that the final length is x*padding. * @throws IOException if an I/O error occurs. */ public static byte[] bytes(Streamable streamable, int padding) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); streamable.write(stream); int offset = padding - stream.size() % padding; int length = stream.size() + offset; byte[] result = new byte[length]; stream.write(result, offset, stream.size()); return result; } }
/** * Serializes a {@link Streamable} object and returns the byte array. * * @param streamable the object to be serialized * @return an array of bytes representing the given streamable object. */ public static byte[] bytes(Streamable streamable) { if (streamable == null) return null; ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { streamable.write(stream); } catch (IOException e) { throw new ApplicationException(e); } return stream.toByteArray(); }
/** * @param streamable the object to be serialized * @param padding the result will be padded such that its length is a multiple of <em>padding</em> * @return the bytes of the given {@link Streamable} object, 0-padded such that the final length is x*padding. */ public static byte[] bytes(Streamable streamable, int padding) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { streamable.write(stream); } catch (IOException e) { throw new ApplicationException(e); } int offset = padding - stream.size() % padding; int length = stream.size() + offset; byte[] result = new byte[length]; stream.write(result, offset, stream.size()); return result; } }