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.IOException;
033: import java.io.OutputStream;
034:
035: import org.pdfbox.cos.COSBase;
036: import org.pdfbox.cos.COSObject;
037: import org.pdfbox.cos.COSNumber;
038:
039: /**
040: * This will write to a RandomAccessFile in the filesystem and keep track
041: * of the position it is writing to and the length of the stream.
042: *
043: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
044: * @version $Revision: 1.5 $
045: */
046: public class RandomAccessFileOutputStream extends OutputStream {
047: private RandomAccess file;
048: private long position;
049: private long lengthWritten = 0;
050: private COSBase expectedLength = null;
051:
052: /**
053: * Constructor to create an output stream that will write to the end of a
054: * random access file.
055: *
056: * @param raf The file to write to.
057: *
058: * @throws IOException If there is a problem accessing the raf.
059: */
060: public RandomAccessFileOutputStream(RandomAccess raf)
061: throws IOException {
062: file = raf;
063: //first get the position that we will be writing to
064: position = raf.length();
065: }
066:
067: /**
068: * This will get the position in the RAF that the stream was written
069: * to.
070: *
071: * @return The position in the raf where the file can be obtained.
072: */
073: public long getPosition() {
074: return position;
075: }
076:
077: /**
078: * The number of bytes written to the stream.
079: *
080: * @return The number of bytes read to the stream.
081: */
082: public long getLength() {
083: long length = -1;
084: if (expectedLength instanceof COSNumber) {
085: length = ((COSNumber) expectedLength).intValue();
086: } else if (expectedLength instanceof COSObject
087: && ((COSObject) expectedLength).getObject() instanceof COSNumber) {
088: length = ((COSNumber) ((COSObject) expectedLength)
089: .getObject()).intValue();
090: }
091: if (length == -1) {
092: length = lengthWritten;
093: }
094: return length;
095: }
096:
097: /**
098: * {@inheritDoc}
099: */
100: public void write(byte[] b, int offset, int length)
101: throws IOException {
102: file.seek(position + lengthWritten);
103: lengthWritten += length;
104: file.write(b, offset, length);
105:
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: public void write(int b) throws IOException {
112: file.seek(position + lengthWritten);
113: lengthWritten++;
114: file.write(b);
115: }
116:
117: /**
118: * This will get the length that the PDF document specified this stream
119: * should be. This may not match the number of bytes read.
120: *
121: * @return The expected length.
122: */
123: public COSBase getExpectedLength() {
124: return expectedLength;
125: }
126:
127: /**
128: * This will set the expected length of this stream.
129: *
130: * @param value The expected value.
131: */
132: public void setExpectedLength(COSBase value) {
133: expectedLength = value;
134: }
135: }
|