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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:26:17+00:00 2026-05-26T14:26:17+00:00

I was wondering if I could get some guidance to solving my problem… You

  • 0

I was wondering if I could get some guidance to solving my problem…

You see I’m trying to get my hands on Socket programming; I managed to create a client and server; the server writes to the client no problem;
I’ve even manage to send files using

byte [] mybytearray  = new byte [(int)myFile.length()];
  FileInputStream fis = new FileInputStream(myFile);

That on the server side.
On the client side

byte [] mybytearray  = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fileos = new FileOutputStream("Filename.dat");
BufferedOutputStream bufferos = new BufferedOutputStream(fileos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;


do {
   bytesRead =
      is.read(mybytearray, current, (mybytearray.length-current));
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bufferos.write(mybytearray, 0 , current);
bufferos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bufferos.close();

My question comes to I’m not able to send large files; I keep on getting
“Exception in thread “main” java.lang.OutOfMemoryError: Java heap space”

Any thoughts or directions to how I can manage sending large files to the client from server? I mean sizes of say 600 MB or so….
any thought?
Highly appreciated … thank you

  • 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-26T14:26:18+00:00Added an answer on May 26, 2026 at 2:26 pm

    Well, your heap is currently having to try to hold ‘mybytearray’, which is the size of the file you’re trying to receive (presumably). You need to move the bos.write() operation to within the loop, and make ‘mybytearray’ a fixed size. Here’s an example of copying one stream to another that doesn’t really care about the size of the data being streamed:

        public static void stream(InputStream in, OutputStream out)
            throws IOException {
        byte[] buf = new byte[1024];
        int bytesRead = 0;
    
        try {
    
            while (-1 != (bytesRead = in.read(buf, 0, buf.length))) {
                out.write(buf, 0, bytesRead);
            }
    
        } catch (IOException e) {
            log.error("Error with streaming op: " + e.getMessage());
            throw (e);
        } finally {
                        try{
               in.close();
               out.flush();
               out.close();
                        } catch (Exception e){}//Ignore
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

net and wondering if I could get some get help with this problem. The
Wondering if I could get some advice and direction on this following requirement: Need
I was wondering if there was somewhere I could get some starter kit /
I was wondering if i could get some help with filtering a select list
I am a new iphone developer and was wondering if I could get some
I was wondering if i could get some help to prevent a duplicate entry
i was wondering if i could get some help for my code. I put
I was wondering if someone could give me some guidance here. I'd like to
I was wondering if I could get some refactoring help to make this query
I was wondering if I could get some advice on increasing the overall efficiency

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.