final public BufferOutputStream out() { return new BufferOutputStream(this); }
public void write(int b) throws IOException { int newPos = pos + 1; checkCapacity(newPos); buffer[pos] = (byte) b; pos = newPos; }
public Buffer getNextBuffer(int size) throws IOException { if( bos==null ) { return null; } return bos.getNextBuffer(size); }
/** * Create a Buffer from an Object * * @param object * @return * @throws Exception */ public static Buffer getBuffer(Object object) throws IOException { if (object != null) { BufferOutputStream bufferOut = new BufferOutputStream(512); DataOutputStream dataOut = new DataOutputStream(bufferOut); ObjectOutputStream objOut = new ObjectOutputStream(dataOut); objOut.writeObject(object); objOut.flush(); objOut.reset(); objOut.close(); return bufferOut.toBuffer(); } return null; }
/** * Inflate a compressed buffer * * @param buffer * @return inflated buffer * @throws IOException */ public static Buffer inflate(Buffer buffer) throws IOException { Buffer result = buffer; if (isCompressed(buffer)) { InputStream bytesIn = new BufferInputStream(buffer); GZIPInputStream gzipIn = new GZIPInputStream(bytesIn); BufferOutputStream bytesOut = new BufferOutputStream(buffer.length); byte[] data = new byte[4096]; int bytesRead = 0; while ((bytesRead = gzipIn.read(data, 0, data.length)) > 0) { bytesOut.write(data, 0, bytesRead); } gzipIn.close(); bytesIn.close(); result = bytesOut.toBuffer(); bytesOut.close(); } return result; }
/** * Compress the buffer * * @param buffer * @return compressed Buffer * @throws IOException */ public static Buffer compress(Buffer buffer) throws IOException { Buffer result = buffer; if (buffer != null) { BufferOutputStream bytesOut = new BufferOutputStream(buffer.length); GZIPOutputStream gzipOut = new GZIPOutputStream(bytesOut, buffer.length); gzipOut.write(buffer.toByteArray()); gzipOut.close(); bytesOut.close(); result = bytesOut.toBuffer(); } return result; }
public byte[] toByteArray() { return toBuffer().toByteArray(); }
/** * Create a Buffer from an Object * * @param object * @return * @throws Exception */ public static Buffer getBuffer(Object object) throws IOException { if (object != null) { BufferOutputStream bufferOut = new BufferOutputStream(512); DataOutputStream dataOut = new DataOutputStream(bufferOut); ObjectOutputStream objOut = new ObjectOutputStream(dataOut); objOut.writeObject(object); objOut.flush(); objOut.reset(); objOut.close(); return bufferOut.toBuffer(); } return null; }
/** * Inflate a compressed buffer * * @param buffer * @return inflated buffer * @throws IOException */ public static Buffer inflate(Buffer buffer) throws IOException { Buffer result = buffer; if (isCompressed(buffer)) { InputStream bytesIn = new BufferInputStream(buffer); GZIPInputStream gzipIn = new GZIPInputStream(bytesIn); BufferOutputStream bytesOut = new BufferOutputStream(buffer.length); byte[] data = new byte[4096]; int bytesRead = 0; while ((bytesRead = gzipIn.read(data, 0, data.length)) > 0) { bytesOut.write(data, 0, bytesRead); } gzipIn.close(); bytesIn.close(); result = bytesOut.toBuffer(); bytesOut.close(); } return result; }
/** * Compress the buffer * * @param buffer * @return compressed Buffer * @throws IOException */ public static Buffer compress(Buffer buffer) throws IOException { Buffer result = buffer; if (buffer != null) { BufferOutputStream bytesOut = new BufferOutputStream(buffer.length); GZIPOutputStream gzipOut = new GZIPOutputStream(bytesOut, buffer.length); gzipOut.write(buffer.toByteArray()); gzipOut.close(); bytesOut.close(); result = bytesOut.toBuffer(); } return result; }
public byte[] toByteArray() { return toBuffer().toByteArray(); }
static public Buffer toUnframedBuffer(MessageBuffer message) { try { int size = message.serializedSizeUnframed(); BufferOutputStream baos = new BufferOutputStream(size); CodedOutputStream output = new CodedOutputStream(baos); message.writeUnframed(output); Buffer rc = baos.toBuffer(); assert rc.length == size : "Did not write as much data as expected."; return rc; } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } }
final public BufferOutputStream out() { return new BufferOutputStream(this); }
public void write(int b) throws IOException { int newPos = pos + 1; checkCapacity(newPos); buffer[pos] = (byte) b; pos = newPos; }
public byte[] toByteArray() { return toBuffer().toByteArray(); }
public Buffer toUnframedBuffer() { try { int size = serializedSizeUnframed(); BufferOutputStream baos = new BufferOutputStream(size); CodedOutputStream output = new CodedOutputStream(baos); writeUnframed(output); Buffer rc = baos.toBuffer(); if( rc.length != size ) { throw new IllegalStateException("Did not write as much data as expected."); } return rc; } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } }
public CodedOutputStream(byte[] data) { super(new BufferOutputStream(data)); }
public void write(int b) throws IOException { int newPos = pos + 1; checkCapacity(newPos); buffer[pos] = (byte) b; pos = newPos; }
static public Buffer toFramedBuffer(MessageBuffer message) { try { int size = message.serializedSizeFramed(); BufferOutputStream baos = new BufferOutputStream(size); CodedOutputStream output = new CodedOutputStream(baos); message.writeFramed(output); Buffer rc = baos.toBuffer(); assert rc.length==size : "Did not write as much data as expected."; return rc; } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } }
final public BufferOutputStream out() { return new BufferOutputStream(this); }