Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * @(#)ImageInputStreamImpl2.java 
  * 
  * Copyright (c) 2011 Werner Randelshofer, Goldau, Switzerland.
  * All rights reserved.
  * 
  * You may not use, copy or modify this file, except in compliance with the
  * license agreement you entered into with Werner Randelshofer.
  * For details see accompanying license terms.
 */
package org.monte.media.io;
ImageInputStreamImpl2 fixes bugs in ImageInputStreamImpl.

ImageInputStreamImpl uses read(byte[]) instead of readFully(byte[]) inside of readShort. This results in corrupt data input if the underlying stream can not fulfill the read operation in a single step.

Author(s):
Werner Randelshofer
Version:
$Id: ImageInputStreamImpl2.java 299 2013-01-03 07:40:18Z werner $
public abstract class ImageInputStreamImpl2 extends ImageInputStreamImpl {
    // Length of the buffer used for readFully(type[], int, int)
    private static final int BYTE_BUF_LENGTH = 8192;
    
Byte buffer used for readFully(type[], int, int). Note that this array is also used for bulk reads in readShort(), readInt(), etc, so it should be large enough to hold a primitive value (i.e. >= 8 bytes). Also note that this array is package protected, so that it can be used by ImageOutputStreamImpl in a similar manner.
    byte[] byteBuf = new byte[];
    @Override
        public short readShort() throws IOException {
        readFully(, 0, 2);
        if ( == .) {
            return (short)
                ((([0] & 0xff) << 8) | (([1] & 0xff) << 0));
        } else {
            return (short)
                ((([1] & 0xff) << 8) | (([0] & 0xff) << 0));
        }
    }
    public int readInt() throws IOException {
        readFully(, 0, 4);
        if ( == .) {
            return
                ((([0] & 0xff) << 24) | (([1] & 0xff) << 16) |
                 (([2] & 0xff) <<  8) | (([3] & 0xff) <<  0));
        } else {
            return
                ((([3] & 0xff) << 24) | (([2] & 0xff) << 16) |
                 (([1] & 0xff) <<  8) | (([0] & 0xff) <<  0));
        }
    }
New to GrepCode? Check out our FAQ X