01: /* Copyright (C) 2003 Internet Archive.
02: *
03: * This file is part of the Heritrix web crawler (crawler.archive.org).
04: *
05: * Heritrix is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU Lesser Public License as published by
07: * the Free Software Foundation; either version 2.1 of the License, or
08: * any later version.
09: *
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) throws IOException,
45: 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,
56: (byte) 0x05 };
57: byte[] actualStart = new byte[4];
58: byte[] pseudoStart;
59: inStream.read(actualStart);
60: if (ArchiveUtils.byteArrayEquals(serialStart, actualStart)) {
61: pseudoStart = serialStart;
62: } else {
63: // Have to fake serialStart and original 4 bytes
64: pseudoStart = new byte[8];
65: System.arraycopy(serialStart, 0, pseudoStart, 0, 4);
66: System.arraycopy(actualStart, 0, pseudoStart, 4, 4);
67: }
68: inStream = new SequenceInputStream(new ByteArrayInputStream(
69: pseudoStart), 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: }
|