?
Solved

Reading binary data from a socket.

Posted on 2003-02-19
5
Medium Priority
?
222 Views
Last Modified: 2010-03-31
Can someone provide a short code listing of the most efficient way to stream a binary file (say PDF or image file) from a socket?

As simple as it sounds, I have not found any elegant solutions for this simple problem on the web that would be efficient and work well in different environments.

Thanks.
 
0
Comment
Question by:MeSpice107
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7982718
This sort of thing:

class FileReceiver implements Runnable {
  /**
   * A self-running class that handles
   * receiving a file transmission
   */
  Socket socket;
  InputStream in;
  OutputStream out;
  long bytesReceived;

  public FileReceiver(Socket socket) {
    this.socket = socket;
    new Thread(this).start();
  }


  public void run() {
    /**
     * Get an input stream on the socket,
     * and write the file to an output
     * stream
     */
    try {
      in = socket.getInputStream();
      int buf;
      out = new FileOutputStream(getSavePath());
      System.out.println("FileReceiver receiving file:");
      while ((buf = in.read()) > -1) {
        doOutput(buf);
      }

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (in != null) {
        try { in.close(); } catch(IOException e) { e.printStackTrace(); }
      }
      if (out != null) {
        System.out.println("\nFile reception completed (unverified)");
        System.out.println("Listening for next file...");
        try { out.close(); socket.close(); } catch(IOException e) { e.printStackTrace(); }
      }
    }
  }

  public void doOutput(int buf) {
    /**
     * Loose couples the reception from
     * the loop in which it happens
     */
    try {
      out.write(buf);
      bytesReceived++;
      if (bytesReceived % 1024 == 0)
      System.out.print("*");
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 7982727
You'd need to put the function getSavePath, returning a String, in there
0
 

Expert Comment

by:CleanupPing
ID: 9059233
MeSpice107:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9553051
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
ANSWERED by: CEHJ

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question