47 }
48
49 private static volatile Throwable trouble;
50
51 public static void main(String[] args) throws Throwable {
52 if (args.length > 1)
53 throw new Exception("Usage: java Accordion [BYTES]");
54
55 final long bytes = args.length == 0 ? 10001 : Long.parseLong(args[0]);
56 final int bufsize = 1729;
57 final long count = bytes/bufsize;
58 final PipedOutputStream out = new PipedOutputStream();
59 final PipedInputStream in = new PipedInputStream(out);
60 final Random random = new Random();
61 final byte[] data = new byte[1729];
62 for (int i = 0; i < data.length; i++)
63 data[i] = (byte)random.nextInt(255);
64 System.out.println("count="+count);
65
66 Thread compressor = new Thread() { public void run() {
67 try {
68 final GZIPOutputStream s = new GZIPOutputStream(out);
69 for (long i = 0; i < count; i++)
70 s.write(data, 0, data.length);
71 s.close();
72 } catch (Throwable t) { trouble = t; }}};
73
74 Thread uncompressor = new Thread() { public void run() {
75 try {
76 final GZIPInputStream s = new GZIPInputStream(in);
77 final byte[] maybeBytes = new byte[data.length];
78 for (long i = 0; i < count; i++) {
79 readFully(s, maybeBytes);
80 if (! Arrays.equals(data, maybeBytes))
81 throw new Exception("data corruption");
82 }
83 if (s.read(maybeBytes, 0, 1) > 0)
84 throw new Exception("Unexpected NON-EOF");
85 s.close();
86 } catch (Throwable t) { trouble = t; }}};
87
88 compressor.start(); uncompressor.start();
89 compressor.join(); uncompressor.join();
90
91 if (trouble != null)
92 throw trouble;
93 }
94 }
|
47 }
48
49 private static volatile Throwable trouble;
50
51 public static void main(String[] args) throws Throwable {
52 if (args.length > 1)
53 throw new Exception("Usage: java Accordion [BYTES]");
54
55 final long bytes = args.length == 0 ? 10001 : Long.parseLong(args[0]);
56 final int bufsize = 1729;
57 final long count = bytes/bufsize;
58 final PipedOutputStream out = new PipedOutputStream();
59 final PipedInputStream in = new PipedInputStream(out);
60 final Random random = new Random();
61 final byte[] data = new byte[1729];
62 for (int i = 0; i < data.length; i++)
63 data[i] = (byte)random.nextInt(255);
64 System.out.println("count="+count);
65
66 Thread compressor = new Thread() { public void run() {
67 try (GZIPOutputStream s = new GZIPOutputStream(out)) {
68 for (long i = 0; i < count; i++)
69 s.write(data, 0, data.length);
70 } catch (Throwable t) { trouble = t; }}};
71
72 Thread uncompressor = new Thread() { public void run() {
73 try (GZIPInputStream s = new GZIPInputStream(in)) {
74 final byte[] maybeBytes = new byte[data.length];
75 for (long i = 0; i < count; i++) {
76 readFully(s, maybeBytes);
77 if (! Arrays.equals(data, maybeBytes))
78 throw new Exception("data corruption");
79 }
80 if (s.read(maybeBytes, 0, 1) > 0)
81 throw new Exception("Unexpected NON-EOF");
82 } catch (Throwable t) { trouble = t; }}};
83
84 compressor.start(); uncompressor.start();
85 compressor.join(); uncompressor.join();
86
87 if (trouble != null)
88 throw trouble;
89 }
90 }
|