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

  • SEARCH
  • Home
  • 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 9008007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:50:27+00:00 2026-06-16T01:50:27+00:00

That’s my first question so I hope I write it correctly. I am trying

  • 0

That’s my first question so I hope I write it correctly.

I am trying to send an byte[] array through a Java socket, that array contains an image.

Here is the code to send the file:

public void WriteBytes(FileInputStream dis) throws IOException{
    //bufferEscritura.writeInt(dis.available()); --- readInt() doesnt work correctly
    Write(String.valueOf((int)dis.available()) + "\r\n");
    byte[] buffer = new byte[1024];
    int bytes = 0;
    while((bytes = dis.read(buffer)) != -1){
        Write(buffer, bytes);
    }
    System.out.println("Photo send!");
}
 public void Write(byte[] buffer, int bytes) throws IOException {
    bufferEscritura.write(buffer, 0, bytes);
}
public void Write(String contenido) throws IOException {
    bufferEscritura.writeBytes(contenido);
}

My image:

URL url = this.getClass().getResource("fuegos_artificiales.png");
FileInputStream dis = new FileInputStream(url.getPath());
sockManager.WriteBytes(dis);

My code to get the image file:

public byte[] ReadBytes() throws IOException{
DataInputStream dis = new DataInputStream(mySocket.getInputStream());
int size = Integer.parseInt(Read());
System.out.println("Recived size: "+ size);
byte[] buffer = new byte[size];
System.out.println("We are going to read!");
dis.readFully(buffer);
System.out.println("Photo received!");
return buffer;

}

public String Leer() throws IOException {
    return (bufferLectura.readLine());
}

And to create an image file:

byte[] array = tcpCliente.getSocket().LeerBytes();
FileOutputStream fos = new FileOutputStream("porfavor.png");
try {
    fos.write(array);
}
finally {
    fos.close();
}

The image file is created but when I try to open it for example with Paint it says that it can’t open it because it is damaged…
I also tried to open both images (the original and the new one) with notepad and they have the same data inside!

I don’t know what is happening…

I hope you help me.

Thanks!

  • 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-16T01:50:28+00:00Added an answer on June 16, 2026 at 1:50 am
    1. Don’t use available() as a measure of file length. It isn’t. There is a specific warning in the Javadoc about that.

    2. Use DataOutputStream.writeInt() to write the length, and DataInputStream.readInt() to read it, and use the same streams to read the image data. Don’t use multiple streams on the same socket.

    Also in this:

    URL url = this.getClass().getResource("fuegos_artificiales.png");
    FileInputStream dis = new FileInputStream(url.getPath());
    

    the second line should be:

    InputStream in = URL.openConnection.getInputStream();
    

    A class resource is not a file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That is my first question around here, my name is Anna! My problem: My
That's a simple question: is there an API to draw a B-tree in Java?
That's my Question =) MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery]; NSArray *songsByArtist = [artistQuery collections];
That's pretty much what I am trying to accomplish. To have each image appear
That is my question. I'm just curious what the consensus is on limiting the
That's a really awkward title :) I need to write a report that generates
That's what I wanna achieve : There will be dll which contains only methods
That is the question. Background: C# Params In C#, you can declare the last
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's the best way I could think of to phrase my question, here is

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.