KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > io > RandomAccessFileOutputStream


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.io;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import org.pdfbox.cos.COSBase;
37 import org.pdfbox.cos.COSObject;
38 import org.pdfbox.cos.COSNumber;
39
40 /**
41  * This will write to a RandomAccessFile in the filesystem and keep track
42  * of the position it is writing to and the length of the stream.
43  *
44  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
45  * @version $Revision: 1.5 $
46  */

47 public class RandomAccessFileOutputStream extends OutputStream JavaDoc
48 {
49     private RandomAccess file;
50     private long position;
51     private long lengthWritten = 0;
52     private COSBase expectedLength = null;
53
54     /**
55      * Constructor to create an output stream that will write to the end of a
56      * random access file.
57      *
58      * @param raf The file to write to.
59      *
60      * @throws IOException If there is a problem accessing the raf.
61      */

62     public RandomAccessFileOutputStream( RandomAccess raf ) throws IOException JavaDoc
63     {
64         file = raf;
65         //first get the position that we will be writing to
66
position = raf.length();
67     }
68
69     /**
70      * This will get the position in the RAF that the stream was written
71      * to.
72      *
73      * @return The position in the raf where the file can be obtained.
74      */

75     public long getPosition()
76     {
77         return position;
78     }
79
80     /**
81      * The number of bytes written to the stream.
82      *
83      * @return The number of bytes read to the stream.
84      */

85     public long getLength()
86     {
87         long length = -1;
88         if( expectedLength instanceof COSNumber )
89         {
90             length = ((COSNumber)expectedLength).intValue();
91         }
92         else if( expectedLength instanceof COSObject &&
93                  ((COSObject)expectedLength).getObject() instanceof COSNumber )
94         {
95             length = ((COSNumber)((COSObject)expectedLength).getObject()).intValue();
96         }
97         if( length == -1 )
98         {
99             length = lengthWritten;
100         }
101         return length;
102     }
103
104     /**
105      * {@inheritDoc}
106      */

107     public void write( byte[] b, int offset, int length ) throws IOException JavaDoc
108     {
109         file.seek( position+lengthWritten );
110         lengthWritten += length;
111         file.write( b, offset, length );
112
113     }
114     /**
115      * {@inheritDoc}
116      */

117     public void write( int b ) throws IOException JavaDoc
118     {
119         file.seek( position+lengthWritten );
120         lengthWritten++;
121         file.write( b );
122     }
123
124     /**
125      * This will get the length that the PDF document specified this stream
126      * should be. This may not match the number of bytes read.
127      *
128      * @return The expected length.
129      */

130     public COSBase getExpectedLength()
131     {
132         return expectedLength;
133     }
134
135     /**
136      * This will set the expected length of this stream.
137      *
138      * @param value The expected value.
139      */

140     public void setExpectedLength(COSBase value)
141     {
142         expectedLength = value;
143     }
144 }
Popular Tags