Java Code Examples for javax.imageio.stream.ImageInputStream#readUnsignedInt()

The following examples show how to use javax.imageio.stream.ImageInputStream#readUnsignedInt() . 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: ReadUnsignedIntTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    dos.writeInt(1);
    dos.writeInt(0x7fffffff);
    dos.writeInt(0x8fffffff);
    dos.writeInt(0xffffffff);

    dos.close();

    ByteArrayInputStream bais =
        new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    for (int i=0; i<4; i++) {
        long res = iis.readUnsignedInt();
        if (res <= 0) {
            throw new RuntimeException("Negative number was read: "+
                                       Long.toString(res, 16));
        }
    }
}
 
Example 2
Source File: ReadUnsignedIntTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    dos.writeInt(1);
    dos.writeInt(0x7fffffff);
    dos.writeInt(0x8fffffff);
    dos.writeInt(0xffffffff);

    dos.close();

    ByteArrayInputStream bais =
        new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    for (int i=0; i<4; i++) {
        long res = iis.readUnsignedInt();
        if (res <= 0) {
            throw new RuntimeException("Negative number was read: "+
                                       Long.toString(res, 16));
        }
    }
}
 
Example 3
Source File: WebPImageReaderSpi.java    From j-webp with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canDecodeInput( Object source ) throws IOException {
  if ( !( source instanceof ImageInputStream ) ) {
    return false;
  }

  ImageInputStream stream = ( ImageInputStream ) source;
  byte[] b = new byte[ 4 ];
  ByteOrder oldByteOrder = stream.getByteOrder();
  stream.mark();
  stream.setByteOrder( ByteOrder.LITTLE_ENDIAN );

  try {
    stream.readFully( b );
    if ( !Arrays.equals( b, RIFF ) ) {
      return false;
    }
    long chunkLength = stream.readUnsignedInt();
    long streamLength = stream.length();
    if ( streamLength != -1 && streamLength != chunkLength + 8 ) {
      return false;
    }
    stream.readFully( b );
    if ( !Arrays.equals( b, WEBP ) ) {
      return false;
    }

    stream.readFully( b );
    if ( !Arrays.equals( b, VP8_ ) && !Arrays.equals( b, VP8L ) && !Arrays.equals( b, VP8X ) ) {
      return false;
    }
  } finally {
    stream.setByteOrder( oldByteOrder );
    stream.reset();
  }

  return true;
}
 
Example 4
Source File: WebPImageReaderSpi.java    From webp-imageio with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canDecodeInput( Object source ) throws IOException {
  if ( !( source instanceof ImageInputStream ) ) {
    return false;
  }

  ImageInputStream stream = ( ImageInputStream ) source;
  byte[] b = new byte[ 4 ];
  ByteOrder oldByteOrder = stream.getByteOrder();
  stream.mark();
  stream.setByteOrder( ByteOrder.LITTLE_ENDIAN );

  try {
    stream.readFully( b );
    if ( !Arrays.equals( b, RIFF ) ) {
      return false;
    }
    long chunkLength = stream.readUnsignedInt();
    long streamLength = stream.length();
    if ( streamLength != -1 && streamLength != chunkLength + 8 ) {
      return false;
    }
    stream.readFully( b );
    if ( !Arrays.equals( b, WEBP ) ) {
      return false;
    }

    stream.readFully( b );
    if ( !Arrays.equals( b, VP8_ ) && !Arrays.equals( b, VP8X ) ) {
      return false;
    }
  } finally {
    stream.setByteOrder( oldByteOrder );
    stream.reset();
  }

  return true;
}