Exemple #1
0
 private static void closeQuietly(java.io.InputStream inJ)
 {
     if (inJ == null) {
         return;
     }
     try {
         inJ.close ();
     } catch (java.io.IOException e) {
     }
 }
        /**
         * Reads all the bytes from the given input stream.
         *
         * Calls read multiple times on the given input stream until it receives an
         * end of file marker. Returns the combined results as a byte array. Note
         * that this method may block if the underlying stream read blocks.
         *
         * @param is
         *            the input stream to be read.
         * @return the content of the stream as a byte array.
         * @throws IOException
         *             if a read error occurs.
         */
        public static byte[] readFullyAndClose(java.io.InputStream isJ)
        {
            // throws IOException {

            try {
                // Initial read
                byte[] buffer = new byte[1024];
                int count = isJ.read(buffer);
                int nextByte = isJ.read();

                // Did we get it all in one read?
                if (nextByte == -1) {
                    byte[] dest = new byte[count];
                    java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                    return dest;
                }

                // Requires additional reads
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
                baos.write(buffer, 0, count);
                baos.write(nextByte);
                while (true) {
                    count = isJ.read(buffer);
                    if (count == -1) {
                        return baos.toByteArray();
                    }
                    baos.write(buffer, 0, count);
                }
            } finally {
                isJ.close();
            }
        }
Exemple #3
0
 private static void closeQuietly(java.io.Writer outJ)
 {
     if (outJ == null) {
         return;
     }
     try {
         outJ.close ();
     } catch (java.io.IOException e) {
     }
 }