@Override public void flush() throws IOException { doFlush(0); }
@Override public <R> R applyToChannel(Function<WritableByteChannel, R> f) throws IOException { if (strictFlushing) throw new UnsupportedOperationException(); //Don't allow writes to the underlying channel while data is buffered flush(); return f.apply(channel); }
@Override public void write(byte[] b) throws IOException { write(b, 0, b.length); }
@Override public void write(ByteBuffer toWrite) throws IOException { if (toWrite.hasArray()) { write(toWrite.array(), toWrite.arrayOffset() + toWrite.position(), toWrite.remaining()); } else { assert toWrite.isDirect(); MemoryUtil.duplicateDirectByteBuffer(toWrite, hollowBuffer); int toWriteRemaining = toWrite.remaining(); if (toWriteRemaining > buffer.remaining()) { if (strictFlushing) { writeExcessSlow(); } else { doFlush(toWriteRemaining - buffer.remaining()); while (hollowBuffer.remaining() > buffer.capacity()) channel.write(hollowBuffer); } } buffer.put(hollowBuffer); } }
@SuppressWarnings("resource") protected static DataOutputStreamPlus getWriteChannel(Socket socket) throws IOException { WritableByteChannel out = socket.getChannel(); // socket channel is null when encrypted(SSL) if (out == null) return new WrappedDataOutputStreamPlus(new BufferedOutputStream(socket.getOutputStream())); return new BufferedDataOutputStreamPlus(out); }
@Override public void writeChar(int v) throws IOException { if (buffer.remaining() < 2) writeSlow(v, 2); else buffer.putChar((char) v); }
@Override public void writeBytes(String s) throws IOException { for (int index = 0; index < s.length(); index++) writeByte(s.charAt(index)); }
@Override public void writeShort(int v) throws IOException { writeChar(v); }
@Override public void writeFloat(float v) throws IOException { writeInt(Float.floatToRawIntBits(v)); }
@Override public void writeDouble(double v) throws IOException { writeLong(Double.doubleToRawLongBits(v)); }
@Override public void write(ByteBuffer toWrite) throws IOException { if (toWrite.hasArray()) { write(toWrite.array(), toWrite.arrayOffset() + toWrite.position(), toWrite.remaining()); } else { assert toWrite.isDirect(); MemoryUtil.duplicateDirectByteBuffer(toWrite, hollowBuffer); int toWriteRemaining = toWrite.remaining(); if (toWriteRemaining > buffer.remaining()) { if (strictFlushing) { writeExcessSlow(); } else { doFlush(toWriteRemaining - buffer.remaining()); while (hollowBuffer.remaining() > buffer.capacity()) channel.write(hollowBuffer); } } buffer.put(hollowBuffer); } }
@SuppressWarnings("resource") protected static DataOutputStreamPlus getWriteChannel(Socket socket) throws IOException { WritableByteChannel out = socket.getChannel(); // socket channel is null when encrypted(SSL) if (out == null) return new WrappedDataOutputStreamPlus(new BufferedOutputStream(socket.getOutputStream())); return new BufferedDataOutputStreamPlus(out); }
@Override public void writeLong(long v) throws IOException { if (buffer.remaining() < 8) writeSlow(v, 8); else buffer.putLong(v); }
@Override public void writeBytes(String s) throws IOException { for (int index = 0; index < s.length(); index++) writeByte(s.charAt(index)); }
@Override public void writeShort(int v) throws IOException { writeChar(v); }
@Override public void writeFloat(float v) throws IOException { writeInt(Float.floatToRawIntBits(v)); }
@Override public void writeDouble(double v) throws IOException { writeLong(Double.doubleToRawLongBits(v)); }
@Override public void write(ByteBuffer toWrite) throws IOException { if (toWrite.hasArray()) { write(toWrite.array(), toWrite.arrayOffset() + toWrite.position(), toWrite.remaining()); } else { assert toWrite.isDirect(); MemoryUtil.duplicateDirectByteBuffer(toWrite, hollowBuffer); int toWriteRemaining = toWrite.remaining(); if (toWriteRemaining > buffer.remaining()) { if (strictFlushing) { writeExcessSlow(); } else { doFlush(toWriteRemaining - buffer.remaining()); while (hollowBuffer.remaining() > buffer.capacity()) channel.write(hollowBuffer); } } buffer.put(hollowBuffer); } }