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.BufferedInputStream;
021import java.io.File;
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.io.RandomAccessFile;
025
026import org.jwat.arc.ArcReaderFactory;
027import org.jwat.common.ByteCountingPushBackInputStream;
028import org.jwat.common.RandomAccessFileInputStream;
029import org.jwat.gzip.GzipEntry;
030import org.jwat.gzip.GzipReader;
031import org.jwat.warc.WarcReaderFactory;
032
033public final class FileIdent {
034
035        /** Buffer size used to read the input stream. */
036    public static final int DEFAULT_READER_BUFFER_SIZE = 8192;
037
038    /** Buffer size used to peek into the input stream. */
039    public static final int DEFAULT_PUSHBASH_BUFFER_SIZE = 32;
040
041    public static final int FILEID_ERROR = -1;
042    public static final int FILEID_UNKNOWN = 0;
043    public static final int FILEID_GZIP = 1;
044    public static final int FILEID_ARC = 2;
045    public static final int FILEID_WARC = 3;
046    public static final int FILEID_ARC_GZ = 4;
047    public static final int FILEID_WARC_GZ = 5;
048
049    public int filenameId;
050
051    public int streamId;
052
053    public static FileIdent ident(File file) {
054        FileIdent fId = new FileIdent();
055        fId.filenameId = identFileName(file);
056        fId.streamId = identFileStream(file);
057        return fId;
058    }
059
060    public static int identFileName(File file) {
061        int fileId = FILEID_UNKNOWN;
062        String fname = file.getName().toLowerCase();
063        if (fname.endsWith(".arc.gz") || fname.endsWith(".arc.gz.open")) {
064            fileId = FILEID_ARC_GZ;
065        } else if (fname.endsWith(".warc.gz") || fname.endsWith(".warc.gz.open")) {
066            fileId = FILEID_WARC_GZ;
067        } else if (fname.endsWith(".arc") || fname.endsWith(".arc.open")) {
068            fileId = FILEID_ARC;
069        } else if (fname.endsWith(".warc") || fname.endsWith(".warc.open")) {
070            fileId = FILEID_WARC;
071        } else if (fname.endsWith(".gz") || fname.endsWith(".gz.open")) {
072            fileId = FILEID_GZIP;
073        }
074        return fileId;
075    }
076
077    public static int identFileStream(File file) {
078        int fileId = FILEID_UNKNOWN;
079        byte[] magicBytes = new byte[32];
080        int read;
081        RandomAccessFile raf = null;
082        RandomAccessFileInputStream rafin;
083        ByteCountingPushBackInputStream pbin = null;
084        GzipReader gzipReader = null;
085        GzipEntry gzipEntry = null;
086        try {
087            raf = new RandomAccessFile( file, "r" );
088            rafin = new RandomAccessFileInputStream( raf );
089            pbin = new ByteCountingPushBackInputStream(rafin, DEFAULT_PUSHBASH_BUFFER_SIZE);
090            read = pbin.peek(magicBytes);
091            if (read == 32) {
092                if (GzipReader.isGzipped(pbin)) {
093                    gzipReader = new GzipReader( pbin );
094                    ByteCountingPushBackInputStream in;
095                    if ( (gzipEntry = gzipReader.getNextEntry()) != null ) {
096                        in = new ByteCountingPushBackInputStream( new BufferedInputStream( gzipEntry.getInputStream(), DEFAULT_READER_BUFFER_SIZE ), DEFAULT_PUSHBASH_BUFFER_SIZE );
097                        if (ArcReaderFactory.isArcRecord(in)) {
098                            fileId = FILEID_ARC_GZ;
099                        } else if (WarcReaderFactory.isWarcRecord(in)) {
100                            fileId = FILEID_WARC_GZ;
101                        } else {
102                            fileId = FILEID_GZIP;
103                        }
104                    }
105                    gzipReader.close();
106                } else if (ArcReaderFactory.isArcRecord(pbin)) {
107                    fileId = FILEID_ARC;
108                } else if (WarcReaderFactory.isWarcRecord(pbin)) {
109                    fileId = FILEID_WARC;
110                }
111            }
112        } catch (FileNotFoundException e) {
113            fileId = FILEID_ERROR;
114            System.out.println("Error reading: " + file.getPath());
115        } catch (IOException e) {
116            fileId = FILEID_ERROR;
117            e.printStackTrace();
118        }
119        finally {
120            if (pbin != null) {
121                try {
122                    pbin.close();
123                } catch (IOException e) {
124                    e.printStackTrace();
125                }
126            }
127            if (raf != null) {
128                try {
129                    raf.close();
130                } catch (IOException e) {
131                    e.printStackTrace();
132                }
133            }
134        }
135        return fileId;
136    }
137
138}