1 31 package org.pdfbox.ttf; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.EOFException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 38 import org.pdfbox.cos.COSStream; 39 40 46 public class MemoryTTFDataStream extends TTFDataStream 47 { 48 private byte[] data = null; 49 private int currentPosition = 0; 50 51 56 public MemoryTTFDataStream( InputStream is ) throws IOException 57 { 58 try 59 { 60 ByteArrayOutputStream output = new ByteArrayOutputStream ( is.available() ); 61 byte[] buffer = new byte[1024]; 62 int amountRead = 0; 63 while( (amountRead = is.read( buffer ) ) != -1 ) 64 { 65 output.write( buffer, 0, amountRead ); 66 } 67 data = output.toByteArray(); 68 } 69 finally 70 { 71 if( is != null ) 72 { 73 is.close(); 74 } 75 } 76 } 77 78 79 80 85 public long readLong() throws IOException 86 { 87 return ((long)(readSignedInt()) << 32) + (readSignedInt() & 0xFFFFFFFFL); 88 } 89 90 96 public int readSignedInt() throws IOException 97 { 98 int ch1 = read(); 99 int ch2 = read(); 100 int ch3 = read(); 101 int ch4 = read(); 102 if( (ch1 | ch2 | ch3 | ch4) < 0) 103 { 104 throw new EOFException (); 105 } 106 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 107 } 108 109 114 public int read() throws IOException 115 { 116 int retval = -1; 117 if( currentPosition < data.length ) 118 { 119 retval = data[currentPosition]; 120 } 121 currentPosition++; 122 return retval; 123 } 124 125 131 public int readUnsignedShort() throws IOException 132 { 133 int ch1 = this.read(); 134 int ch2 = this.read(); 135 if ((ch1 | ch2) < 0) 136 { 137 throw new EOFException (); 138 } 139 return (ch1 << 8) + (ch2 << 0); 140 } 141 142 148 public short readSignedShort() throws IOException 149 { 150 int ch1 = this.read(); 151 int ch2 = this.read(); 152 if ((ch1 | ch2) < 0) 153 { 154 throw new EOFException (); 155 } 156 return (short)((ch1 << 8) + (ch2 << 0)); 157 } 158 159 164 public void close() throws IOException 165 { 166 data = null; 167 } 168 169 175 public void seek(long pos) throws IOException 176 { 177 currentPosition = (int)pos; 178 } 179 180 191 public int read(byte[] b, 192 int off, 193 int len) 194 throws IOException 195 { 196 int amountRead = Math.min( len, data.length-currentPosition ); 197 System.arraycopy(data,currentPosition,b, off, amountRead ); 198 currentPosition+=amountRead; 199 200 return amountRead; 201 } 202 203 208 public long getCurrentPosition() throws IOException 209 { 210 return currentPosition; 211 } 212 213 225 public COSStream getCOSStream() 226 { 227 throw new UnsupportedOperationException ( "not yet implemented" ); 228 } 229 230 } | Popular Tags |