View Javadoc

1   /* Copyright (C) 2003 Internet Archive.
2    *
3    * This file is part of the Heritrix web crawler (crawler.archive.org).
4    *
5    * Heritrix is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser Public License as published by
7    * the Free Software Foundation; either version 2.1 of the License, or
8    * any later version.
9    *
10   * Heritrix is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser Public License
16   * along with Heritrix; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * QueueCat.java
20   * Created on Nov 12, 2003
21   *
22   * $Header$
23   */
24  package org.archive.queue;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.EOFException;
28  import java.io.FileInputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.ObjectInputStream;
32  import java.io.SequenceInputStream;
33  
34  import org.archive.util.ArchiveUtils;
35  import org.archive.util.Reporter;
36  
37  /***
38   * Command-line tool that displays serialized object streams in a
39   * line-oriented format.
40   *
41   * @author gojomo
42   */
43  public class QueueCat {
44      public static void main(String[] args) 
45      throws IOException, ClassNotFoundException {
46          InputStream inStream;
47          if (args.length == 0) {
48              inStream = System.in;
49          } else {
50              inStream = new FileInputStream(args[0]);
51          }
52  
53          // Need to handle the case where the stream lacks the usual
54          // objectstream prefix
55          byte[] serialStart = { (byte)0xac, (byte)0xed, (byte)0x00, (byte)0x05 };
56          byte[] actualStart = new byte[4];
57          byte[] pseudoStart;
58          inStream.read(actualStart);
59          if (ArchiveUtils.byteArrayEquals(serialStart,actualStart)) {
60              pseudoStart = serialStart;
61          } else {
62              // Have to fake serialStart and original 4 bytes
63              pseudoStart = new byte[8];
64              System.arraycopy(serialStart,0,pseudoStart,0,4);
65              System.arraycopy(actualStart,0,pseudoStart,4,4);
66          }
67          inStream = new SequenceInputStream(
68              new ByteArrayInputStream(pseudoStart),
69              inStream);
70  
71          ObjectInputStream oin = new ObjectInputStream(inStream);
72  
73          Object o;
74          while(true) {
75              try {
76                  o=oin.readObject();
77              } catch (EOFException e) {
78                  return;
79              }
80              if(o instanceof Reporter) {
81                  System.out.println(((Reporter)o).singleLineReport());
82              } else {
83                  // TODO: flatten multiple-line strings!
84                  System.out.println(o.toString());
85              }
86          }
87      }
88  }