Java Code Examples for com.lowagie.text.pdf.RandomAccessFileOrArray#readUnsignedShort()

The following examples show how to use com.lowagie.text.pdf.RandomAccessFileOrArray#readUnsignedShort() . 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: TIFFDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory from a SeekableStream. The directory parameter specifies which
 * directory to read from the linked list present in the stream; directory 0 is normally read
 * but it is possible to store multiple images in a single TIFF file by maintaining multiple
 * directories.
 *
 * @param stream a SeekableStream to read from.
 * @param directory the index of the directory to read.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, int directory) throws IOException {

	long global_save_offset = stream.getFilePointer();
	long ifd_offset;

	// Read the TIFF header
	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	isBigEndian = endian == 0x4d4d;

	int magic = readUnsignedShort(stream);
	if (magic != 42) {
		throw new IllegalArgumentException("Bad magic number, should be 42.");
	}

	// Get the initial ifd offset as an unsigned int (using a long)
	ifd_offset = readUnsignedInt(stream);

	for (int i = 0; i < directory; i++) {
		if (ifd_offset == 0L) {
			throw new IllegalArgumentException("Directory number too large.");
		}

		stream.seek(ifd_offset);
		int entries = readUnsignedShort(stream);
		stream.skip(12 * entries);

		ifd_offset = readUnsignedInt(stream);
	}

	stream.seek(ifd_offset);
	initialize(stream);
	stream.seek(global_save_offset);
}
 
Example 2
Source File: TIFFDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory by reading a SeekableStream. The ifd_offset parameter specifies
 * the stream offset from which to begin reading; this mechanism is sometimes used to store
 * private IFDs within a TIFF file that are not part of the normal sequence of IFDs.
 *
 * @param stream a SeekableStream to read from.
 * @param ifd_offset the long byte offset of the directory.
 * @param directory the index of the directory to read beyond the one at the current stream
 *            offset; zero indicates the IFD at the current offset.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory) throws IOException {

	long global_save_offset = stream.getFilePointer();
	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	isBigEndian = endian == 0x4d4d;

	// Seek to the first IFD.
	stream.seek(ifd_offset);

	// Seek to desired IFD if necessary.
	int dirNum = 0;
	while (dirNum < directory) {
		// Get the number of fields in the current IFD.
		int numEntries = readUnsignedShort(stream);

		// Skip to the next IFD offset value field.
		stream.seek(ifd_offset + 12 * numEntries);

		// Read the offset to the next IFD beyond this one.
		ifd_offset = readUnsignedInt(stream);

		// Seek to the next IFD.
		stream.seek(ifd_offset);

		// Increment the directory.
		dirNum++;
	}

	initialize(stream);
	stream.seek(global_save_offset);
}
 
Example 3
Source File: TIFFDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private int readUnsignedShort(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedShort();
	} else {
		return stream.readUnsignedShortLE();
	}
}
 
Example 4
Source File: TIFFDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static int readUnsignedShort(RandomAccessFileOrArray stream, boolean isBigEndian) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedShort();
	} else {
		return stream.readUnsignedShortLE();
	}
}
 
Example 5
Source File: TIFFDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the number of image directories (subimages) stored in a given TIFF file, represented
 * by a <code>SeekableStream</code>.
 */
public static int getNumDirectories(RandomAccessFileOrArray stream) throws IOException {
	long pointer = stream.getFilePointer(); // Save stream pointer

	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	boolean isBigEndian = endian == 0x4d4d;
	int magic = readUnsignedShort(stream, isBigEndian);
	if (magic != 42) {
		throw new IllegalArgumentException("Bad magic number, should be 42.");
	}

	stream.seek(4L);
	long offset = readUnsignedInt(stream, isBigEndian);

	int numDirectories = 0;
	while (offset != 0L) {
		++numDirectories;

		// EOFException means IFD was probably not properly terminated.
		try {
			stream.seek(offset);
			int entries = readUnsignedShort(stream, isBigEndian);
			stream.skip(12 * entries);
			offset = readUnsignedInt(stream, isBigEndian);
		} catch (EOFException eof) {
			// numDirectories--;
			break;
		}
	}

	stream.seek(pointer); // Reset stream pointer
	return numDirectories;
}
 
