001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.io;
031:
032: import java.io.InputStream;
033: import java.io.IOException;
034:
035: /**
036: * A simple subclass that adds a few convience methods.
037: *
038: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
039: * @version $Revision: 1.6 $
040: */
041: public class PushBackInputStream extends java.io.PushbackInputStream {
042:
043: /**
044: * Constructor.
045: *
046: * @param input The input stream.
047: * @param size The size of the push back buffer.
048: *
049: * @throws IOException If there is an error with the stream.
050: */
051: public PushBackInputStream(InputStream input, int size)
052: throws IOException {
053: super (input, size);
054: if (input == null) {
055: throw new IOException("Error: input was null");
056: }
057: }
058:
059: /**
060: * This will peek at the next byte.
061: *
062: * @return The next byte on the stream, leaving it as available to read.
063: *
064: * @throws IOException If there is an error reading the next byte.
065: */
066: public int peek() throws IOException {
067: int result = read();
068: if (result != -1) {
069: unread(result);
070: }
071: return result;
072: }
073:
074: /**
075: * A simple test to see if we are at the end of the stream.
076: *
077: * @return true if we are at the end of the stream.
078: *
079: * @throws IOException If there is an error reading the next byte.
080: */
081: public boolean isEOF() throws IOException {
082: int peek = peek();
083: return peek == -1;
084: }
085:
086: /**
087: * This is a method used to fix PDFBox issue 974661, the PDF parsing code needs
088: * to know if there is at least x amount of data left in the stream, but the available()
089: * method returns how much data will be available without blocking. PDFBox is willing to
090: * block to read the data, so we will first fill the internal buffer.
091: *
092: * @throws IOException If there is an error filling the buffer.
093: */
094: public void fillBuffer() throws IOException {
095: int bufferLength = buf.length;
096: byte[] tmpBuffer = new byte[bufferLength];
097: int amountRead = 0;
098: int totalAmountRead = 0;
099: while (amountRead != -1 && totalAmountRead < bufferLength) {
100: amountRead = this .read(tmpBuffer, totalAmountRead,
101: bufferLength - totalAmountRead);
102: if (amountRead != -1) {
103: totalAmountRead += amountRead;
104: }
105: }
106: this .unread(tmpBuffer, 0, totalAmountRead);
107: }
108: }
|