将Reader中的内容复制到Writer中

@param reader Reader
@param writer Writer
@param bufferSize 缓存大小
@return 传输的byte数
@throws IORuntimeException IO异常
771075
public static long copy(Reader reader, Writer writer, int bufferSize) throws IORuntimeException {
		return copy(reader, writer, bufferSize, null);
	}
1211665
public static Writer getWriter(OutputStream os, Charset charset) throws IOException {
	if (charset == null) charset = SystemUtil.getCharset();
	return new BufferedWriter(new OutputStreamWriter(os, charset));
    }
1120116
public final static String process(final Reader reader, final Configuration configuration) throws IOException
    {
        final Processor p = new Processor(!(reader instanceof BufferedReader) ? new BufferedReader(reader) : reader,
                configuration);
        return p.process();
    }
1304943
public static BufferedReader getBufferedReader(final File resource, final String encoding) throws IOException {
		return new BufferedReader(getInputStreamReader(resource, encoding));
	}
148937
public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        int availableBytes = input.available();
        log.debug("copy - bufferSize: {} available: {}", bufferSize, availableBytes);
        byte[] buf = null;
        if (availableBytes > 0) {
            if (availableBytes >= bufferSize) {
                buf = new byte[bufferSize];
            } else {
                buf = new byte[availableBytes];
            }
            int bytesRead = input.read(buf);
            while (bytesRead != -1) {
                output.write(buf, 0, bytesRead);
                bytesRead = input.read(buf);
                log.trace("Bytes read: {}", bytesRead);
            }
            output.flush();
        } else {
            log.debug("Available is 0, attempting to read anyway");
            buf = new byte[bufferSize];
            int bytesRead = input.read(buf);
            while (bytesRead != -1) {
                output.write(buf, 0, bytesRead);
                bytesRead = input.read(buf);
                log.trace("Bytes read: {}", bytesRead);
            }
            output.flush();
        }
    }