Example 6
Source File: TIFFDirectory.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory by reading a SeekableStream.
 * The ifd_offset parameter specifies the stream offset from which
 * to begin reading; this mechanism is sometimes used to store
 * private IFDs within a TIFF file that are not part of the normal
 * sequence of IFDs.
 *
 * @param stream a SeekableStream to read from.
 * @param ifd_offset the long byte offset of the directory.
 * @param directory the index of the directory to read beyond the
 *        one at the current stream offset; zero indicates the IFD
 *        at the current offset.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
throws IOException {
    
    long global_save_offset = stream.getFilePointer();
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    isBigEndian = (endian == 0x4d4d);
    
    // Seek to the first IFD.
    stream.seek(ifd_offset);
    
    // Seek to desired IFD if necessary.
    int dirNum = 0;
    while(dirNum < directory) {
        // Get the number of fields in the current IFD.
        int numEntries = readUnsignedShort(stream);
        
        // Skip to the next IFD offset value field.
        stream.seek(ifd_offset + 12*numEntries);
        
        // Read the offset to the next IFD beyond this one.
        ifd_offset = readUnsignedInt(stream);
        
        // Seek to the next IFD.
        stream.seek(ifd_offset);
        
        // Increment the directory.
        dirNum++;
    }
    
    initialize(stream);
    stream.seek(global_save_offset);
}
 
Example 7
Source File: TIFFDirectory.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int readUnsignedShort(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedShort();
    } else {
        return stream.readUnsignedShortLE();
    }
}
 
Example 8
Source File: TIFFDirectory.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static int readUnsignedShort(RandomAccessFileOrArray stream,
boolean isBigEndian)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedShort();
    } else {
        return stream.readUnsignedShortLE();
    }
}
 
Example 9
Source File: TIFFDirectory.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the number of image directories (subimages) stored in a
 * given TIFF file, represented by a <code>SeekableStream</code>.
 */
public static int getNumDirectories(RandomAccessFileOrArray stream)
throws IOException{
    long pointer = stream.getFilePointer(); // Save stream pointer
    
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    boolean isBigEndian = (endian == 0x4d4d);
    int magic = readUnsignedShort(stream, isBigEndian);
    if (magic != 42) {
        throw new
        IllegalArgumentException("Bad magic number, should be 42.");
    }
    
    stream.seek(4L);
    long offset = readUnsignedInt(stream, isBigEndian);
    
    int numDirectories = 0;
    while (offset != 0L) {
        ++numDirectories;
        
        // EOFException means IFD was probably not properly terminated.
        try {
            stream.seek(offset);
            int entries = readUnsignedShort(stream, isBigEndian);
            stream.skip(12*entries);
            offset = readUnsignedInt(stream, isBigEndian);
        } catch(EOFException eof) {
            //numDirectories--;
            break;
        }
    }
    
    stream.seek(pointer); // Reset stream pointer
    return numDirectories;
}
 
Example 10
Source File: TIFFDirectory.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory by reading a SeekableStream.
 * The ifd_offset parameter specifies the stream offset from which
 * to begin reading; this mechanism is sometimes used to store
 * private IFDs within a TIFF file that are not part of the normal
 * sequence of IFDs.
 *
 * @param stream a SeekableStream to read from.
 * @param ifd_offset the long byte offset of the directory.
 * @param directory the index of the directory to read beyond the
 *        one at the current stream offset; zero indicates the IFD
 *        at the current offset.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
throws IOException {
    
    long global_save_offset = stream.getFilePointer();
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    isBigEndian = (endian == 0x4d4d);
    
    // Seek to the first IFD.
    stream.seek(ifd_offset);
    
    // Seek to desired IFD if necessary.
    int dirNum = 0;
    while(dirNum < directory) {
        // Get the number of fields in the current IFD.
        int numEntries = readUnsignedShort(stream);
        
        // Skip to the next IFD offset value field.
        stream.seek(ifd_offset + 12*numEntries);
        
        // Read the offset to the next IFD beyond this one.
        ifd_offset = readUnsignedInt(stream);
        
        // Seek to the next IFD.
        stream.seek(ifd_offset);
        
        // Increment the directory.
        dirNum++;
    }
    
    initialize(stream);
    stream.seek(global_save_offset);
}
 
