Results 1 to 1 of 1
-
How to use Java's compression classes to reduce the amount of data sent over a socket
This Java tip shows how to use Java's compression classes to reduce the amount of data sent over a socket.
Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.net.ServerSocket; import java.net.Socket; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class CompRcv { public static void main(String[] args) throws Exception { ServerSocket ssock = new ServerSocket(Integer.parseInt(args[0])); System.out.println("Listening"); Socket sock = ssock.accept(); GZIPInputStream zip = new GZIPInputStream(sock.getInputStream()); while (true) { int c; c = zip.read(); if (c == -1) break; System.out.print((char) c); } } } class CompSend { public static void main(String[] args) throws Exception { Socket sock = new Socket(args[0], Integer.parseInt(args[1])); GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream()); String line; BufferedReader bis = new BufferedReader(new FileReader(args[2])); while (true) { try { line = bis.readLine(); if (line == null) break; line = line + "\n"; zip.write(line.getBytes(), 0, line.length()); } catch (Exception e) { break; } } zip.finish(); zip.close(); sock.close(); } }
Similar Threads
-
Socket
By vortex in forum New To JavaReplies: 2Last Post: 05-25-2008, 06:41 AM -
Data compression at 48 Mbyte/s in native Java
By rlasse in forum Java SoftwareReplies: 0Last Post: 03-17-2008, 02:33 AM -
How to reduce the size or avoiding out of memory error?
By rajeshkumarmsc in forum Advanced JavaReplies: 3Last Post: 08-11-2007, 10:15 PM -
iteration on huge amount of files in a folder
By tshaked in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:08 PM -
XML through a socket
By Heather in forum XMLReplies: 2Last Post: 07-04-2007, 09:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks