@Override public boolean canDecodeInput(final Object source) throws IOException { if (!(source instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) source; stream.mark(); try { // NOTE: All images I have found starts with #?RADIANCE (or has no #? line at all), // although some sources claim that #?RGBE is also used. byte[] magic = new byte[HDR.RADIANCE_MAGIC.length]; stream.readFully(magic); return Arrays.equals(HDR.RADIANCE_MAGIC, magic) || Arrays.equals(HDR.RGBE_MAGIC, Arrays.copyOf(magic, 6)); } finally { stream.reset(); } }
private static boolean canDecode(ImageInputStream input) throws IOException { try { input.mark(); return input.readInt() == ICNS.MAGIC; } finally { input.reset(); } }
private static int readMagic(final ImageInputStream stream) throws IOException { stream.mark(); try { return stream.readInt(); } finally { stream.reset(); } }
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(); } }
private List<JPEGSegment> readSegments() throws IOException { imageInput.mark(); try { imageInput.seek(streamOffsets.get(currentStreamIndex)); return JPEGSegmentUtil.readSegments(imageInput, SEGMENT_IDENTIFIERS); } catch (IIOException | IllegalArgumentException ignore) { if (DEBUG) { ignore.printStackTrace(); } } finally { imageInput.reset(); } // In case of an exception, avoid NPE when referencing segments later return Collections.emptyList(); }
/** * Look for an {@link ImageReader} instance that is able to read the provided {@link * ImageInputStream}, which must be non null. * * <p>In case no reader is found, <code>null</code> is returned. * * @param inStream an instance of {@link ImageInputStream} for which we need to find a suitable * {@link ImageReader}. * @return a suitable instance of {@link ImageReader} or <code>null</code> if one cannot be * found. */ public static ImageReader getImageioReader(final ImageInputStream inStream) { Utilities.ensureNonNull("inStream", inStream); // get a reader inStream.mark(); final Iterator<ImageReader> readersIt = ImageIO.getImageReaders(inStream); if (!readersIt.hasNext()) { return null; } return readersIt.next(); }
/** * Close the {@link InStream} {@link ImageInputStream} if we open it up on purpose to read * header info for this {@link AbstractGridCoverage2DReader}. If the stream cannot be closed, we * just reset and mark it. * * @throws IOException */ private void finalStreamPreparation() throws IOException { if (closeMe) inStream.close(); else { inStream.reset(); inStream.mark(); } }
/** * Look for an {@link ImageReader} instance that is able to read the provided {@link * ImageInputStream}, which must be non null. * * <p> * * <p>In case no reader is found, <code>null</code> is returned. * * @param inStream an instance of {@link ImageInputStream} for which we need to find a suitable * {@link ImageReader}. * @return a suitable instance of {@link ImageReader} or <code>null</code> if one cannot be * found. */ static ImageReader getReader(final ImageInputStream inStream) { Utilities.ensureNonNull("inStream", inStream); // get a reader inStream.mark(); final Iterator<ImageReader> readersIt = ImageIO.getImageReaders(inStream); if (!readersIt.hasNext()) { return null; } return readersIt.next(); }
private static boolean isAppSegmentWithId(final String segmentId, final ImageInputStream stream) throws IOException { notNull(segmentId, "segmentId"); stream.mark(); try { int length = stream.readUnsignedShort(); // Length including length field itself byte[] data = new byte[Math.min(segmentId.length() + 1, length - 2)]; stream.readFully(data); return segmentId.equals(asNullTerminatedAsciiString(data, 0)); } finally { stream.reset(); } }
public static ImageReaderSpi getReaderSpiFromStream( ImageReaderSpi suggestedSPI, ImageInputStream inStream) throws IOException { ImageReaderSpi readerSPI = null; // get a reader and try to cache the suggested SPI first inStream.mark(); if (suggestedSPI != null && suggestedSPI.canDecodeInput(inStream)) { readerSPI = suggestedSPI; inStream.reset(); } else { inStream.mark(); ImageReader reader = ImageIOExt.getImageioReader(inStream); if (reader != null) readerSPI = reader.getOriginatingProvider(); inStream.reset(); } return readerSPI; }
/** * Returns an {@link ImageReaderSpi} instance for the input {@link ImageInputStream} and the * optional suggested spi. */ public static ImageReaderSpi getReaderSpiFromStream( ImageReaderSpi suggestedSPI, ImageInputStream inStream) throws IOException { ImageReaderSpi readerSPI = null; // get a reader and try to use the suggested SPI first inStream.mark(); if (suggestedSPI != null && suggestedSPI.canDecodeInput(inStream)) { readerSPI = suggestedSPI; inStream.reset(); } else { inStream.mark(); ImageReader reader = ImageIOExt.getImageioReader(inStream); if (reader != null) readerSPI = reader.getOriginatingProvider(); inStream.reset(); } return readerSPI; }
public boolean canDecodeInput(final Object pSource) throws IOException { if (!(pSource instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) pSource; stream.mark(); try { if (isPICT(stream)) { // If PICT Clipping format, return true immediately return true; } else { // Skip header 512 bytes for file-based streams stream.reset(); // We need to set mark again, to make sure the reset call in // the finally block will not consume existing marks stream.mark(); skipNullHeader(stream); } return isPICT(stream); } catch (EOFException ignore) { return false; } finally { stream.reset(); } }
static boolean canDecode(final ImageInputStream pInput, final int pType) throws IOException { byte[] signature = new byte[4]; try { pInput.mark(); pInput.readFully(signature); int count = pInput.readByte() + (pInput.readByte() << 8); return (signature[0] == 0x0 && signature[1] == 0x0 && signature[2] == pType && signature[3] == 0x0 && count > 0); } finally { pInput.reset(); } }
public boolean canDecodeInput(final Object pSource) throws IOException { if (!(pSource instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) pSource; stream.mark(); try { if (stream.readInt() == PSD.SIGNATURE_8BPS) { int version = stream.readUnsignedShort(); switch (version) { case PSD.VERSION_PSD: case PSD.VERSION_PSB: break; default: return false; } return true; } return false; } finally { stream.reset(); } }
private static boolean canDecode(ImageInputStream pInput) throws IOException { pInput.mark(); try { // Is it IFF if (pInput.readInt() == IFF.CHUNK_FORM) { pInput.readInt();// Skip length field int type = pInput.readInt(); // Is it ILBM or PBM if (type == IFF.TYPE_ILBM || type == IFF.TYPE_PBM) { return true; } } } finally { pInput.reset(); } return false; }
@Override public boolean canDecodeInput(final Object source) throws IOException { if (!(source instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) source; stream.mark(); try { short magic = stream.readShort(); switch (magic) { case SGI.MAGIC: byte compression = stream.readByte(); byte bpp = stream.readByte(); return (compression == SGI.COMPRESSION_NONE || compression == SGI.COMPRESSION_RLE) && (bpp == 1 || bpp == 2); default: return false; } } finally { stream.reset(); } }
@Override public boolean canDecodeInput(final Object source) throws IOException { if (!(source instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) source; stream.mark(); try { ByteOrder originalByteOrder = stream.getByteOrder(); stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); try { return stream.readInt() == DCX.MAGIC; } finally { stream.setByteOrder(originalByteOrder); } } finally { stream.reset(); } }
static boolean canDecodeAs(final Object pSource, final int magic) throws IOException { if (!(pSource instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) pSource; stream.mark(); try { byte[] bom = new byte[2]; stream.readFully(bom); ByteOrder originalOrder = stream.getByteOrder(); try { if (bom[0] == 'I' && bom[1] == 'I') { stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); } else if (bom[0] == 'M' && bom[1] == 'M') { stream.setByteOrder(ByteOrder.BIG_ENDIAN); } else { return false; } return stream.readUnsignedShort() == magic; } finally { stream.setByteOrder(originalOrder); } } finally { stream.reset(); } }
@Test public void testReadResetRead() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(32), 16); stream.mark(); byte[] first = new byte[16]; stream.readFully(first); stream.reset(); byte[] second = new byte[16]; stream.readFully(second); assertTrue(Arrays.equals(first, second)); }
@Test public void testProviderShouldNotConsumeExistingMarks() throws IOException { try (ImageInputStream stream = new ByteArrayImageInputStream(new byte[1024])) { stream.seek(42); stream.mark(); stream.seek(123); sProvider.canDecodeInput(stream); assertEquals(123, stream.getStreamPosition()); stream.reset(); assertEquals(42, stream.getStreamPosition()); } }