/** * Discards the initial position of the stream prior to the current stream position. * The implementation is as below: * * {@preformat java * flushBefore(getStreamPosition()); * } * * @throws IOException if an I/O error occurred. */ @Override public final void flush() throws IOException { flushBefore(getStreamPosition()); }
/** * Reads a byte from the stream and returns a {@code true} if it is nonzero, {@code false} otherwise. * The implementation is as below: * * {@preformat java * return readByte() != 0; * } * * @return the value of the next boolean from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ @Override public final boolean readBoolean() throws IOException { return readByte() != 0; }
switch (operation) { default: throw new AssertionError(operation); case 0: assertEquals("read()", r.read(), t.read()); break; case 1: assertEquals("readBoolean()", r.readBoolean(), t.readBoolean()); break; case 2: assertEquals("readChar()", r.readChar(), t.readChar()); break; case 3: assertEquals("readByte()", r.readByte(), t.readByte()); break; case 4: assertEquals("readShort()", r.readShort(), t.readShort()); break; case 5: assertEquals("readUnsignedShort()", r.readUnsignedShort(), t.readUnsignedShort()); break; case 6: assertEquals("readInt()", r.readInt(), t.readInt()); break; case 7: assertEquals("readUnsignedInt()", r.readUnsignedInt(), t.readUnsignedInt()); break; case 8: assertEquals("readLong()", r.readLong(), t.readLong()); break; case 9: assertEquals("readFloat()", r.readFloat(), t.readFloat(), 0f); break; case 10: assertEquals("readDouble()", r.readDouble(), t.readDouble(), 0d); break; case 11: assertEquals("readBit()", r.readBit(), t.readBit()); break; case 12: { final int n = random.nextInt(Long.SIZE + 1); assertEquals("readBits(" + n + ')', r.readBits(n), t.readBits(n)); break; final int length = random.nextInt(ARRAY_MAX_LENGTH); final byte[] actual = new byte[length]; final int n = t.read(actual); assertFalse("Reached EOF", n < 0); final byte[] expected = new byte[n]; final int length = random.nextInt(ARRAY_MAX_LENGTH); final byte[] expected = new byte[length]; r.readFully(expected); final byte[] actual = new byte[length]; t.readFully(actual); assertArrayEquals("readFully(byte[])", expected, actual);
final ChannelDataOutput c = (ChannelDataOutput) stream; if (c.channel instanceof ReadableByteChannel) { stream = new ChannelImageInputStream(c.filename, (ReadableByteChannel) c.channel, c.buffer, true);
/** * Fills a buffer with random data and compares the result with a standard image input stream. * We will allocate a small buffer for the {@code ChannelImageInputStream} in order to force * frequent interactions between the buffer and the channel. * * @throws IOException should never happen since we read and write in memory only. */ @Test public void testWithRandomData() throws IOException { final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_MAX_CAPACITY); final ByteOrder byteOrder = random.nextBoolean() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; final byte[] data = createRandomArray(STREAM_LENGTH); referenceStream = ImageIO.createImageInputStream(new ByteArrayInputStream(data)); referenceStream.setByteOrder(byteOrder); testedStream = new ChannelImageInputStream("testWithRandomData", Channels.newChannel(new ByteArrayInputStream(data)), buffer, false); testedStream.setByteOrder(byteOrder); transferRandomData(testedStream, data.length - ARRAY_MAX_LENGTH, 24); }
/** * Reads up to {@code dest.length} bytes from the stream, and stores them into * {@code dest} starting at index 0. The default implementation is as below: * * {@preformat java * return read(dest, 0, dest.length); * } * * @param dest an array of bytes to be written to. * @return the number of bytes actually read, or -1 on EOF. * @throws IOException if an error occurred while reading. */ @Override public final int read(final byte[] dest) throws IOException { return read(dest, 0, dest.length); }
/** * Advances the current stream position by the given amount of bytes. * The bit offset is reset to 0 by this method. * * @param n the number of bytes to seek forward. * @return the number of bytes skipped. * @throws IOException if an error occurred while skipping. */ @Override public final long skipBytes(final long n) throws IOException { return skipBytes((int) Math.min(n, Integer.MAX_VALUE)); }
/** * Returns the next byte from the stream as an unsigned integer between 0 and 255, * or -1 if we reached the end of stream. * * @return the next byte as an unsigned integer, or -1 on end of stream. * @throws IOException if an error occurred while reading the stream. */ @Override public final int read() throws IOException { return hasRemaining() ? JDK8.toUnsignedInt(buffer.get()) : -1; }
final ChannelDataOutput c = (ChannelDataOutput) stream; if (c.channel instanceof ReadableByteChannel) { stream = new ChannelImageInputStream(c.filename, (ReadableByteChannel) c.channel, c.buffer, true);
/** * Reads up to {@code dest.length} bytes from the stream, and stores them into * {@code dest} starting at index 0. The default implementation is as below: * * {@preformat java * return read(dest, 0, dest.length); * } * * @param dest an array of bytes to be written to. * @return the number of bytes actually read, or -1 on EOF. * @throws IOException if an error occurred while reading. */ @Override public final int read(final byte[] dest) throws IOException { return read(dest, 0, dest.length); }
/** * Advances the current stream position by the given amount of bytes. * The bit offset is reset to 0 by this method. * * @param n the number of bytes to seek forward. * @return the number of bytes skipped. * @throws IOException if an error occurred while skipping. */ @Override public final long skipBytes(final long n) throws IOException { return skipBytes((int) Math.min(n, Integer.MAX_VALUE)); }
/** * Skips over <var>n</var> bytes of data from the input stream. * This implementation does not skip more bytes than the buffer capacity. * * @param n maximal number of bytes to skip. * @return number of bytes actually skipped. * @throws IOException if an error occurred while reading. */ @Override public final int skipBytes(int n) throws IOException { if (!hasRemaining()) { return 0; } int r = buffer.remaining(); if (n >= r) { n = r; } buffer.position(buffer.position() + n); return n; }
/** * Discards the initial position of the stream prior to the current stream position. * The implementation is as below: * * {@preformat java * flushBefore(getStreamPosition()); * } * * @throws IOException if an I/O error occurred. */ @Override public final void flush() throws IOException { flushBefore(getStreamPosition()); }
asDataInput = (DataInput) in; } else { asDataInput = new ChannelImageInputStream(in); // Upgrade existing instance. c.view = asDataInput;
/** * Reads up to {@code length} bytes from the stream, and modifies the supplied * {@code IIOByteBuffer} to indicate the byte array, offset, and length where * the data may be found. * * @param dest the buffer to be written to. * @param length the maximum number of bytes to read. * @throws IOException if an error occurred while reading. */ @Override public final void readBytes(final IIOByteBuffer dest, int length) throws IOException { final byte[] data = new byte[length]; length = read(data); dest.setData(data); dest.setOffset(0); dest.setLength(length); }
/** * Reads a byte from the stream and returns a {@code true} if it is nonzero, {@code false} otherwise. * The implementation is as below: * * {@preformat java * return readByte() != 0; * } * * @return the value of the next boolean from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ @Override public final boolean readBoolean() throws IOException { return readByte() != 0; }
/** * Reads up to {@code length} bytes from the stream, and stores them into {@code dest} * starting at index {@code offset}. If no bytes can be read because the end of the stream * has been reached, -1 is returned. * * @param dest an array of bytes to be written to. * @param offset the starting position within {@code dest} to write. * @param length the maximum number of bytes to read. * @return the number of bytes actually read, or -1 on EOF. * @throws IOException if an error occurred while reading. */ @Override public final int read(final byte[] dest, int offset, int length) throws IOException { if (!hasRemaining()) { return -1; } final int requested = length; while (length != 0 && hasRemaining()) { final int n = Math.min(buffer.remaining(), length); buffer.get(dest, offset, n); offset += n; length -= n; } return requested - length; }
asDataInput = (DataInput) in; } else { asDataInput = new ChannelImageInputStream(in); // Upgrade existing instance. c.view = asDataInput;