/** * Grows as-needed to fit the provided new capacity. * * @returns the possibly new buffer. */ private char[] getBuffer(int additional) throws IOException { long newLen = (long)length + additional; if(newLen > MAX_LENGTH) throw new IOException("Maximum buffer length is " + MAX_LENGTH + ", " + newLen + " requested"); char[] buf = this.buffer; int bufLen = buf.length; if(newLen > bufLen) { // Find the next power of two that will hold all of the contents int newBufLen = bufLen==0 ? BufferManager.BUFFER_SIZE : (bufLen << 1); while(newBufLen < newLen) { newBufLen <<= 1; } char[] newBuf = (newBufLen == BufferManager.BUFFER_SIZE) ? BufferManager.getChars() : new char[newBufLen]; System.arraycopy(buf, 0, newBuf, 0, length); // Recycle buffer if(bufLen == BufferManager.BUFFER_SIZE) { //BufferManager.release(buf, false); } buf = newBuf; this.buffer = buf; } return buf; }
/** * Copies all information from one stream to another. Internally reuses thread-local * buffers to avoid initial buffer zeroing cost and later garbage collection overhead. * * @return the number of bytes copied * * @see BufferManager#getChars() */ public static long copy(Reader in, StringBuilder out) throws IOException { char[] buff = BufferManager.getChars(); try { long totalChars = 0; int numChars; while((numChars = in.read(buff, 0, BufferManager.BUFFER_SIZE))!=-1) { out.append(buff, 0, numChars); totalChars += numChars; } return totalChars; } finally { BufferManager.release(buff, false); } }
/** * Copies all information from one stream to another. Internally reuses thread-local * buffers to avoid initial buffer zeroing cost and later garbage collection overhead. * * @return the number of bytes copied * * @see BufferManager#getChars() */ public static long copy(Reader in, Writer out) throws IOException { char[] buff = BufferManager.getChars(); try { long totalChars = 0; int numChars; while((numChars = in.read(buff, 0, BufferManager.BUFFER_SIZE))!=-1) { out.write(buff, 0, numChars); totalChars += numChars; } return totalChars; } finally { BufferManager.release(buff, false); } }
/** * Reads the contents of a File and returns as a String in the provided character set. * * @see #readFileAsString(java.io.File) */ public static String readFileAsString(File file, Charset charset) throws IOException { long len = file.length(); StringBuilder SB = len>0 && len<=Integer.MAX_VALUE ? new StringBuilder((int)len) : new StringBuilder(); Reader in = new InputStreamReader(new FileInputStream(file), charset); try { char[] buff = BufferManager.getChars(); try { int numChars; while((numChars = in.read(buff, 0, BufferManager.BUFFER_SIZE)) != -1) { SB.append(buff, 0, numChars); } } finally { BufferManager.release(buff, false); } } finally { in.close(); } return SB.toString(); } }
Reader stderrIn = new InputStreamReader(process.getErrorStream(), charset); try { char[] buff = BufferManager.getChars(); try { int count;
/** * Copies all information from one stream to an appendable. * * @return the number of bytes copied * * @see BufferManager#getChars() */ public static long copy(Reader in, Appendable out) throws IOException { if(in == null) throw new NullArgumentException("in"); if(out == null) throw new NullArgumentException("out"); char[] buff = BufferManager.getChars(); try { long totalChars = 0; int numChars; while((numChars = in.read(buff, 0, BufferManager.BUFFER_SIZE))!=-1) { out.append(new String(buff, 0, numChars)); totalChars += numChars; } return totalChars; } finally { BufferManager.release(buff, false); } }
Reader stdoutIn = new InputStreamReader(process.getInputStream(), charset); try { char[] buff = BufferManager.getChars(); try { int count;
byte[] bytes = BufferManager.getBytes(); try { char[] chars = BufferManager.getChars(); try { long index = writeStart;