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.OutputStream;
021
022/**
023 * Factory used for creating <code>WarcWriter</code> instances.
024 * Factory methods are available for creating <code>WarcWriter</code>
025 * instances for writing either compressed or uncompressed records.
026 * Use of buffered methods and/or buffering speeds up the writer considerably.
027 *
028 * @author nicl
029 */
030public class WarcWriterFactory {
031
032    /**
033     * Private constructor to enforce factory methods.
034     */
035    protected WarcWriterFactory() {
036    }
037
038    /**
039     * Creates a new unbuffered <code>WarcWriter</code> from an
040     * <code>OutputStream</code>.
041     * Returns a compressing or non compressing writer according to the arguments.
042     * @param out output stream to write to
043     * @param compressed compression switch
044     * @return unbuffered <code>WarcWriter</code>
045     */
046    public static WarcWriter getWriter(OutputStream out, boolean compressed) {
047        if (out == null) {
048            throw new IllegalArgumentException(
049                    "The 'out' parameter is null!");
050        }
051        if (compressed) {
052            return new WarcWriterCompressed(out);
053        } else {
054            return new WarcWriterUncompressed(out);
055        }
056    }
057
058    /**
059     * Creates a new buffered <code>WarcWriter</code> from an
060     * <code>OutputStream</code>.
061     * Returns a compressing or non compressing writer according to the arguments.
062     * @param out output stream to write to
063     * @param buffer_size buffer size to use
064     * @param compressed compression switch
065     * @return buffered <code>WarcWriter</code>
066     */
067    public static WarcWriter getWriter(OutputStream out, int buffer_size, boolean compressed) {
068        if (out == null) {
069            throw new IllegalArgumentException(
070                    "The 'out' parameter is null!");
071        }
072        if (buffer_size <= 0) {
073            throw new IllegalArgumentException(
074                    "The 'buffer_size' parameter is less than or equal to zero!");
075        }
076        if (compressed) {
077            return new WarcWriterCompressed(out, buffer_size);
078        } else {
079            return new WarcWriterUncompressed(out, buffer_size);
080        }
081    }
082
083    /**
084     * Creates a new unbuffered non compressing <code>WarcWriter</code> from an
085     * <code>OutputStream</code>.
086     * @param out output stream to write to
087     * @return unbuffered non compressing <code>WarcWriter</code>
088     */
089    public static WarcWriter getWriterUncompressed(OutputStream out) {
090        if (out == null) {
091            throw new IllegalArgumentException(
092                    "The 'out' parameter is null!");
093        }
094        return new WarcWriterUncompressed(out);
095    }
096
097    /**
098     * Creates a new buffered non compressing <code>WarcWriter</code> from an
099     * <code>OutputStream</code>.
100     * @param out output stream to write to
101     * @param buffer_size buffer size to use
102     * @return buffered non compressing <code>WarcWriter</code>
103     */
104    public static WarcWriter getWriterUncompressed(OutputStream out, int buffer_size) {
105        if (out == null) {
106            throw new IllegalArgumentException(
107                    "The 'out' parameter is null!");
108        }
109        if (buffer_size <= 0) {
110            throw new IllegalArgumentException(
111                    "The 'buffer_size' parameter is less than or equal to zero!");
112        }
113        return new WarcWriterUncompressed(out, buffer_size);
114    }
115
116    /**
117     * Creates a new unbuffered compressing <code>WarcWriter</code> from an
118     * <code>OutputStream</code>.
119     * @param out output stream to write to
120     * @return unbuffered compressing <code>WarcWriter</code>
121     */
122    public static WarcWriter getWriterCompressed(OutputStream out) {
123        if (out == null) {
124            throw new IllegalArgumentException(
125                    "The 'out' parameter is null!");
126        }
127        return new WarcWriterCompressed(out);
128    }
129
130    /**
131     * Creates a new buffered compressing <code>WarcWriter</code> from an
132     * <code>OutputStream</code>.
133     * @param out output stream to write to
134     * @param buffer_size buffer size to use
135     * @return buffered compressing <code>WarcWriter</code>
136     */
137    public static WarcWriter getWriterCompressed(OutputStream out, int buffer_size) {
138        if (out == null) {
139            throw new IllegalArgumentException(
140                    "The 'out' parameter is null!");
141        }
142        if (buffer_size <= 0) {
143            throw new IllegalArgumentException(
144                    "The 'buffer_size' parameter is less than or equal to zero!");
145        }
146        return new WarcWriterCompressed(out, buffer_size);
147    }
148
149}