javax.imageio.stream.ImageInputStreamImpl Java Examples

The following examples show how to use javax.imageio.stream.ImageInputStreamImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RewindableLineReaderTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a reader over a dummy sequence of characters. That reader returns letters
 * from A to Z, than restart that sequence an infinite number of times.
 */
private static RewindableLineReader reader() throws IOException {
    return new RewindableLineReader(new InputStreamAdapter(new ImageInputStreamImpl() {
        /** Next byte to return, in A … Z range. */
        private char next = 'A';

        /** Returns the next byte in A … Z range. */
        @Override public int read() {
            final char c = next;
            if (++next > 'Z') {
                next = 'A';
            }
            return c;
        }

        /**
         * Transfers at most {@value #TRANSFERT_SIZE} bytes. We put a limit in the number of bytes
         * to be transfered in order to cause {@link BufferedReader} to invalidate the mark sooner
         * than waiting that we have filled the buffer.
         */
        @Override public int read(final byte[] buffer, int offset, int length) {
            if (length > TRANSFERT_SIZE) {
                length = TRANSFERT_SIZE;
            }
            for (int i=0; i<length; i++) {
                buffer[offset++] = (byte) read();
            }
            return length;
        }

        /** The only seek allowed for this test should be at the beginning of stream. */
        @Override public void seek(final long pos) throws IOException {
            assertEquals("Should seek at origin.", 0, pos);
            next = 'A';
        }
    }), StandardCharsets.US_ASCII);
}
 
Example #2
Source File: FacetHeatmap.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
static BufferedImage readImage(final byte[] bytes) {
  // Wrap ImageInputStream around the bytes.  We could use MemoryCacheImageInputStream but it will
  // cache the data which is quite unnecessary given we have it all in-memory already.
  ImageInputStream imageInputStream = new ImageInputStreamImpl() {
    //TODO re-use this instance; superclass has 8KB buffer.

    @Override
    public int read() throws IOException {
      checkClosed();
      bitOffset = 0;
      if (streamPos >= bytes.length) {
        return -1;
      } else {
        return bytes[(int) streamPos++];
      }
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      checkClosed();
      bitOffset = 0;
      if (streamPos >= bytes.length) {
        return -1;
      } else {
        int copyLen = Math.min(len, bytes.length - (int)streamPos);
        System.arraycopy(bytes, (int)streamPos, b, off, copyLen);
        streamPos += copyLen;
        return copyLen;
      }
    }

    @Override
    public long length() {
      return bytes.length;
    }

    @Override
    public boolean isCached() {
      return true;
    }

    @Override
    public boolean isCachedMemory() {
      return true;
    }
  };
  try {
    //TODO can/should we re-use an imageReader instance on FacetInfo?
    ImageReader imageReader = imageReaderSpi.createReaderInstance();

    imageReader.setInput(imageInputStream,
        false,//forwardOnly
        true);//ignoreMetadata
    return imageReader.read(0);//read first & only image
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Problem reading png heatmap: " + e);
  }
}