Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 09:56 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
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.

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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket vortex New To Java 2 05-25-2008 08:41 AM
Data compression at 48 Mbyte/s in native Java rlasse Java Announcements 0 03-17-2008 04:33 AM
How to reduce the size or avoiding out of memory error? rajeshkumarmsc Advanced Java 3 08-12-2007 12:15 AM
iteration on huge amount of files in a folder tshaked Advanced Java 1 08-07-2007 09:08 PM
XML through a socket Heather XML 2 07-04-2007 11:31 AM


All times are GMT +3. The time now is 07:04 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org