Represents the commonalities between all sorts of document contents.
| 32 | /** Represents the commonalities between all sorts of document contents. |
| 33 | */ |
| 34 | public class DocumentContentImpl implements DocumentContent |
| 35 | { |
| 36 | /** Buffer size for reading |
| 37 | * 16k is 4 times the block size on most filesystems |
| 38 | * so it should be efficient for most cases |
| 39 | * */ |
| 40 | private static final int INTERNAL_BUFFER_SIZE = 16*1024; |
| 41 | |
| 42 | /** Default construction */ |
| 43 | public DocumentContentImpl() { |
| 44 | content = ""; |
| 45 | } // default construction |
| 46 | |
| 47 | /** Contruction from URL and offsets. */ |
| 48 | public DocumentContentImpl(URL u, String encoding, Long start, Long end) |
| 49 | throws IOException { |
| 50 | |
| 51 | int readLength = 0; |
| 52 | char[] readBuffer = new char[INTERNAL_BUFFER_SIZE]; |
| 53 | |
| 54 | BufferedReader uReader = null; |
| 55 | InputStream uStream = null; |
| 56 | StringBuffer buf = new StringBuffer(); |
| 57 | |
| 58 | long s = 0, e = Long.MAX_VALUE; |
| 59 | if(start != null && end != null) { |
| 60 | s = start.longValue(); |
| 61 | e = end.longValue(); |
| 62 | } |
| 63 | |
| 64 | try { |
| 65 | URLConnection conn = u.openConnection(); |
| 66 | uStream = conn.getInputStream(); |
| 67 | |
| 68 | if ("gzip".equals(conn.getContentEncoding())) { |
| 69 | uStream = new GZIPInputStream(uStream); |
| 70 | } |
| 71 | |
| 72 | if(encoding != null && !encoding.equalsIgnoreCase("")) { |
| 73 | uReader = new BomStrippingInputStreamReader(uStream, encoding, INTERNAL_BUFFER_SIZE); |
| 74 | } else { |
| 75 | uReader = new BomStrippingInputStreamReader(uStream, INTERNAL_BUFFER_SIZE); |
| 76 | }; |
| 77 | |
| 78 | // 1. skip S characters |
| 79 | uReader.skip(s); |
| 80 | |
| 81 | // 2. how many character shall I read? |
| 82 | long toRead = e - s; |
| 83 | |
| 84 | // 3. read gtom source into buffer |
| 85 | while ( |
| 86 | toRead > 0 && |
| 87 | (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1 |
| 88 | ) { |
| 89 | if (toRead < readLength) { |
| 90 | //well, if toRead(long) is less than readLenght(int) |
| 91 | //then there can be no overflow, so the cast is safe |
nothing calls this directly
no outgoing calls
no test coverage detected