/** write the low-order 8 bits of the given int */ @Override public final void write(int b) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(1); buffer.put((byte)b); }
/** override OutputStream's write() */ @Override public void write(byte[] source, int offset, int len) { if (this.ignoreWrites) return; checkIfWritable(); int remainingSpace = this.buffer.capacity() - this.buffer.position(); if (remainingSpace < len) { this.buffer.put(source, offset, remainingSpace); offset += remainingSpace; len -= remainingSpace; ensureCapacity(len); } this.buffer.put(source, offset, len); }
/** write the low-order 8 bits of the given int */ @Override public final void write(int b) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(1); buffer.put((byte)b); }
/** * Write a byte buffer to this HeapDataOutputStream, * * the contents of the buffer between the position and the limit * are copied to the output stream. */ public void write(ByteBuffer source) { if (this.ignoreWrites) return; checkIfWritable(); int remainingSpace = this.buffer.capacity() - this.buffer.position(); if (remainingSpace < source.remaining()) { int oldLimit = source.limit(); source.limit(source.position() + remainingSpace); this.buffer.put(source); source.limit(oldLimit); ensureCapacity(source.remaining()); } this.buffer.put(source); }
/** * Writes a <code>double</code> value, * which is comprised of eight bytes, to the output stream. * It does this as if it first converts this * <code>double</code> value to a <code>long</code> * in exactly the manner of the <code>Double.doubleToLongBits</code> * method and then writes the <code>long</code> * value in exactly the manner of the <code>writeLong</code> * method. The bytes written by this method * may be read by the <code>readDouble</code> * method of interface <code>DataInput</code>, * which will then return a <code>double</code> * equal to <code>v</code>. * * @param v the <code>double</code> value to be written. */ public final void writeDouble(double v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(8); buffer.putDouble(v); }
/** * Writes a <code>float</code> value, * which is comprised of four bytes, to the output stream. * It does this as if it first converts this * <code>float</code> value to an <code>int</code> * in exactly the manner of the <code>Float.floatToIntBits</code> * method and then writes the <code>int</code> * value in exactly the manner of the <code>writeInt</code> * method. The bytes written by this method * may be read by the <code>readFloat</code> * method of interface <code>DataInput</code>, * which will then return a <code>float</code> * equal to <code>v</code>. * * @param v the <code>float</code> value to be written. */ public final void writeFloat(float v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(4); buffer.putFloat(v); }
/** * Writes a <code>double</code> value, * which is comprised of eight bytes, to the output stream. * It does this as if it first converts this * <code>double</code> value to a <code>long</code> * in exactly the manner of the <code>Double.doubleToLongBits</code> * method and then writes the <code>long</code> * value in exactly the manner of the <code>writeLong</code> * method. The bytes written by this method * may be read by the <code>readDouble</code> * method of interface <code>DataInput</code>, * which will then return a <code>double</code> * equal to <code>v</code>. * * @param v the <code>double</code> value to be written. */ public final void writeDouble(double v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(8); buffer.putDouble(v); }
/** * Writes a <code>char</code> value, wich * is comprised of two bytes, to the * output stream. * The byte values to be written, in the order * shown, are: * <p><pre><code> * (byte)(0xff & (v >> 8)) * (byte)(0xff & v) * </code></pre><p> * The bytes written by this method may be * read by the <code>readChar</code> method * of interface <code>DataInput</code> , which * will then return a <code>char</code> equal * to <code>(char)v</code>. * * @param v the <code>char</code> value to be written. */ public final void writeChar(int v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(2); buffer.putChar((char)v); }
/** * Writes a <code>char</code> value, wich * is comprised of two bytes, to the * output stream. * The byte values to be written, in the order * shown, are: * <p><pre><code> * (byte)(0xff & (v >> 8)) * (byte)(0xff & v) * </code></pre><p> * The bytes written by this method may be * read by the <code>readChar</code> method * of interface <code>DataInput</code> , which * will then return a <code>char</code> equal * to <code>(char)v</code>. * * @param v the <code>char</code> value to be written. */ public final void writeChar(int v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(2); buffer.putChar((char)v); }
/** * Writes an <code>int</code> value, which is * comprised of four bytes, to the output stream. * The byte values to be written, in the order * shown, are: * <p><pre><code> * (byte)(0xff & (v >> 24)) * (byte)(0xff & (v >> 16)) * (byte)(0xff & (v >>    8)) * (byte)(0xff & v) * </code></pre><p> * The bytes written by this method may be read * by the <code>readInt</code> method of interface * <code>DataInput</code> , which will then * return an <code>int</code> equal to <code>v</code>. * * @param v the <code>int</code> value to be written. */ public final void writeInt(int v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(4); buffer.putInt(v); }
/** * Writes an <code>int</code> value, which is * comprised of four bytes, to the output stream. * The byte values to be written, in the order * shown, are: * <p><pre><code> * (byte)(0xff & (v >> 24)) * (byte)(0xff & (v >> 16)) * (byte)(0xff & (v >>    8)) * (byte)(0xff & v) * </code></pre><p> * The bytes written by this method may be read * by the <code>readInt</code> method of interface * <code>DataInput</code> , which will then * return an <code>int</code> equal to <code>v</code>. * * @param v the <code>int</code> value to be written. */ public final void writeInt(int v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(4); buffer.putInt(v); }
/** * Writes every character in the string <code>s</code>, * to the output stream, in order, * two bytes per character. If <code>s</code> * is <code>null</code>, a <code>NullPointerException</code> * is thrown. If <code>s.length</code> * is zero, then no characters are written. * Otherwise, the character <code>s[0]</code> * is written first, then <code>s[1]</code>, * and so on; the last character written is * <code>s[s.length-1]</code>. For each character, * two bytes are actually written, high-order * byte first, in exactly the manner of the * <code>writeChar</code> method. * * @param s the string value to be written. */ public final void writeChars(String s) { if (this.ignoreWrites) return; checkIfWritable(); int len = s.length(); if (len > 0) { ensureCapacity(len*2); for (int i=0; i < len; i++) { this.buffer.putChar(s.charAt(i)); } } }
/** * Writes a <code>long</code> value, which is * comprised of eight bytes, to the output stream. * The byte values to be written, in the order * shown, are: * <p><pre><code> * (byte)(0xff & (v >> 56)) * (byte)(0xff & (v >> 48)) * (byte)(0xff & (v >> 40)) * (byte)(0xff & (v >> 32)) * (byte)(0xff & (v >> 24)) * (byte)(0xff & (v >> 16)) * (byte)(0xff & (v >> 8)) * (byte)(0xff & v) * </code></pre><p> * The bytes written by this method may be * read by the <code>readLong</code> method * of interface <code>DataInput</code> , which * will then return a <code>long</code> equal * to <code>v</code>. * * @param v the <code>long</code> value to be written. */ public final void writeLong(long v) { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(8); buffer.putLong(v); }
/** * Reserves space in the output for a long * and returns a LongUpdater than can be used * to update this particular long. * @return the LongUpdater that allows the long to be updated */ public final LongUpdater reserveLong() { if (this.ignoreWrites) return null; checkIfWritable(); ensureCapacity(8); LongUpdater result = new LongUpdater(this.buffer); buffer.putLong(0L); return result; }
/** * Reserves space in the output for a long * and returns a LongUpdater than can be used * to update this particular long. * @return the LongUpdater that allows the long to be updated */ public final LongUpdater reserveLong() { if (this.ignoreWrites) return null; checkIfWritable(); ensureCapacity(8); LongUpdater result = new LongUpdater(this.buffer); buffer.putLong(0L); return result; }
/** override OutputStream's write() */ @Override public void write(byte[] source, int offset, int len) { if (len == 0) return; if (this.ignoreWrites) return; checkIfWritable(); if (this.doNotCopy && len > MIN_TO_COPY) { moveBufferToChunks(); addToChunks(source, offset, len); } else { int remainingSpace = this.buffer.capacity() - this.buffer.position(); if (remainingSpace < len) { this.buffer.put(source, offset, remainingSpace); offset += remainingSpace; len -= remainingSpace; ensureCapacity(len); } this.buffer.put(source, offset, len); } }
/** * Same as {@link #writeUTF} but it does not encode the length in the * first two bytes and allows strings longer than 65k to be encoded. */ public void writeUTFNoLength(String str) { if (this.ignoreWrites) return; checkIfWritable(); try { if (ASCII_STRINGS) { writeAsciiUTF(str, false); } else { writeUTF(str, false, false); } } catch (UTFDataFormatException ex) { // this shouldn't happen since we did not encode the length throw new IllegalStateException(LocalizedStrings.HeapDataOutputStream_UNEXPECTED_0.toLocalizedString(ex)); } }
/** * Same as {@link #writeUTF} but it does not encode the length in the * first two bytes and allows strings longer than 65k to be encoded. */ public void writeUTFNoLength(String str) { if (this.ignoreWrites) return; checkIfWritable(); try { if (ASCII_STRINGS) { writeAsciiUTF(str, false); } else { writeFullUTF(str, false); } } catch (UTFDataFormatException ex) { // this shouldn't happen since we did not encode the length throw new IllegalStateException(LocalizedStrings.HeapDataOutputStream_UNEXPECTED_0.toLocalizedString(ex)); } }
/** * Writes the given object to this stream as a byte array. * The byte array is produced by serializing v. The serialization * is done by calling DataSerializer.writeObject. */ public void writeAsSerializedByteArray(Object v) throws IOException { if (this.ignoreWrites) return; checkIfWritable(); ensureCapacity(5); if (v instanceof HeapDataOutputStream) { HeapDataOutputStream other = (HeapDataOutputStream)v; InternalDataSerializer.writeArrayLength(other.size(), this); other.sendTo((OutputStream)this); other.rewind(); } else { ByteBuffer sizeBuf = this.buffer; int sizePos = sizeBuf.position(); sizeBuf.position(sizePos+5); final int preArraySize = size(); DataSerializer.writeObject(v, this); int arraySize = size() - preArraySize; sizeBuf.put(sizePos, InternalDataSerializer.INT_ARRAY_LEN); sizeBuf.putInt(sizePos+1, arraySize); } }
/** * Write a byte source to this HeapDataOutputStream, * * the contents of the buffer between the position and the limit * are copied to the output stream. */ public void write(ByteSource source) { ByteBuffer bb = source.getBackingByteBuffer(); if (bb != null) { write(bb); return; } if (this.ignoreWrites) return; checkIfWritable(); int remainingSpace = this.buffer.limit() - this.buffer.position(); if (remainingSpace < source.remaining()) { int oldLimit = source.limit(); source.limit(source.position() + remainingSpace); source.sendTo(this.buffer); source.limit(oldLimit); ensureCapacity(source.remaining()); } source.sendTo(this.buffer); } }