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 5971311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:29:57+00:00 2026-05-22T20:29:57+00:00

I´m having a problem, in my server, after I send a file with X

  • 0

I´m having a problem, in my server, after I send a file with X bytes, I send a string saying this file is over and another file is coming, like

FILE: a SIZE: Y\r\n
send Y bytes
FILE a FINISHED\r\n
FILE b SIZE: Z\r\n
send Z byes
FILE b FINISHED\r\n
FILES FINISHED\r\n

In my client it does not recive properly.
I use readline() to get the command lines after reading Y or Z bytes from the socket.
With one file it works fine, with multiple files it rarely works (yeah, I dont know how it worked once or twice)

Here are some code I use to transfer binary

public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
        long size, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    long curRead = 0;
    long totalRead = 0;
    long sizeToRead = size;
    while(totalRead < sizeToRead)
    {
        if(totalRead + buffer.length <= sizeToRead)
        {
            curRead = is.read(buffer);
        }
        else
        {
            curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
        }
        totalRead = totalRead + curRead;
        fout.write(buffer, 0, (int) curRead);
    }
}





public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    int count = 0;
    while((count = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, count);
    }
}

just for note I could solve replacing readline to this code:

    ByteArrayOutputStream ba = new ByteArrayOutputStream();
    int ch;
    while(true)
    {
        ch = is.read();
        if(ch == -1)
            throw new IOException("Conecção finalizada");
        if(ch == 13)
        {
            ch = is.read();
            if(ch == 10)
                return new String(ba.toByteArray(), "ISO-8859-1");
            else
                ba.write(13);
        }
        ba.write(ch);
    }

PS: “is” is my input stream from socket: socket.getInputStream();

still I dont know if its the best implementation to do, im tryinf to figure out

  • 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-05-22T20:29:58+00:00Added an answer on May 22, 2026 at 8:29 pm

    There’s no readLine() calls in the code here, but to answer your question; Yes, calling BufferedReader.readLine() might very well leave stuff around in its internal buffer. It’s buffering the input.

    If you wrap one of your InputStream in a BufferedReader, you can’t really get much sane behavior if you read from the BufferedReader and then later on read from the InputStream.

    You could read bytes from your InputStream and parse out a text line from that by looking for a pair of \r\n bytes. When you got a line saying “FILE: a SIZE: Y\r\n” , you go on as usual, except the buffer you used to parse lines might contain the first few bytes of your file, so write those bytes out first.

    Or you use the idea of FTP and use one TCP stream for commands and one TCP stream for the actual transfer, reading from the command stream with a BufferedReader.readLine(), and reading the data as you already do with an InputStream.

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

Sidebar

Related Questions

I'm having a problem accessing a class after deploying my website to a server.
We were having a problem with our build server not checking out modifications from
I'm having a problem running a T4 template using TextTransform.exe on my build server.
Recently, I've started having a problem with my SQL Server 2005 client running on
I am having an odd problem with my SQL Server express 2005 DB. It
I am having an minor problem using sql server 2008. When I go for
We are using CruiseControl with a StarTeam server and having problems with the StarTeam
I'm having some problems with my WCF server and client connections. If I use
I'm having some problems with the ranking used by fulltext search in SQL Server.
We're having problem with a huge number of legacy stored procedures at work. Do

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.