public int read() throws IOException { if (streamPos >= length) { // Local EOF return -1; } else { int read = stream.read(); if (read >= 0) { streamPos++; } return read; } }
@Override public Directory read(final ImageInputStream input) throws IOException { Validate.notNull(input, "input"); Map<Short, Entry> entries = new LinkedHashMap<>(); // 0x1c identifies start of a tag while (input.read() == 0x1c) { short tagId = input.readShort(); int tagByteCount = input.readUnsignedShort(); boolean array = IPTC.Tags.isArray(tagId); Entry entry = readEntry(input, tagId, tagByteCount, array, array ? entries.get(tagId) : null); if (entry != null) { entries.put(tagId, entry); } } return new IPTCDirectory(entries.values()); }
@Test public void testRead() throws IOException { ImageInputStream wrapped = createStream(42); wrapped.skipBytes(13); ImageInputStream stream = new SubImageInputStream(wrapped, 27); assertEquals(0, stream.getStreamPosition()); assertEquals(27, stream.length()); stream.read(); assertEquals(1, stream.getStreamPosition()); assertEquals(27, stream.length()); stream.readFully(new byte[11]); assertEquals(12, stream.getStreamPosition()); assertEquals(27, stream.length()); assertEquals(25, wrapped.getStreamPosition()); }
public int read() throws IOException { if (hasLength && left-- <= 0) { left = 0; return -1; } return input.read(); }
public int read(final ImageInputStream stream) throws IOException { return stream.read(); }
public int read(final ImageInputStream stream, byte[] b, int off, int len) throws IOException { return stream.read(b, off, len); }
public int read(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { if (streamPos >= length) { // Local EOF return -1; } // Safe cast, as pLength can never cause int overflow int length = (int) Math.min(pLength, this.length - streamPos); int count = stream.read(pBytes, pOffset, length); if (count >= 0) { streamPos += count; } return count; }
private int readDirect(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { // Invalidate the buffer, as its contents is no longer in sync with the stream's position. buffer.limit(0); int read = stream.read(pBuffer, pOffset, pLength); if (read > 0) { streamPos += read; } return read; }
private void fillBuffer() throws IOException { buffer.clear(); int length = stream.read(buffer.array(), 0, buffer.capacity()); if (length >= 0) { buffer.position(length); buffer.flip(); } else { buffer.limit(0); } }
public int read(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { if (hasLength && left <= 0) { return -1; } int read = input.read(pBytes, pOffset, (int) findMaxLen(pLength)); if (hasLength) { left = read < 0 ? 0 : left - read; } return read; }
/** * read a row of data from an uncompressed floating point map. * * <p><b>INFO:</b> this is a reader method. * * @param rowdata the buffer to hold the read row. * @param currentrow the index of the row to read. * @throws IOException * @throws DataFormatException */ private void readUncompressedFPRowByNumber(ByteBuffer rowdata, int currentrow) throws IOException, DataFormatException { int datanumber = nativeRasterRegion.getCols() * numberOfBytesPerValue; imageIS.seek((currentrow * (long) datanumber)); imageIS.read(rowdata.array()); }
public static boolean canDecode(final ImageInputStream pInput) throws IOException { if (pInput == null) { throw new IllegalArgumentException("input == null"); } try { pInput.mark(); for (byte header : WMF.HEADER) { int read = (byte) pInput.read(); if (header != read) { return false; } } return true; } finally { pInput.reset(); } }
@Test(expected = IIOException.class) public void testStreamNonJPEG() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(new ByteArrayInputStream(new byte[] {42, 42, 0, 0, 77, 99}))); stream.read(); }
@Test(expected = IIOException.class) public void testStreamEmpty() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(new ByteArrayInputStream(new byte[0]))); stream.read(); }
@Test public void testEOFSOSSegmentBug() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/eof-sos-segment-bug.jpg"))); long length = 0; while (stream.read() != -1) { length++; } assertEquals(9281L, length); // Sanity check: same as file size, except..? }
@Test public void testStreamRealDataArray() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/invalid-icc-duplicate-sequence-numbers-rgb-internal-kodak-srgb-jfif.jpg"))); byte[] bytes = new byte[20]; // NOTE: read(byte[], int, int) must always read len bytes (or until EOF), due to known bug in Sun code assertEquals(20, stream.read(bytes, 0, 20)); assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xDB, 0x0, 0x43, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1}, bytes); }
@Test public void testEOFExceptionInSegmentParsingShouldNotCreateBadState2() throws IOException { ImageInputStream iis = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/broken-jpeg/51432b02-02a8-11e7-9203-b42b1c43c0c3.jpg"))); byte[] buffer = new byte[4096]; // NOTE: This is a simulation of how the native parts of com.sun...JPEGImageReader would read the image... assertEquals(2, iis.read(buffer, 0, buffer.length)); assertEquals(2, iis.getStreamPosition()); iis.seek(2000); // Just a random postion beyond EOF assertEquals(2000, iis.getStreamPosition()); // So far, so good (but stream position is now really beyond EOF)... // This however, will blow up with an EOFException internally (but we'll return -1 to be good) assertEquals(-1, iis.read(buffer, 0, buffer.length)); assertEquals(-1, iis.read()); assertEquals(2000, iis.getStreamPosition()); // Again, should just continue returning -1 for ever assertEquals(-1, iis.read()); assertEquals(-1, iis.read(buffer, 0, buffer.length)); assertEquals(2000, iis.getStreamPosition()); }
@Test public void testStreamRealDataLength() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/cmm-exception-adobe-rgb.jpg"))); long length = 0; while (stream.read() != -1) { length++; } assertThat(length, new LessOrEqual<>(10203L)); // In no case should length increase assertEquals(9607L, length); // May change, if more chunks are passed to reader... }
@Test public void testReadPaddedSegmentsBug() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-padded-segments.jpg"))); List<JPEGSegment> appSegments = JPEGSegmentUtil.readSegments(stream, JPEGSegmentUtil.APP_SEGMENTS); assertEquals(1, appSegments.size()); assertEquals(JPEG.APP1, appSegments.get(0).marker()); assertEquals("Exif", appSegments.get(0).identifier()); stream.seek(0L); long length = 0; while (stream.read() != -1) { length++; } assertEquals(1061L, length); // Sanity check: same as file size, except padding and the filtered ICC_PROFILE segment }
@Test public void testSeekAfterEOF() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); stream.seek(6); assertEquals(-1, stream.read()); }