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.warc; 019 020import java.io.BufferedOutputStream; 021import java.io.IOException; 022import java.io.OutputStream; 023 024/** 025 * WARC Writer implementation for writing uncompressed files. 026 * Use WarcWriterFactory to get an instance of this class. 027 * 028 * @author nicl 029 */ 030public class WarcWriterUncompressed extends WarcWriter { 031 032 /** 033 * Construct an unbuffered WARC writer used to write uncompressed records. 034 * @param out outputstream to write to 035 */ 036 WarcWriterUncompressed(OutputStream out) { 037 if (out == null) { 038 throw new IllegalArgumentException( 039 "The 'out' parameter is null!"); 040 } 041 this.out = out; 042 init(); 043 } 044 045 /** 046 * Construct a buffered WARC writer used to write uncompressed records. 047 * @param out outputstream to stream to 048 * @param buffer_size outputstream buffer size 049 * @throws IllegalArgumentException if out is null or buffer_size <= 0 050 */ 051 WarcWriterUncompressed(OutputStream out, int buffer_size) { 052 if (out == null) { 053 throw new IllegalArgumentException( 054 "The 'out' parameter is null!"); 055 } 056 if (buffer_size <= 0) { 057 throw new IllegalArgumentException( 058 "The 'buffer_size' parameter is less than or equal to zero!"); 059 } 060 this.out = new BufferedOutputStream(out, buffer_size); 061 init(); 062 } 063 064 @Override 065 public boolean isCompressed() { 066 return false; 067 } 068 069 @Override 070 public void close() throws IOException { 071 if (state == S_HEADER_WRITTEN || state == S_PAYLOAD_WRITTEN) { 072 closeRecord(); 073 } 074 if (out != null) { 075 out.flush(); 076 out.close(); 077 out = null; 078 } 079 } 080 081 @Override 082 public void closeRecord() throws IOException { 083 if (state == S_HEADER_WRITTEN || state == S_PAYLOAD_WRITTEN) { 084 closeRecord_impl(); 085 state = S_RECORD_CLOSED; 086 } else if (state == S_INIT) { 087 throw new IllegalStateException("Please write a record before closing it!"); 088 } 089 } 090 091 /* 092 * state changed to S_HEADER_WRITTEN 093 * Sets the header and headerContentLength fields. 094 * payloadWrittenTotal is set to 0 095 * @see org.jwat.warc.WarcWriter#writeHeader(org.jwat.warc.WarcRecord) 096 */ 097 @Override 098 public byte[] writeHeader(WarcRecord record) throws IOException { 099 if (record == null) { 100 throw new IllegalArgumentException( 101 "The 'record' parameter is null!"); 102 } 103 if (state == S_HEADER_WRITTEN) { 104 throw new IllegalStateException("Headers written back to back!"); 105 } else if (state == S_PAYLOAD_WRITTEN) { 106 closeRecord_impl(); 107 } 108 return writeHeader_impl(record); 109 } 110 111}