@Override public FastBufferOutputStream getResponseOutputStream() { if (stream == null) { stream = new FastBufferOutputStream(); } return stream; }
private void flushBuffers() throws IOException { if (writer != null) { writer.flush(); } else if (stream != null) { stream.flush(); } } }
/** * Creates instance of FastBufferInputStream. * * @param stream */ @Deprecated public FastBufferInputStream(FastBufferOutputStream stream) { this(stream.getFirstBuffer()); }
public InputStream getInputStream() throws IOException { FastBufferOutputStream fbos = new FastBufferOutputStream(); ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(fbos); try { paintAndWrite(imageOutputStream); } finally { if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { LOGGER.debug(e.getMessage(), e); } try { fbos.close(); } catch (IOException e) { // Swallow } } } ByteBuffer buffer = fbos.getFirstBuffer(); buffer.compact(); return new FastBufferInputStream(buffer); }
private static ByteBuffer readContent(InputStream is) throws IOException { if (is == null) { throw new NullPointerException("Resource input stream is null"); } FastBufferOutputStream os = new FastBufferOutputStream(); try { ResourceUtils.copyStreamContent(is, os); } finally { try { is.close(); } catch (IOException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } } try { os.close(); } catch (IOException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } } } ByteBuffer buffer = os.getFirstBuffer(); buffer.compact(); return buffer; }
/** * Writes all data written up to the moment to out. * * @param out * @throws IOException */ public void writeTo(OutputStream out) throws IOException { ByteBuffer b = getFirstBuffer(); while (b != null) { out.write(b.getBytes(), 0, b.getUsedSize()); b = b.getNext(); } }
/** * Returns instance of FastBufferOutputStream containing all data written to this writer. * * @return instance of FastBufferOutputStream containing all data written to this writer */ public FastBufferOutputStream convertToOutputStream() { CharBuffer c = firstBuffer; ByteBuffer first = c.toByteBuffer(); ByteBuffer b = first; while (c != null) { c = c.getNext(); if (c == null) { break; } ByteBuffer n = c.toByteBuffer(); b.setNext(n); b = n; } return new FastBufferOutputStream(first); }
public InputStream getWrittenDataAsStream() throws IOException { flushBuffers(); if (stream != null) { ByteBuffer firstBuffer = stream.getFirstBuffer(); firstBuffer.compact(); return new FastBufferInputStream(firstBuffer); } else { return EMPTY_STREAM; } }
/** * Returns instance of FastBufferOutputStream containing all data written to this writer. * * @param encoding * @return instance of FastBufferOutputStream containing all data written to this writer * @throws UnsupportedEncodingException */ public FastBufferOutputStream convertToOutputStream(String encoding) throws UnsupportedEncodingException { CharBuffer c = firstBuffer; ByteBuffer first = c.toByteBuffer(encoding); ByteBuffer b = first; while (c != null) { c = c.getNext(); if (c == null) { break; } ByteBuffer n = c.toByteBuffer(encoding); b.setNext(n); b = n; } return new FastBufferOutputStream(first); }
/** * Writes all data written up to the moment to out. * * @param out * @throws IOException */ public void writeTo(Writer out, String encoding) throws IOException { ByteBuffer b = getFirstBuffer(); while (b != null) { CharBuffer c = b.toCharBuffer(encoding); out.write(c.getChars(), 0, c.getUsedSize()); b = b.getNext(); } }
public byte[] toByteArray() { ByteBuffer b = getFirstBuffer(); if (b == null) { return new byte[0]; } ByteBuffer l = b; while (l.getNext() != null) { l = l.getNext(); } byte[] result = new byte[l.getTotalSize()]; int index = 0; while (b != null) { int s = b.getUsedSize(); System.arraycopy(b.getBytes(), 0, result, index, s); index += s; b = b.getNext(); } return result; }