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.common;
019
020import java.io.IOException;
021import java.util.Arrays;
022
023import org.jwat.common.ByteCountingPushBackInputStream;
024import org.jwat.common.Scheme;
025
026/**
027 * Unified archive reader factory base.
028 *
029 * @author nicl
030 */
031public class ReaderFactoryAbstract {
032
033    /** GZip header magic number. */
034    protected static final int GZIP_MAGIC = 0x8b1f;
035
036    /** An ARC version block starts with this string. */
037    protected static final String ARC_MAGIC_HEADER = "filedesc:";
038
039    /**
040     * A WARC header block starts with this string including trailing version
041     * information.
042     * */
043    protected static final String WARC_MAGIC_HEADER = "WARC/";
044
045    /**
046     * Private constructor to enforce factory methods.
047     */
048    protected ReaderFactoryAbstract() {
049    }
050
051    /**
052     * Check head of <code>PushBackInputStream</code> for a GZip magic number.
053     * The state of the <code>PushBackInputStream</code> is the same after the
054     * call as before the call.
055     * @param pbin <code>PushBackInputStream</code> with GZip entries
056     * @return boolean indicating presence of a GZip magic number
057     * @throws IOException if an I/O error occurs while examining head of stream
058     */
059    public static boolean isGzipped(ByteCountingPushBackInputStream pbin) throws IOException {
060        if (pbin == null) {
061            throw new IllegalArgumentException("'pbin'is null!");
062        }
063        byte[] magicBytes = new byte[2];
064        int magicNumber = 0xdeadbeef;
065        // Look for the leading 2 magic bytes in front of every valid GZip entry.
066        int read = pbin.readFully(magicBytes);
067        if (read == 2) {
068            magicNumber = ((magicBytes[1] & 255) << 8) | (magicBytes[0] & 255);
069        }
070        if (read > 0) {
071            pbin.unread(magicBytes, 0, read);
072        }
073        return (magicNumber == GZIP_MAGIC);
074    }
075
076    /**
077     * Check head of <code>PushBackInputStream</code> for an ARC file identifier.
078     * The identifier for ARC files is "filedesc:" in the beginning.
079     * @param pbin <code>PushBackInputStream</code> with an ARC version block
080     * @return boolean indicating presence of an ARC file identifier
081     * @throws IOException if an I/O error occurs while examining head of stream
082     */
083    public static boolean isArcFile(ByteCountingPushBackInputStream pbin) throws IOException {
084        byte[] streamBytes = new byte[ARC_MAGIC_HEADER.length()];
085        byte[] arcBytes = ARC_MAGIC_HEADER.getBytes();
086        // Look for an ARC file identifier in the beginning of the stream.
087        pbin.peek(streamBytes);
088        return (Arrays.equals(arcBytes, streamBytes));
089    }
090
091    /**
092     * Check head of <code>PushBackInputStream</code> for an ARC record identifier.
093     * The identifier for ARC files is "filedesc:" in the beginning.
094     * @param pbin <code>PushBackInputStream</code> with an ARC version block
095     * @return boolean indicating presence of an ARC file identifier
096     * @throws IOException if an I/O error occurs while examining head of stream
097     */
098    public static boolean isArcRecord(ByteCountingPushBackInputStream pbin) throws IOException {
099        byte[] streamBytes = new byte[32];
100        // Look for a valid scheme in the beginning of the stream.
101        pbin.peek(streamBytes);
102        return Scheme.startsWithScheme(streamBytes);
103    }
104
105    /**
106     * Check head of <code>PushBackInputStream</code> for a WARC file identifier.
107     * The identifier for WARC files is "WARC/" in the beginning.
108     * @param pbin <code>PushBackInputStream</code> with WARC records
109     * @return boolean indicating presence of a WARC file identifier
110     * @throws IOException if an I/O error occurs while examining head of stream
111     */
112    public static boolean isWarcFile(ByteCountingPushBackInputStream pbin) throws IOException {
113        return isWarcRecord(pbin);
114    }
115
116    /**
117     * Check head of <code>PushBackInputStream</code> for a WARC record identifier.
118     * The identifier for WARC records is "WARC/" in the beginning.
119     * @param pbin <code>PushBackInputStream</code> with WARC records
120     * @return boolean indicating presence of a WARC magic number
121     * @throws IOException if an I/O error occurs while examining head of stream
122     */
123    public static boolean isWarcRecord(ByteCountingPushBackInputStream pbin) throws IOException {
124        byte[] streamBytes = new byte[WARC_MAGIC_HEADER.length()];
125        byte[] warcBytes = WARC_MAGIC_HEADER.getBytes();
126        // Look for the leading magic bytes in front of every valid WARC record.
127        pbin.peek(streamBytes);
128        return (Arrays.equals(warcBytes, streamBytes));
129    }
130
131}