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 28 public class PdfInputFile 29 implements PdfInput { 30 31 34 protected FileChannel _fileChannel; 35 36 39 protected long _length; 40 41 44 protected String _name; 45 46 49 protected RandomAccessFile _randomAccessFile; 50 51 60 public PdfInputFile(File pdfFile) throws IOException { 61 62 _randomAccessFile = new RandomAccessFile(pdfFile, "r"); 63 _fileChannel = _randomAccessFile.getChannel(); 64 _length = _randomAccessFile.length(); 65 _name = pdfFile.getPath(); 66 67 } 68 69 74 public void close() throws IOException { 75 synchronized (this) { 76 if (_fileChannel != null) { 77 _fileChannel.close(); 78 _fileChannel = null; 79 } 80 if (_randomAccessFile != null) { 81 _randomAccessFile.close(); 82 _randomAccessFile = null; 83 } 84 } 85 } 86 87 public long getLength() { 88 synchronized (this) { 89 90 return _length; 91 92 } 93 } 94 95 public ByteBuffer readBytes(long start, long end) throws IOException { 96 synchronized (this) { 97 98 ByteBuffer bbuf = ByteBuffer.allocateDirect((int)(end - start)); 99 _fileChannel.read(bbuf, start); 100 bbuf.position(0); 101 return bbuf; 102 103 } 104 } 105 106 public CharBuffer readChars(long start, long end) throws IOException { 107 synchronized (this) { 108 109 110 try { 111 return Charset.forName("ISO-8859-1").newDecoder().decode( 112 readBytes(start, end) ); 113 } 115 catch (CharacterCodingException e) { 116 e.printStackTrace(); 117 Runtime.getRuntime().exit(-1); 118 } 119 120 return null; 122 } 123 } 124 125 public String getName() { 126 synchronized (this) { 127 128 return _name; 129 130 } 131 } 132 133 } 134 | Popular Tags |