Example 11
Source File: TIFFDirectory.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int readUnsignedShort(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedShort();
    } else {
        return stream.readUnsignedShortLE();
    }
}
 
Example 12
Source File: TIFFDirectory.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static int readUnsignedShort(RandomAccessFileOrArray stream,
boolean isBigEndian)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedShort();
    } else {
        return stream.readUnsignedShortLE();
    }
}
 
Example 13
Source File: TIFFDirectory.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the number of image directories (subimages) stored in a
 * given TIFF file, represented by a <code>SeekableStream</code>.
 */
public static int getNumDirectories(RandomAccessFileOrArray stream)
throws IOException{
    long pointer = stream.getFilePointer(); // Save stream pointer
    
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    boolean isBigEndian = (endian == 0x4d4d);
    int magic = readUnsignedShort(stream, isBigEndian);
    if (magic != 42) {
        throw new
        IllegalArgumentException("Bad magic number, should be 42.");
    }
    
    stream.seek(4L);
    long offset = readUnsignedInt(stream, isBigEndian);
    
    int numDirectories = 0;
    while (offset != 0L) {
        ++numDirectories;
        
        // EOFException means IFD was probably not properly terminated.
        try {
            stream.seek(offset);
            int entries = readUnsignedShort(stream, isBigEndian);
            stream.skip(12*entries);
            offset = readUnsignedInt(stream, isBigEndian);
        } catch(EOFException eof) {
            numDirectories--;
            break;
        }
    }
    
    stream.seek(pointer); // Reset stream pointer
    return numDirectories;
}
 
Example 14
Source File: TIFFDirectory.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Constructs a TIFFDirectory from a SeekableStream.
 * The directory parameter specifies which directory to read from
 * the linked list present in the stream; directory 0 is normally
 * read but it is possible to store multiple images in a single
 * TIFF file by maintaining multiple directories.
 *
 * @param stream a SeekableStream to read from.
 * @param directory the index of the directory to read.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
throws IOException {
    
    long global_save_offset = stream.getFilePointer();
    long ifd_offset;
    
    // Read the TIFF header
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    isBigEndian = (endian == 0x4d4d);
    
    int magic = readUnsignedShort(stream);
    if (magic != 42) {
        throw new
        IllegalArgumentException("Bad magic number, should be 42.");
    }
    
    // Get the initial ifd offset as an unsigned int (using a long)
    ifd_offset = readUnsignedInt(stream);
    
    for (int i = 0; i < directory; i++) {
        if (ifd_offset == 0L) {
            throw new
            IllegalArgumentException("Directory number too large.");
        }
        
        stream.seek(ifd_offset);
        int entries = readUnsignedShort(stream);
        stream.skip(12*entries);
        
        ifd_offset = readUnsignedInt(stream);
    }
    
    stream.seek(ifd_offset);
    initialize(stream);
    stream.seek(global_save_offset);
}
 
Example 15
Source File: TIFFDirectory.java    From MesquiteCore with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Constructs a TIFFDirectory from a SeekableStream.
 * The directory parameter specifies which directory to read from
 * the linked list present in the stream; directory 0 is normally
 * read but it is possible to store multiple images in a single
 * TIFF file by maintaing multiple directories.
 *
 * @param stream a SeekableStream to read from.
 * @param directory the index of the directory to read.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
throws IOException {
    
    long global_save_offset = stream.getFilePointer();
    long ifd_offset;
    
    // Read the TIFF header
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    isBigEndian = (endian == 0x4d4d);
    
    int magic = readUnsignedShort(stream);
    if (magic != 42) {
        throw new
        IllegalArgumentException("Bad magic number, should be 42.");
    }
    
    // Get the initial ifd offset as an unsigned int (using a long)
    ifd_offset = readUnsignedInt(stream);
    
    for (int i = 0; i < directory; i++) {
        if (ifd_offset == 0L) {
            throw new
            IllegalArgumentException("Directory number too large.");
        }
        
        stream.seek(ifd_offset);
        int entries = readUnsignedShort(stream);
        stream.skip(12*entries);
        
        ifd_offset = readUnsignedInt(stream);
    }
    
    stream.seek(ifd_offset);
    initialize(stream);
    stream.seek(global_save_offset);
}