1 /*
2 * Trip Tracker, a real-time position tracking system for the Internet.
3 * Copyright (C) 2006 Team Trip Tracker
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 package triptracker.testing.compress;
21
22 /***
23 * Simple demonstration of the CompressedBlockOutputStream and
24 * CompressedBlockInputStream transmitting data across a socket.
25 *
26 * $Id: Demo.java,v 1.1 2005/10/26 17:40:19 isenhour Exp $
27 */
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.net.Socket;
31 import java.util.Random;
32 import java.util.zip.Deflater;
33
34 public class Demo {
35 private static final int CHUNKS = 512;
36
37 public static void sendData(int port) throws IOException {
38 // Connect to the server
39 Socket s = new Socket("triptracker.dyndns.org", port);
40 OutputStream out = s.getOutputStream();
41
42 // Make a stream that compresses outgoing data,
43 // sending a compressed block for every 1K worth of
44 // uncompressed data written.
45 CompressedBlockOutputStream compressed =
46 new CompressedBlockOutputStream(out, 1024,
47 Deflater.BEST_COMPRESSION, Deflater.FILTERED);
48
49 Random rand = new Random();
50 byte[] random = new byte[CHUNKS * 1024];
51 rand.nextBytes(random);
52
53 // compressed.write(random);
54 out.write(random);
55
56 // Build a writer that wraps the (compressed) socket
57 // output stream
58 // PrintWriter out = new PrintWriter(new OutputStreamWriter(compressed));
59
60 // Send across 10000 lines of output
61 // for (int i = 0; i < 10000; i++) {
62 // out.println("This is line " + (i + 1) + " of the test output.");
63 // }
64
65 // Note that if we don't close the stream, the last
66 // block of data may not be sent across the connection. We
67 // could also force the last block to be sent by calling
68 // flush(), which would leave the socket connection open.
69 out.flush();
70 out.close();
71 }
72
73 public static void main(String[] args) throws IOException {
74 // Pick an obscure default port. An alternate port
75 // can be given as the first command line argument.
76 int port = 11535;
77 if (args.length > 0) {
78 port = Integer.parseInt(args[0]);
79 }
80
81 // new Server(port);
82
83 sendData(port);
84 }
85 }