jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes How can I specify size of a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I specify size of a String?" Watch "How can I specify size of a String?" New topic

How can I specify size of a String?

Sean Hetfield
Ranch Hand

Joined: Dec 01, 2003
Posts: 83
I want to send different Strings through a network with different Sizes, starting from 0 byte to up. How can I define that?
Subhash L. Sriram

Joined: Jan 30, 2005
Posts: 28
Use a String array?

String[] array = new String[size];

pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
how do you want to send them ? if you are sending just the bytes over the wire (e.g. i you do not use java serialization / rmi stuff) and you want to send several strings at once (if you just send one string, then just use all the bytes you receive), then you probably need to send the string length or use a stop sequence:


if you send the length, then you need to know how many bytes you use for the length information (e.g 4 bytes length, then as many bytes an in the length information, then 4 bytes length infor.... and so on and on and on). if you use a stop sequence (e.g. a line break) then you need to make sure, that this sequence can never occur in the string itself.

did that help ?
if not: perhaps you can tell us what you want to achieve and we can give more tips....

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
If you simply use serialization, you don't have to worry about it at all - it will just work automagically.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sean Hetfield
Ranch Hand

Joined: Dec 01, 2003
Posts: 83
I am using this code in both Client and Server to send a message through a socket:

Socket socket = new Socket(address,port);//initiate a socket object

OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);//create a PrintWriter object for output

InputStream sIn = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(sIn);
BufferedReader br = new BufferedReader(isr);//create a BufferReader object for input

pw.println("Client sent a message");
pw.flush(); //Send the message to Server

long sendTime = System.currentTimeMillis();

String inString = br.readLine();

long returnTime = System.currentTimeMillis();

So I need send different String Sizes such as 50, 150, 9000, 128k one at a time and calculate the latency time and draw the chart.
So What I don't know is, because I use BufferedReader.readLine(), how to define String with these sizes in Client at the time of send?

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Since you're trying to measure latency, I would avoid using Strings (and Readers) and stick with byte arrays and Input/OutputStream. By using Strings and Readers, you are also measuring the time it takes to convert the String to a byte array in your calculations, giving you incorrect results.

I would recommend creating a byte array of the maximum size you need (again to avoid including looping time in your calculations) and then use OutputStream.write(buffer, 0, length). You can very length and reuse the same buffer. You might also want to use java.util.Random to fill the byte array just to be super cool.

If both client and server know the lengths to expect, You can avoid sending the length before the "string" of bytes. Otherwise, DataOutputStream will help you as you can send an int and then the byte array. DataInputStream has the readFully(byte[], offset, length) method that could prove useful.

Note also that sending 128k bytes will not be a single packet, so you'll definitely be including a lot of time for writing/reading to/from the socket, but I suppose that's what the assignment asks so it's fine.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Hmmmm. Sockets, RMI, clients and servers, measuring latency ...

This sounds way, way too advanced for beginners. I'm moving this to the Java in General(intermediate) forum.
[ January 31, 2005: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link: http://aspose.com/file-tools
subject: How can I specify size of a String?
It's not a secret anymore!