Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:29:54+00:00 2026-06-07T16:29:54+00:00

I am using the code provided by this accepted answer to send a list

  • 0

I am using the code provided by this accepted answer to send a list of files over a socket in Java. My goal is to be receiving a list of images. What I would like to do is read these images directly into memory as BufferedImages before writing them to disk. However, my first attempts, which was to use ImageIO.read(bis) (again, see the attached question) failed, as it attempted to continue reading beyond the end of the first image file.

My current idea is to write the data from the socket to a new output stream, then read that stream from an intput stream that is passed to ImageIO.read(). This way, I can write it byte by byte as the program is currently doing, but send it to the BufferedImage rather than the file. however I’m not sure how to link the output stream to an input stream.

Can anyone recommend simple edits to the code above, or provide another method of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:29:57+00:00Added an answer on June 7, 2026 at 4:29 pm

    In order to read the image before writing it to disk, you’ll need to use a ByteArrayInputStream. http://docs.oracle.com/javase/6/docs/api/java/io/ByteArrayInputStream.html

    Basically, it creates a inputstream that reads from a specified byte array. So, you’d read the image length, then it’s name, then the length-amount of bytes, create the ByteArrayInputStream, and pass it to ImageIO.read

    Example snippet:

    long fileLength = dis.readLong();
    String fileName = dis.readUTF();
    byte[] bytes = new byte[fileLength];
    dis.readFully(bytes);
    BufferedImage bimage = ImageIO.read(new ByteArrayInputStream(bytes));
    

    Or using the code from the other answer you cited:

    String dirPath = ...;
    
    ServerSocket serverSocket = ...;
    Socket socket = serverSocket.accept();
    
    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream dis = new DataInputStream(bis);
    
    int filesCount = dis.readInt();
    File[] files = new File[filesCount];
    
    for(int i = 0; i < filesCount; i++)
    {
        long fileLength = dis.readLong();
        String fileName = dis.readUTF();
        byte[] bytes = new byte[fileLength];
        dis.readFully(bytes);
        BufferedImage bimage = ImageIO.read(new ByteArrayInputStream(bytes));
    
        //do some shit with your bufferedimage or whatever
    
        files[i] = new File(dirPath + "/" + fileName);
    
        FileOutputStream fos = new FileOutputStream(files[i]);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
    
        bos.write(bytes, 0, fileLength);
    
        bos.close();
    }
    
    dis.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading this google guide and using this sample code provided by google ,
I'm using the code provided below to display time and date. can anyone help
I am using the sample provided here and have used the code inside a
Eclipse provides an opportunity to view java source code by using Source attachement in
I am using Code Igniter, The HMVC library , and Smarty with this library
I'm trying to access Gmail by using three-legged Oauth PHP code provided by Google
I am using the code in this question NSURLConnection download large file (>40MB) to
I'm adding navigation tabs to an action bar using the sample code provided here:
I am using the example from TheElements sample code provided with the iPhone SDK
This is the driver code provided by my instructor, It isn't meant to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.