private void setupTokenDecoder(BoolDecoder bc, int first_partition_length_in_bytes, long offset) throws IOException { long partitionSize = 0; long partitionsStart = offset+first_partition_length_in_bytes; long partition = partitionsStart; multiTokenPartition = bc.readLiteral(2); int num_part = 1 << multiTokenPartition; if (num_part > 1) { partition += 3 * (num_part - 1); } for (int i = 0; i < num_part; i++) { /* * Calculate the length of this partition. The last partition size * is implicit. */ if (i < num_part - 1) { partitionSize = readPartitionSize(partitionsStart+(i*3)); bc.seek(); } else { partitionSize = frame.length() - partition; } tokenBoolDecoders.add(new BoolDecoder(frame, partition)); partition+=partitionSize; } tokenBoolDecoder = tokenBoolDecoders.elementAt(0); }
@Override public long length() { // WTF?! This method is allowed to throw IOException in the interface... try { return stream.length(); } catch (IOException ignore) { } return -1; } }
@Override public long length() { try { long length = stream.length(); return length < 0 ? -1 : Math.min(length - startPos, this.length); } catch (IOException ignore) { } return -1; }
ImageInputStream is = ImageIO.createImageInputStream(new File(pathToImage)); if (is == null || is.length() == 0){ // handle error } Iterator<ImageReader> iterator = ImageIO.getImageReaders(is); if (iterator == null || !iterator.hasNext()) { throw new IOException("Image file format not supported by ImageIO: " + pathToImage); } // We are just looking for the first reader compatible: ImageReader reader = (ImageReader) iterator.next(); iterator = null; reader.setInput(is);
private static void skipToEnd(final ImageInputStream stream) throws IOException { if (stream.length() > 0) { // Seek to end of file stream.seek(stream.length()); } else { // Skip to end long lastGood = stream.getStreamPosition(); while (stream.read() != -1) { lastGood = stream.getStreamPosition(); stream.skipBytes(1024); } stream.seek(lastGood); while (true) { if (stream.read() == -1) { break; } // Just continue reading to EOF... } } }
public long length() { try { long len = iis.length(); return patch == null || len < 0 ? len : len + patch.length; } catch (IOException e) { return -1; } }
@Override public long length() { // WTF?! This method is allowed to throw IOException in the interface... try { return mStream.length(); } catch (IOException ignore) { } return -1; } }
@Override public long length() { try { return in.length(); } catch (IOException ex) { return -1L; } }
@Override public long length() { // WTF?! This method is allowed to throw IOException in the interface... try { return stream.length(); } catch (IOException ignore) { } return -1; } }
private static long skipToEOF(final ImageInputStream stream) throws IOException { long length = stream.length(); if (length > 0) { // Known length, skip there and we're done. stream.seek(length); } else { // Otherwise, seek to EOF the hard way. // First, store stream position... long pos = stream.getStreamPosition(); // ...skip 1k blocks until we're passed EOF... while (stream.skipBytes(1024L) > 0) { if (stream.read() == -1) { break; } pos = stream.getStreamPosition(); } // ...go back to last known pos... stream.seek(pos); // ...finally seek until EOF one byte at a time. Done. while (stream.read() != -1) { } } return stream.getStreamPosition(); }
@Test public void testCreate() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(11), 7); assertEquals(0, stream.getStreamPosition()); assertEquals(7, stream.length()); }
@Override public long length() { try { long length = stream.length(); return length < 0 ? -1 : Math.min(length - startPos, this.length); } catch (IOException ignore) { } return -1; }
@Override public long length() { try { long length = mStream.length(); return length < 0 ? -1 : Math.min(length - mStartPos, mLength); } catch (IOException ignore) { } return -1; }
@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 SubImageInputStream(ImageInputStream in, long offset, long length) throws IOException { this.in = in; this.offset = offset; this.length = length; if (in.length() != -1 && offset + length > in.length()) { throw new IllegalArgumentException("Offset too large. offset=" + offset + " length=" + length + " in.length=" + in.length()); } // setByteOrder(in.getByteOrder()); in.seek(offset); }
private static ucar.unidata.io.RandomAccessFile getRaf(Object input) throws IOException { if (input instanceof String) { String location = (String) input; return getRafFromFile(location); } else if (input instanceof File) { File file = (File) input; return getRafFromFile(file.getAbsolutePath()); } else if (input instanceof ImageInputStream) { ImageInputStream imageInputStream = (ImageInputStream) input; return new ImageInputStreamRandomAccessFile(imageInputStream, imageInputStream.length()); } return null; }
private static RandomAccessFile getRafForTesting(Object input) throws IOException { if (input instanceof String || input instanceof File) { File file = getFile(input); MemoryCacheImageInputStream imageInputStream = new MemoryCacheImageInputStream(openInputStream(file)); return new ImageInputStreamRandomAccessFile(imageInputStream, file.length()); } else if (input instanceof ImageInputStream) { ImageInputStream imageInputStream = (ImageInputStream) input; return new ImageInputStreamRandomAccessFile(imageInputStream, imageInputStream.length()); } throw new IllegalArgumentException(); }
public void testCreate() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(11), 7); assertEquals(0, stream.getStreamPosition()); assertEquals(7, stream.length()); }
public void testCreate() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(11), 7); assertEquals(0, stream.getStreamPosition()); assertEquals(7, stream.length()); }