1822N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1822N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1822N/A *
1822N/A * This code is free software; you can redistribute it and/or modify it
1822N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1822N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1822N/A *
1822N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1822N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1822N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1822N/A * version 2 for more details (a copy is included in the LICENSE file that
1822N/A * accompanied this code).
1822N/A *
1822N/A * You should have received a copy of the GNU General Public License version
1822N/A * 2 along with this work; if not, write to the Free Software Foundation,
1822N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1822N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1822N/A */
1822N/A
1822N/A/**
1822N/A * IOUtils: A collection of IO-related public static methods.
1822N/A */
1822N/A
1822N/Apackage sun.misc;
1822N/A
1822N/Aimport java.io.EOFException;
1822N/Aimport java.io.IOException;
1822N/Aimport java.io.InputStream;
1822N/Aimport java.util.Arrays;
1822N/A
1822N/Apublic class IOUtils {
1822N/A
1822N/A /**
1822N/A * Read up to <code>length</code> of bytes from <code>in</code>
1822N/A * until EOF is detected.
1822N/A * @param in input stream, must not be null
1822N/A * @param length number of bytes to read, -1 or Integer.MAX_VALUE means
1822N/A * read as much as possible
1822N/A * @param readAll if true, an EOFException will be thrown if not enough
1822N/A * bytes are read. Ignored when length is -1 or Integer.MAX_VALUE
1822N/A * @return bytes read
1822N/A * @throws IOException Any IO error or a premature EOF is detected
1822N/A */
1822N/A public static byte[] readFully(InputStream is, int length, boolean readAll)
1822N/A throws IOException {
1822N/A byte[] output = {};
1822N/A if (length == -1) length = Integer.MAX_VALUE;
1822N/A int pos = 0;
1822N/A while (pos < length) {
1822N/A int bytesToRead;
1822N/A if (pos >= output.length) { // Only expand when there's no room
1822N/A bytesToRead = Math.min(length - pos, output.length + 1024);
1822N/A if (output.length < pos + bytesToRead) {
1822N/A output = Arrays.copyOf(output, pos + bytesToRead);
1822N/A }
1822N/A } else {
1822N/A bytesToRead = output.length - pos;
1822N/A }
1822N/A int cc = is.read(output, pos, bytesToRead);
1822N/A if (cc < 0) {
1822N/A if (readAll && length != Integer.MAX_VALUE) {
1822N/A throw new EOFException("Detect premature EOF");
1822N/A } else {
1822N/A if (output.length != pos) {
1822N/A output = Arrays.copyOf(output, pos);
1822N/A }
1822N/A break;
1822N/A }
1822N/A }
1822N/A pos += cc;
1822N/A }
1822N/A return output;
1822N/A }
1822N/A}