/** * 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(); } }
//throws IOException /* * Read a two-byte short in little-endian order. */ internal int readShortLE(java.io.InputStream inJ) { if (inJ.read(b, 0, 2) == 2) { return (b[0] & 0XFF) | ((b[1] & 0XFF) << 8); } else { throw new java.io.EOFException();//Messages.getString("archive.3C")); } }
//throws IOException /* * Read a four-byte int in little-endian order. */ internal long readIntLE(java.io.InputStream inJ) { if (inJ.read(b, 0, 4) == 4) { return ( ((b[0] & 0XFF)) | ((b[1] & 0XFF) << 8) | ((b[2] & 0XFF) << 16) | ((b[3] & 0XFF) << 24)) & 0XFFFFFFFFL; // Here for sure NO sign extension is wanted. } else { throw new java.io.EOFException();//Messages.getString("archive.3D")); } }
//throws IOException private void myReadFully(java.io.InputStream inJ, byte[] b) { int len = b.Length; int off = 0; while (len > 0) { int count = inJ.read(b, off, len); if (count <= 0) { throw new java.io.EOFException(); } off += count; len -= count; } }
//throws IOException /* * Read a four-byte int in little-endian order. */ internal static long readIntLE(java.io.RandomAccessFile raf) { int b0 = raf.read(); int b1 = raf.read(); int b2 = raf.read(); int b3 = raf.read(); if (b3 < 0) { throw new java.io.EOFException();//Messages.getString("archive.3B")); } return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24); // ATTENTION: DOES SIGN EXTENSION: IS THIS WANTED? }
/* * Helper to read the entire contents of the manifest from the * given input stream. Usually we can do this in a single read * but we need to account for 'infinite' streams, by ensuring we * have a line feed within a reasonable number of characters. */ private byte[] readFully(java.io.InputStream isJ) { // throws IOException { // Initial read byte[] buffer = new byte[4096]; 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; } // Does it look like a manifest? if (!containsLine(buffer, count)) { // archive.2E=Manifest is too long throw new java.io.IOException("Manifest is too long"); //$NON-NLS-1$ } // 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); } }