1 4 5 package com.etymon.pjx; 6 7 import java.io.*; 8 import java.nio.*; 9 import java.nio.channels.*; 10 import java.nio.charset.*; 11 12 37 public class PdfInputBuffer 38 implements PdfInput { 39 40 43 protected ByteBuffer _bbuf; 44 45 48 protected CharBuffer _cbuf; 49 50 53 protected FileChannel _fileChannel; 54 55 58 protected FileInputStream _fileInputStream; 59 60 63 protected String _name; 64 65 77 public PdfInputBuffer(ByteBuffer pdfBuffer, String name) throws IOException { 78 79 _bbuf = pdfBuffer.slice(); 80 81 _name = (name != null) ? name : new String (); 82 83 initBuffer(); 84 85 } 86 87 98 public PdfInputBuffer(File pdfFile) throws IOException { 99 100 initFile(pdfFile, false); 101 102 } 103 104 124 public PdfInputBuffer(File pdfFile, boolean memoryMapped) throws IOException { 125 126 initFile(pdfFile, memoryMapped); 127 128 } 129 130 135 public void close() throws IOException { 136 synchronized (this) { 137 if (_fileChannel != null) { 138 _fileChannel.close(); 139 _fileChannel = null; 140 } 141 if (_fileInputStream != null) { 142 _fileInputStream.close(); 143 _fileInputStream = null; 144 } 145 } 146 } 147 148 153 protected void init() { 154 155 _bbuf.position(0); 156 try { 157 _cbuf = Charset.forName("ISO-8859-1").newDecoder().decode(_bbuf); 158 } 160 catch (CharacterCodingException e) { 161 e.printStackTrace(); 162 Runtime.getRuntime().exit(-1); 163 } 164 165 } 166 167 172 protected void initBuffer() { 173 174 _fileInputStream = null; 175 _fileChannel = null; 176 177 init(); 178 179 } 180 181 186 protected void initFile(File pdfFile, boolean memoryMapped) throws IOException { 187 188 _name = pdfFile.getPath(); 189 190 if (memoryMapped) { 191 192 _fileInputStream = new FileInputStream(pdfFile); 193 _fileChannel = _fileInputStream.getChannel(); 194 long fileChannelSize = _fileChannel.size(); 195 if (fileChannelSize > Integer.MAX_VALUE) { 196 _fileChannel.close(); 197 _fileInputStream.close(); 198 throw new IllegalArgumentException ( 199 "Huge files not supported by this class; use PdfInputFile."); 200 } 201 _bbuf = _fileChannel.map(FileChannel.MapMode.READ_ONLY, 202 0, (int)fileChannelSize); 203 init(); 204 205 } else { 206 207 FileInputStream fis = new FileInputStream(pdfFile); 208 FileChannel fc = fis.getChannel(); 209 long fcSize = fc.size(); 210 if (fcSize > Integer.MAX_VALUE) { 211 fc.close(); 212 fis.close(); 213 throw new IllegalArgumentException ( 214 "Huge files not supported by this class; use PdfInputFile."); 215 } 216 _bbuf = ByteBuffer.allocateDirect((int)fcSize); 217 fc.read(_bbuf); 218 fc.close(); 219 fis.close(); 220 221 initBuffer(); 222 223 } 224 225 } 226 227 public ByteBuffer readBytes(long start, long end) throws IOException { 228 synchronized (this) { 229 230 _bbuf.limit((int)end); 231 _bbuf.position((int)start); 232 return _bbuf.slice(); 233 234 } 235 } 236 237 public CharBuffer readChars(long start, long end) throws IOException { 238 synchronized (this) { 239 240 _cbuf.limit((int)end); 241 _cbuf.position((int)start); 242 return _cbuf.slice(); 243 244 } 245 } 246 247 public long getLength() { 248 synchronized (this) { 249 250 return _bbuf.capacity(); 251 252 } 253 } 254 255 public String getName() { 256 synchronized (this) { 257 258 return _name; 259 260 } 261 } 262 263 } 264 | Popular Tags |