public void flush() throws IOException { flushBuffer(); this.outstream.flush(); }
public void write(final byte[] b) throws IOException { if (b == null) { return; } write(b, 0, b.length); }
public void writeLine(final CharArrayBuffer s) throws IOException { if (s == null) { return; } if (this.ascii) { int off = 0; int remaining = s.length(); while (remaining > 0) { int chunk = this.buffer.capacity() - this.buffer.length(); chunk = Math.min(chunk, remaining); if (chunk > 0) { this.buffer.append(s, off, chunk); } if (this.buffer.isFull()) { flushBuffer(); } off += chunk; remaining -= chunk; } } else { // This is VERY memory inefficient, BUT since non-ASCII charsets are // NOT meant to be used anyway, there's no point optimizing it byte[] tmp = s.toString().getBytes(this.charset); write(tmp); } write(CRLF); }
flushBuffer(); writeEncoded(cbuf); write(CRLF);
/** * Writes characters from the specified string followed by a line delimiter * to this session buffer. * <p> * This method uses CR-LF as a line delimiter. * * @param s the line. * @exception IOException if an I/O error occurs. */ public void writeLine(final String s) throws IOException { if (s == null) { return; } if (s.length() > 0) { if (this.ascii) { for (int i = 0; i < s.length(); i++) { write(s.charAt(i)); } } else { final CharBuffer cbuf = CharBuffer.wrap(s); writeEncoded(cbuf); } } write(CRLF); }
protected AbstractSessionOutputBuffer( final OutputStream outstream, final int buffersize, final Charset charset, final int minChunkLimit, final CodingErrorAction malformedCharAction, final CodingErrorAction unmappableCharAction) { super(); Args.notNull(outstream, "Input stream"); Args.notNegative(buffersize, "Buffer size"); this.outstream = outstream; this.buffer = new ByteArrayBuffer(buffersize); this.charset = charset != null ? charset : Consts.ASCII; this.ascii = this.charset.equals(Consts.ASCII); this.encoder = null; this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512; this.metrics = createTransportMetrics(); this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction : CodingErrorAction.REPORT; this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction : CodingErrorAction.REPORT; }
private void writeEncoded(final CharBuffer cbuf) throws IOException { if (!cbuf.hasRemaining()) { return; } if (this.encoder == null) { this.encoder = this.charset.newEncoder(); this.encoder.onMalformedInput(this.onMalformedCharAction); this.encoder.onUnmappableCharacter(this.onUnmappableCharAction); } if (this.bbuf == null) { this.bbuf = ByteBuffer.allocate(1024); } this.encoder.reset(); while (cbuf.hasRemaining()) { final CoderResult result = this.encoder.encode(cbuf, this.bbuf, true); handleEncodingResult(result); } final CoderResult result = this.encoder.flush(this.bbuf); handleEncodingResult(result); this.bbuf.clear(); }
flushBuffer(); writeEncoded(cbuf); write(CRLF);
public void writeLine(final CharArrayBuffer s) throws IOException { if (s == null) { return; } if (this.ascii) { int off = 0; int remaining = s.length(); while (remaining > 0) { int chunk = this.buffer.capacity() - this.buffer.length(); chunk = Math.min(chunk, remaining); if (chunk > 0) { this.buffer.append(s, off, chunk); } if (this.buffer.isFull()) { flushBuffer(); } off += chunk; remaining -= chunk; } } else { // This is VERY memory inefficient, BUT since non-ASCII charsets are // NOT meant to be used anyway, there's no point optimizing it byte[] tmp = s.toString().getBytes(this.charset); write(tmp); } write(CRLF); }
/** * Writes characters from the specified string followed by a line delimiter * to this session buffer. * <p> * This method uses CR-LF as a line delimiter. * * @param s the line. * @exception IOException if an I/O error occurs. */ public void writeLine(final String s) throws IOException { if (s == null) { return; } if (s.length() > 0) { if (this.ascii) { for (int i = 0; i < s.length(); i++) { write(s.charAt(i)); } } else { final CharBuffer cbuf = CharBuffer.wrap(s); writeEncoded(cbuf); } } write(CRLF); }
protected AbstractSessionOutputBuffer( final OutputStream outstream, final int buffersize, final Charset charset, final int minChunkLimit, final CodingErrorAction malformedCharAction, final CodingErrorAction unmappableCharAction) { super(); Args.notNull(outstream, "Input stream"); Args.notNegative(buffersize, "Buffer size"); this.outstream = outstream; this.buffer = new ByteArrayBuffer(buffersize); this.charset = charset != null ? charset : Consts.ASCII; this.ascii = this.charset.equals(Consts.ASCII); this.encoder = null; this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512; this.metrics = createTransportMetrics(); this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction : CodingErrorAction.REPORT; this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction : CodingErrorAction.REPORT; }
private void writeEncoded(final CharBuffer cbuf) throws IOException { if (!cbuf.hasRemaining()) { return; } if (this.encoder == null) { this.encoder = this.charset.newEncoder(); this.encoder.onMalformedInput(this.onMalformedCharAction); this.encoder.onUnmappableCharacter(this.onUnmappableCharAction); } if (this.bbuf == null) { this.bbuf = ByteBuffer.allocate(1024); } this.encoder.reset(); while (cbuf.hasRemaining()) { final CoderResult result = this.encoder.encode(cbuf, this.bbuf, true); handleEncodingResult(result); } final CoderResult result = this.encoder.flush(this.bbuf); handleEncodingResult(result); this.bbuf.clear(); }
flushBuffer(); writeEncoded(cbuf); write(CRLF);
public void write(int b) throws IOException { if (this.buffer.isFull()) { flushBuffer(); } this.buffer.append(b); }
public void writeLine(final CharArrayBuffer s) throws IOException { if (s == null) { return; } if (this.ascii) { int off = 0; int remaining = s.length(); while (remaining > 0) { int chunk = this.buffer.capacity() - this.buffer.length(); chunk = Math.min(chunk, remaining); if (chunk > 0) { this.buffer.append(s, off, chunk); } if (this.buffer.isFull()) { flushBuffer(); } off += chunk; remaining -= chunk; } } else { // This is VERY memory inefficient, BUT since non-ASCII charsets are // NOT meant to be used anyway, there's no point optimizing it byte[] tmp = s.toString().getBytes(this.charset); write(tmp); } write(CRLF); }
/** * Writes characters from the specified string followed by a line delimiter * to this session buffer. * <p> * This method uses CR-LF as a line delimiter. * * @param s the line. * @exception IOException if an I/O error occurs. */ public void writeLine(final String s) throws IOException { if (s == null) { return; } if (s.length() > 0) { if (this.ascii) { for (int i = 0; i < s.length(); i++) { write(s.charAt(i)); } } else { final CharBuffer cbuf = CharBuffer.wrap(s); writeEncoded(cbuf); } } write(CRLF); }