306634
public static String readerToString(Reader reader) {
		try {
			StringBuilder sb = new StringBuilder();
			char[] buf = new char[1024];
			int numRead = 0;
			while ((numRead = reader.read(buf)) != -1) {
				sb.append(buf, 0, numRead);
			}
			return sb.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
482311
@Override
    public BufferedReader reader() throws IOException {
        byte[] raw = request.getRawBody();
        if (raw != null) {
            return IOUtils.toBufferedReader(new InputStreamReader(new ByteArrayInputStream(raw)));
        }
        return null;
    }
465971
MappableJournalSegmentReader<E> createReader() {
    checkOpen();
    MappableJournalSegmentReader<E> reader = new MappableJournalSegmentReader<>(
        openChannel(file.file()), this, maxEntrySize, index, namespace);
    MappedByteBuffer buffer = writer.buffer();
    if (buffer != null) {
      reader.map(buffer);
    }
    readers.add(reader);
    return reader;
  }
894608
public byte[] readRawBytes(final int size) throws IOException
    {
        if (size < 0)
        {
            throw ProtobufException.negativeSize();
        }

        if (totalBytesRetired + bufferPos + size > currentLimit)
        {
            // Read to the end of the stream anyway.
            skipRawBytes(currentLimit - totalBytesRetired - bufferPos);
            // Then fail.
            throw ProtobufException.truncatedMessage();
        }

        if (size <= bufferSize - bufferPos)
        {
            // We have all the bytes we need already.
            final byte[] bytes = new byte[size];
            System.arraycopy(buffer, bufferPos, bytes, 0, size);
            bufferPos += size;
            return bytes;
        }
        else if (size < buffer.length)
        {
            // Reading more bytes than are in the buffer, but not an excessive number
            // of bytes. We can safely allocate the resulting array ahead of time.

            // First copy what we have.
            final byte[] bytes = new byte[size];
            int pos = bufferSize - bufferPos;
            System.arraycopy(buffer, bufferPos, bytes, 0, pos);
            bufferPos = bufferSize;

            // We want to use refillBuffer() and then copy from the buffer into our
            // byte array rather than reading directly into our byte array because
            // the input may be unbuffered.
            refillBuffer(true);

            while (size - pos > bufferSize)
            {
                System.arraycopy(buffer, 0, bytes, pos, bufferSize);
                pos += bufferSize;
                bufferPos = bufferSize;
                refillBuffer(true);
            }

            System.arraycopy(buffer, 0, bytes, pos, size - pos);
            bufferPos = size - pos;

            return bytes;
        }
        else
        {
            // The size is very large. For security reasons, we can't allocate the
            // entire byte array yet. The size comes directly from the input, so a
            // maliciously-crafted message could provide a bogus very large size in
            // order to trick the app into allocating a lot of memory. We avoid this
            // by allocating and reading only a small chunk at a time, so that the
            // malicious message must actually *be* extremely large to cause
            // problems. Meanwhile, we limit the allowed size of a message elsewhere.

            // Remember the buffer markers since we'll have to copy the bytes out of
            // it later.
            final int originalBufferPos = bufferPos;
            final int originalBufferSize = bufferSize;

            // Mark the current buffer consumed.
            totalBytesRetired += bufferSize;
            bufferPos = 0;
            bufferSize = 0;

            // Read all the rest of the bytes we need.
            int sizeLeft = size - (originalBufferSize - originalBufferPos);
            final List<byte[]> chunks = new ArrayList<byte[]>();

            while (sizeLeft > 0)
            {
                final byte[] chunk = new byte[Math.min(sizeLeft, buffer.length)];
                int pos = 0;
                while (pos < chunk.length)
                {
                    final int n = (input == null) ? -1 :
                            input.read(chunk, pos, chunk.length - pos);
                    if (n == -1)
                    {
                        throw ProtobufException.truncatedMessage();
                    }
                    totalBytesRetired += n;
                    pos += n;
                }
                sizeLeft -= chunk.length;
                chunks.add(chunk);
            }

            // OK, got everything. Now concatenate it all into one buffer.
            final byte[] bytes = new byte[size];

            // Start by copying the leftover bytes from this.buffer.
            int pos = originalBufferSize - originalBufferPos;
            System.arraycopy(buffer, originalBufferPos, bytes, 0, pos);

            // And now all the chunks.
            for (final byte[] chunk : chunks)
            {
                System.arraycopy(chunk, 0, bytes, pos, chunk.length);
                pos += chunk.length;
            }

            // Done.
            return bytes;
        }
    }
734581
public Toml read(Reader reader) {
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(reader);

      StringBuilder w = new StringBuilder();
      String line = bufferedReader.readLine();
      while (line != null) {
        w.append(line).append('\n');
        line = bufferedReader.readLine();
      }
      read(w.toString());
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      try {
        bufferedReader.close();
      } catch (IOException e) {}
    }
    return this;
  }
1116652
public static boolean copy(InputStream input, OutputStream output, int bufferSize)
   {
      try
      {
         byte[] buffer = new byte[bufferSize];

         int read = input.read(buffer);
         while (read != -1) {
            output.write(buffer, 0, read);
            read = input.read(buffer);
         }

         output.flush();
      }
      catch (IOException e) {
         return false;
      }
      return true;
   }
683604
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws IORuntimeException {
		return copy(in, out, bufferSize, null);
	}
28544
void parseDocument(
            final Reader reader, final int suggestedBufferSize,
            final IMarkupHandler handler, final ParseStatus status)
            throws ParseException {


        final long parsingStartTimeNanos = System.nanoTime();

        char[] buffer = null;

        try {

            handler.handleDocumentStart(parsingStartTimeNanos, 1, 1);

            int bufferSize = suggestedBufferSize;
            buffer = this.pool.allocateBuffer(bufferSize);

            int bufferContentSize = reader.read(buffer);

            boolean cont = (bufferContentSize != -1);

            status.offset = -1;
            status.line = 1;
            status.col = 1;
            status.inStructure = false;
            status.parsingDisabled = true;
            status.parsingDisabledLimitSequence = null;
            status.autoCloseRequired = null;
            status.autoCloseLimits = null;

            while (cont) {

                parseBuffer(buffer, 0, bufferContentSize, handler, status);

                int readOffset = 0;
                int readLen = bufferSize;

                if (status.offset == 0) {

                    if (bufferContentSize == bufferSize) {
                        // Buffer is not big enough, double it!

                        char[] newBuffer = null;
                        try {

                            bufferSize *= 2;

                            newBuffer = this.pool.allocateBuffer(bufferSize);
                            System.arraycopy(buffer, 0, newBuffer, 0, bufferContentSize);

                            this.pool.releaseBuffer(buffer);

                            buffer = newBuffer;

                        } catch (final Exception ignored) {
                            this.pool.releaseBuffer(newBuffer);
                        }

                    }

                    // it's possible for two reads to occur in a row and 1) read less than the bufferSize and 2)
                    // still not find the next tag/end of structure
                    readOffset = bufferContentSize;
                    readLen = bufferSize - readOffset;

                } else if (status.offset < bufferContentSize) {

                    System.arraycopy(buffer, status.offset, buffer, 0, bufferContentSize - status.offset);

                    readOffset = bufferContentSize - status.offset;
                    readLen = bufferSize - readOffset;

                    status.offset = 0;
                    bufferContentSize = readOffset;

                }

                final int read = reader.read(buffer, readOffset, readLen);
                if (read != -1) {
                    bufferContentSize = readOffset + read;
                } else {
                    cont = false;
                }

            }

            // Iteration done, now it's time to clean up in case we still have some text to be notified

            int lastLine = status.line;
            int lastCol = status.col;

            final int lastStart = status.offset;
            final int lastLen = bufferContentSize - lastStart;

            if (lastLen > 0) {

                if (status.inStructure) {
                    throw new ParseException(
                            "Incomplete structure: \"" + new String(buffer, lastStart, lastLen) + "\"", status.line, status.col);
                }

                handler.handleText(buffer, lastStart, lastLen, status.line, status.col);

                // As we have produced an additional text event, we need to fast-forward the
                // lastLine and lastCol position to include the last text structure.
                for (int i = lastStart; i < (lastStart + lastLen); i++) {
                    final char c = buffer[i];
                    if (c == '\n') {
                        lastLine++;
                        lastCol = 1;
                    } else {
                        lastCol++;
                    }

                }

            }

            final long parsingEndTimeNanos = System.nanoTime();
            handler.handleDocumentEnd(parsingEndTimeNanos, (parsingEndTimeNanos - parsingStartTimeNanos), lastLine, lastCol);

        } catch (final ParseException e) {
            throw e;
        } catch (final Exception e) {
            throw new ParseException(e);
        } finally {
            this.pool.releaseBuffer(buffer);
            try {
                reader.close();
            } catch (final Throwable ignored) {
                // This exception can be safely ignored
            }
        }

    }
425576
public Reader createReader(byte[] cachedBytes) throws IOException {
	if (cachedBytes != null) {
	    return new InputStreamReader(new ByteArrayInputStream(cachedBytes));
	}
        if (inputStream != null) {
            return new InputStreamReader(inputStream, charset);
        }
        if (reader != null) {
            return reader;
        }
        return new InputStreamReader(new FileInputStream(file), charset);
    }
915986
private static void copyStream(InputStream is, OutputStream os, int bufferSize)
            throws IOException {
        Assert.checkNotNullParam("is", is);
        Assert.checkNotNullParam("os", os);
        byte[] buff = new byte[bufferSize];
        int rc;
        while ((rc = is.read(buff)) != -1) os.write(buff, 0, rc);
        os.flush();
    }
307715
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
        final int aLen = bufferA.readableBytes();
        final int bLen = bufferB.readableBytes();
        final int minLength = Math.min(aLen, bLen);
        final int uintCount = minLength >>> 2;
        final int byteCount = minLength & 3;
        int aIndex = bufferA.readerIndex();
        int bIndex = bufferB.readerIndex();

        if (uintCount > 0) {
            boolean bufferAIsBigEndian = bufferA.order() == ByteOrder.BIG_ENDIAN;
            final long res;
            int uintCountIncrement = uintCount << 2;

            if (bufferA.order() == bufferB.order()) {
                res = bufferAIsBigEndian ? compareUintBigEndian(bufferA, bufferB, aIndex, bIndex, uintCountIncrement) :
                        compareUintLittleEndian(bufferA, bufferB, aIndex, bIndex, uintCountIncrement);
            } else {
                res = bufferAIsBigEndian ? compareUintBigEndianA(bufferA, bufferB, aIndex, bIndex, uintCountIncrement) :
                        compareUintBigEndianB(bufferA, bufferB, aIndex, bIndex, uintCountIncrement);
            }
            if (res != 0) {
                // Ensure we not overflow when cast
                return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, res));
            }
            aIndex += uintCountIncrement;
            bIndex += uintCountIncrement;
        }

        for (int aEnd = aIndex + byteCount; aIndex < aEnd; ++aIndex, ++bIndex) {
            int comp = bufferA.getUnsignedByte(aIndex) - bufferB.getUnsignedByte(bIndex);
            if (comp != 0) {
                return comp;
            }
        }

        return aLen - bLen;
    }
