This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reading & Writing bytes to socket

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
I have a series of large XML files that I would like to transfer "one by one" from my server to the client (in bytes) over sockets.
I would like to use a byte array and also a buffer. As of now, I have the folloing;
SERVER
OutputStream out = socket.getOutputStream();
out.write(bytes);
out.flush();
out.close();
CLIENT
InputStream in = socket.getInputStream();
byte[] bytes = new byte[999999999];
//a huge byte array because each file is very hugeint i = in.read(bytes);
String request = new String(bytes, "UTF-8");
in.close();
After the transfer of one XML file, I get an "OutOfMemeoryException". Could someone give me an optimal solution so that I can transfer large amounts of bytes from the server to the client efficiently. Thanks!
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an array of one gigabyte, if I'm not mistaken. Copying it into a String will add another two gigabytes to that (because Java uses a two-byte character representation). Yes, I'm willing to believe that you run out of memory alright
For a start, you could transfer the length of the XML file before you transfer the file itself (eg using DataInputStream/DataOutputStream), create a StringBuffer of exactly the right size, and then shovel the data into the StringBuffer a few thousand bytes at a time. If you convert a StringBuffer to a String, the backing char[] array will not be copied unless you try to modify the StringBuffer afterwards.
If you can get away with not holding the files in memory in the first place, even better. Write them out to wherever as you get them in.
- Peter
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic