001/** 002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC 003 * and GZip files. (http://jwat.org/) 004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jwat.arc; 019 020import java.io.BufferedInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023 024import org.jwat.common.ByteCountingPushBackInputStream; 025 026/** 027 * ARC Reader implementation for reading uncompressed files. 028 * 029 * @author lbihanic, selghissassi, nicl 030 */ 031public class ArcReaderUncompressed extends ArcReader { 032 033 /** Buffer size used by <code>PushbackInputStream</code>. */ 034 public static final int PUSHBACK_BUFFER_SIZE = 32; 035 036 /** ARC file <code>ByteCountingPushBackInputStream</code>. */ 037 protected ByteCountingPushBackInputStream in; 038 039 /** Start offset of current or next valid record. */ 040 protected long startOffset = -1; 041 042 /** 043 * This constructor is used to get random access to records. 044 * The records are then accessed using the getNextRecordFrom methods 045 * using a supplied input stream for each record. 046 */ 047 public ArcReaderUncompressed() { 048 init(); 049 } 050 051 /** 052 * Construct reader using the supplied input stream. 053 * This method is primarily for sequential access to records. 054 * @param in ARC file input stream 055 */ 056 public ArcReaderUncompressed(ByteCountingPushBackInputStream in) { 057 if (in == null) { 058 throw new IllegalArgumentException("The inputstream 'in' is null"); 059 } 060 this.in = in; 061 init(); 062 } 063 064 @Override 065 public boolean isCompressed() { 066 return false; 067 } 068 069 @Override 070 public void close() { 071 if (currentRecord != null) { 072 try { 073 currentRecord.close(); 074 } catch (IOException e) { /* ignore */ } 075 currentRecord = null; 076 } 077 if (in != null) { 078 try { 079 in.close(); 080 } catch (IOException e) { /* ignore */ } 081 in = null; 082 } 083 } 084 085 @Override 086 protected void recordClosed() { 087 if (currentRecord != null) { 088 consumed += currentRecord.consumed; 089 } else { 090 throw new IllegalStateException("'currentRecord' is null, this should never happen!"); 091 } 092 } 093 094 @Override 095 public long getStartOffset() { 096 return startOffset; 097 } 098 099 @Override 100 public long getOffset() { 101 if (in != null) { 102 return in.getConsumed(); 103 } else { 104 return consumed; 105 } 106 } 107 108 @Override 109 public long getConsumed() { 110 if (in != null) { 111 return in.getConsumed(); 112 } else { 113 return consumed; 114 } 115 } 116 117 @Override 118 public ArcRecordBase getNextRecord() throws IOException { 119 if (currentRecord != null) { 120 currentRecord.close(); 121 } 122 if (in == null) { 123 throw new IllegalStateException( 124 "This reader has been initialized with an incompatible constructor, 'in' is null"); 125 } 126 currentRecord = ArcRecordBase.parseRecord(in, this); 127 if (currentRecord != null) { 128 startOffset = currentRecord.header.startOffset; 129 } 130 return currentRecord; 131 } 132 133 @Override 134 public ArcRecordBase getNextRecordFrom(InputStream rin, long offset) 135 throws IOException { 136 if (currentRecord != null) { 137 currentRecord.close(); 138 } 139 if (in != null) { 140 throw new IllegalStateException( 141 "This reader has been initialized with an incompatible constructor, 'in' is not null"); 142 } 143 if (rin == null) { 144 throw new IllegalArgumentException( 145 "The inputstream 'rin' is null"); 146 } 147 if (offset < -1) { 148 throw new IllegalArgumentException( 149 "The 'offset' is less than -1: " + offset); 150 } 151 ByteCountingPushBackInputStream pbin = 152 new ByteCountingPushBackInputStream(rin, PUSHBACK_BUFFER_SIZE); 153 currentRecord = ArcRecordBase.parseRecord(pbin, this); 154 if (currentRecord != null) { 155 startOffset = offset; 156 currentRecord.header.startOffset = offset; 157 } 158 return currentRecord; 159 } 160 161 @Override 162 public ArcRecordBase getNextRecordFrom(InputStream rin, long offset, 163 int buffer_size) throws IOException { 164 if (currentRecord != null) { 165 currentRecord.close(); 166 } 167 if (in != null) { 168 throw new IllegalStateException( 169 "This reader has been initialized with an incompatible constructor, 'in' is not null"); 170 } 171 if (rin == null) { 172 throw new IllegalArgumentException( 173 "The inputstream 'rin' is null"); 174 } 175 if (offset < -1) { 176 throw new IllegalArgumentException( 177 "The 'offset' is less than -1: " + offset); 178 } 179 if (buffer_size <= 0) { 180 throw new IllegalArgumentException( 181 "The 'buffer_size' is less than or equal to zero: " 182 + buffer_size); 183 } 184 ByteCountingPushBackInputStream pbin = 185 new ByteCountingPushBackInputStream( 186 new BufferedInputStream(rin, buffer_size), 187 PUSHBACK_BUFFER_SIZE); 188 currentRecord = ArcRecordBase.parseRecord(pbin, this); 189 if (currentRecord != null) { 190 startOffset = offset; 191 currentRecord.header.startOffset = offset; 192 } 193 return currentRecord; 194 } 195 196}