305363
void parseDocument(final Reader reader, final int suggestedBufferSize, final IRawHandler handler)
            throws RawParseException {

        // We are trying to read the entire resource into the buffer. If it's not big enough,
        // then it will have to be grown. Note grown buffers are not maintained by the pool,
        // so template cache should play a more important role for this, allowing the system to
        // not require the creation of large buffers each time.

        final long parsingStartTimeNanos = System.nanoTime();

        char[] buffer = null;

        try {

            handler.handleDocumentStart(parsingStartTimeNanos, 1, 1);

            int bufferSize = suggestedBufferSize;
            buffer = this.pool.allocateBuffer(bufferSize);

            int bufferContentSize = reader.read(buffer);

            boolean cont = (bufferContentSize != -1);

            while (cont) {

                if (bufferContentSize == bufferSize) {
                    // Buffer is not big enough, double it!

                    char[] newBuffer = null;
                    try {

                        bufferSize *= 2;

                        newBuffer = this.pool.allocateBuffer(bufferSize);
                        System.arraycopy(buffer, 0, newBuffer, 0, bufferContentSize);

                        this.pool.releaseBuffer(buffer);

                        buffer = newBuffer;

                    } catch (final Exception ignored) {
                        this.pool.releaseBuffer(newBuffer);
                    }

                }

                final int read = reader.read(buffer, bufferContentSize, (bufferSize - bufferContentSize));
                if (read != -1) {
                    bufferContentSize += read;
                } else {
                    cont = false;
                }

            }

            handler.handleText(buffer, 0, bufferContentSize, 1, 1);

            final int[] lastLineCol = computeLastLineCol(buffer, bufferContentSize);

            final long parsingEndTimeNanos = System.nanoTime();
            handler.handleDocumentEnd(parsingEndTimeNanos, (parsingEndTimeNanos - parsingStartTimeNanos), lastLineCol[0], lastLineCol[1]);

        } catch (final RawParseException e) {
            throw e;
        } catch (final Exception e) {
            throw new RawParseException(e);
        } finally {
            this.pool.releaseBuffer(buffer);
            try {
                reader.close();
            } catch (final Throwable ignored) {
                // This exception can be safely ignored
            }
        }

    }