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.archive; 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.io.RandomAccessFile; 023 024import org.jwat.arc.ArcRecordBase; 025import org.jwat.arc.ArcWriter; 026import org.jwat.arc.ArcWriterFactory; 027import org.jwat.common.RandomAccessFileOutputStream; 028import org.jwat.warc.WarcRecord; 029import org.jwat.warc.WarcWriter; 030import org.jwat.warc.WarcWriterFactory; 031 032/** 033 * Simple class used to clone every record read from a (W)ARC file. 034 * Used for debugging purposes. 035 * 036 * @author nicl 037 */ 038public class Cloner { 039 040 /** Buffer size used when writing (W)ARC data. */ 041 public static final int DEFAULT_WRITER_BUFFER_SIZE = 8192; 042 043 private static Cloner cloner; 044 045 public static synchronized Cloner getCloner() { 046 if (cloner == null) { 047 cloner = new Cloner(); 048 } 049 return cloner; 050 } 051 052 private Cloner() { 053 } 054 055 private RandomAccessFile arcRaf; 056 private RandomAccessFileOutputStream arcRafOut; 057 private ArcWriter arcWriter; 058 059 private RandomAccessFile warcRaf; 060 private RandomAccessFileOutputStream warcRafOut; 061 private WarcWriter warcWriter; 062 063 public synchronized void cloneArcRecord(ArcRecordBase record, ManagedPayload managedPayload) throws IOException { 064 if (arcWriter == null) { 065 arcRaf = new RandomAccessFile("erroneous.arc", "rw"); 066 arcRaf.seek(0); 067 arcRaf.setLength(0); 068 arcRafOut = new RandomAccessFileOutputStream(arcRaf); 069 arcWriter = ArcWriterFactory.getWriter(arcRafOut, DEFAULT_WRITER_BUFFER_SIZE, false); 070 } 071 arcWriter.writeHeader(record); 072 InputStream httpHeaderStream = managedPayload.getHttpHeaderStream(); 073 if (httpHeaderStream != null) { 074 arcWriter.streamPayload(httpHeaderStream); 075 httpHeaderStream.close(); 076 httpHeaderStream = null; 077 } 078 InputStream payloadStream = managedPayload.getPayloadStream(); 079 if (payloadStream != null) { 080 arcWriter.streamPayload(payloadStream); 081 payloadStream.close(); 082 payloadStream = null; 083 } 084 arcWriter.closeRecord(); 085 } 086 087 public synchronized void cloneWarcRecord(WarcRecord record, ManagedPayload managedPayload) throws IOException { 088 if (warcWriter == null) { 089 warcRaf = new RandomAccessFile("erroneous.warc", "rw"); 090 warcRaf.seek(0); 091 warcRaf.setLength(0); 092 warcRafOut = new RandomAccessFileOutputStream(warcRaf); 093 warcWriter = WarcWriterFactory.getWriter(warcRafOut, DEFAULT_WRITER_BUFFER_SIZE, false); 094 } 095 warcWriter.writeHeader(record); 096 InputStream httpHeaderStream = managedPayload.getHttpHeaderStream(); 097 if (httpHeaderStream != null) { 098 warcWriter.streamPayload(httpHeaderStream); 099 httpHeaderStream.close(); 100 httpHeaderStream = null; 101 } 102 InputStream payloadStream = managedPayload.getPayloadStream(); 103 if (payloadStream != null) { 104 warcWriter.streamPayload(payloadStream); 105 payloadStream.close(); 106 payloadStream = null; 107 } 108 warcWriter.closeRecord(); 109 } 110 111 public void close() throws IOException { 112 if (arcWriter != null) { 113 arcWriter.close(); 114 arcWriter = null; 115 } 116 if (warcWriter != null) { 117 warcWriter.close(); 118 warcWriter = null; 119 } 120 if (arcRafOut != null) { 121 arcRafOut.close(); 122 arcRafOut = null; 123 } 124 if (warcRafOut != null) { 125 warcRafOut.close(); 126 warcRafOut = null; 127 } 128 if (arcRaf != null) { 129 arcRaf.close(); 130 arcRaf = null; 131 } 132 if (warcRaf != null) { 133 warcRaf.close(); 134 warcRaf = null; 135 } 136 } 137 138}