1 31 package org.pdfbox.io; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 36 import org.pdfbox.cos.COSBase; 37 import org.pdfbox.cos.COSObject; 38 import org.pdfbox.cos.COSNumber; 39 40 47 public class RandomAccessFileOutputStream extends OutputStream 48 { 49 private RandomAccess file; 50 private long position; 51 private long lengthWritten = 0; 52 private COSBase expectedLength = null; 53 54 62 public RandomAccessFileOutputStream( RandomAccess raf ) throws IOException 63 { 64 file = raf; 65 position = raf.length(); 67 } 68 69 75 public long getPosition() 76 { 77 return position; 78 } 79 80 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 107 public void write( byte[] b, int offset, int length ) throws IOException 108 { 109 file.seek( position+lengthWritten ); 110 lengthWritten += length; 111 file.write( b, offset, length ); 112 113 } 114 117 public void write( int b ) throws IOException 118 { 119 file.seek( position+lengthWritten ); 120 lengthWritten++; 121 file.write( b ); 122 } 123 124 130 public COSBase getExpectedLength() 131 { 132 return expectedLength; 133 } 134 135 140 public void setExpectedLength(COSBase value) 141 { 142 expectedLength = value; 143 } 144 } | Popular Tags |