Jump to content



Photo

WHILE loops and InputStreams

java

  • Please log in to reply
18 replies to this topic

#1 Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 01 December 2012 - 03:16

can some one show me an example of correctly using a ByteArrayInputStream? using a while loop


#2 kjordan2001

kjordan2001

    Mystery Solver

  • Tech Issues Solved: 1
  • Joined: 27-May 02

Posted 01 December 2012 - 08:56

ByteArrayInputStream is = ...;
ByteArrayOutputStream os = ...;
byte[] buf = new byte[4096];
int read;
while ((read=is.read(buf, 0, 4096)) {
   //Do something with contents of buf
   //Like write to a ByteArrayOutputStream
   os.write(buf, 0, read);
}
os.toByteArray();

Of course you can use a third party library such as Apache commons-io and its IOUtils class to read the whole thing without a loop. Larger buffer sizes may be beneficial too, but it may just depend on what you're doing with what you're reading.

#3 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 02 December 2012 - 15:03

ByteArrayInputStream is = ...;
ByteArrayOutputStream os = ...;
byte[] buf = new byte[4096];
int read;
while ((read=is.read(buf, 0, 4096)) {
   //Do something with contents of buf
   //Like write to a ByteArrayOutputStream
   os.write(buf, 0, read);
}
os.toByteArray();

Of course you can use a third party library such as Apache commons-io and its IOUtils class to read the whole thing without a loop. Larger buffer sizes may be beneficial too, but it may just depend on what you're doing with what you're reading.


cheers :p will have to try it .... im just trying to read 256b nothing huge

#4 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 05 December 2012 - 21:54

ByteArrayInputStream is = ...;
ByteArrayOutputStream os = ...;
byte[] buf = new byte[4096];
int read;
while ((read=is.read(buf, 0, 4096)) {
   //Do something with contents of buf
   //Like write to a ByteArrayOutputStream
   os.write(buf, 0, read);
}
os.toByteArray();

Of course you can use a third party library such as Apache commons-io and its IOUtils class to read the whole thing without a loop. Larger buffer sizes may be beneficial too, but it may just depend on what you're doing with what you're reading.


refering back to this .... how do I write information to the new byte xD i have never worked with bytes before xD , obviously i see your write thing ...but I dont understand it so im not going to use it xD do i need to byte up a string and then add it to the new byte

#5 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 05 December 2012 - 23:14

what would i need to do to this ... to send and receive just bytes... the whole point is to send the bytes and then it converted into a string on the other side (this is to sort out a previous issue I have with sending encrypted strings and them not keeping formating )

import java.io.*;
import java.net.*;
public class chatServer
{
public static void main (String[] args) throws IOException
{
  ServerSocket serverSocket = null;

  try
  {
   serverSocket = new ServerSocket (4444);
  }
  catch (IOException e)
  {
   System.err.println ("Could not listen on port: 4444.");
   System.exit (1);
  }

  System.out.println ("Server - Listening on port 4444");
  Socket clientSocket = null;

  try
  {
   clientSocket = serverSocket.accept ();
  }
  catch (IOException e)
  {
   System.err.println ("Accept failed."); System.exit(1);
  }

  PrintWriter out = new PrintWriter (clientSocket.getOutputStream(), true);
  BufferedReader in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
  String toClient, fromClient;
  toClient = "Hello";
  System.out.println ("Server Message: " + toClient);
  out.println (toClient);
  fromClient = in.readLine ();
  System.out.println ("Client Message: " + fromClient);
  out.close ();
  in.close ();
  clientSocket.close ();
  serverSocket.close ();
}
}


#6 kjordan2001

kjordan2001

    Mystery Solver

  • Tech Issues Solved: 1
  • Joined: 27-May 02

Posted 05 December 2012 - 23:33

import java.io.*;
import java.net.*;
public class chatServer
{
public static void main (String[] args) throws IOException
{
  ServerSocket serverSocket = null;

  try
  {
   serverSocket = new ServerSocket (4444);
  }
  catch (IOException e)
  {
   System.err.println ("Could not listen on port: 4444.");
   System.exit (1);
  }

  System.out.println ("Server - Listening on port 4444");
  Socket clientSocket = null;

  try
  {
   clientSocket = serverSocket.accept ();
  }
  catch (IOException e)
  {
   System.err.println ("Accept failed."); System.exit(1);
  }

  PrintWriter out = new PrintWriter (clientSocket.getOutputStream(), true);
  BufferedReader in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
  String toClient, fromClient;
  toClient = "Hello";
  System.out.println ("Server Message: " + toClient);
  out.println (toClient);
CharArrayWriter os = new CharArrayWriter();
char[] buf = new char[4096];
int read;
while ((read=in.read(buf, 0, 4096)) {
   os.write(buf, 0, read);
}
fromClient = os.toString();
  System.out.println ("Client Message: " + fromClient);
  out.close ();
  in.close ();
  clientSocket.close ();
  serverSocket.close ();
}
}

Since you're just passing strings and using a BufferedReader which is character specific, I've change it to a CharArrayWriter which is the equivalent of a ByteArrayOutputStream.

You may also want to call flush() on your PrintWriter to send the data to the client immediately if you'll be keeping it open longer than you are now.

#7 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 December 2012 - 00:03

Since you're just passing strings and using a BufferedReader which is character specific, I've change it to a CharArrayWriter which is the equivalent of a ByteArrayOutputStream.

You may also want to call flush() on your PrintWriter to send the data to the client immediately if you'll be keeping it open longer than you are now.


ye it will be open longer I have code from that but I have stripped it down to bare basics just to play around and get to understanding it.... so lets say I wanted to send a massive string .... and recieve it all the other side in the same format as its sent ? what would I do ... I have written a huge encryption chat program and got it working except for the decrypting on the receiving side because when it sends the encrypted string the format never sends the same for some reason and I dunno why .... if I start a private chat with you would you be able to help me ?

I am really stuck REALLY stuck this problem of the encrypted string is just messing with me

#8 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 December 2012 - 00:50

when i send an encrypted string it has a size of 256 .... when I receive it I get it in seval patches between 1-256 bits a time ... I know it works cause by pure chance it sent correct once and decrypted ... but every other time it messes up the spaces, lines, formatting ... only gets a certain amount and starts processing before the rest comes through etc

#9 kjordan2001

kjordan2001

    Mystery Solver

  • Tech Issues Solved: 1
  • Joined: 27-May 02

Posted 06 December 2012 - 03:50

when i send an encrypted string it has a size of 256 .... when I receive it I get it in seval patches between 1-256 bits a time ... I know it works cause by pure chance it sent correct once and decrypted ... but every other time it messes up the spaces, lines, formatting ... only gets a certain amount and starts processing before the rest comes through etc


Are you calling flush() when you're sending? You were also using readline before instead of reading the whole string. Use the modified code I posted and that should read everything in. Not sure what happened to it, but the forum seems to have devoured the greater than 0 on the while loop (i.e. read until there's nothing left). You could also try using UDP which is less reliable on whether you'll know you sent the message fine, but it also sends it all at once. You also may want to develop a message format with a delimiter string and possibly a header that would let your receiver know the size of the message it should expect.

#10 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 December 2012 - 12:26

Are you calling flush() when you're sending? You were also using readline before instead of reading the whole string. Use the modified code I posted and that should read everything in. Not sure what happened to it, but the forum seems to have devoured the greater than 0 on the while loop (i.e. read until there's nothing left). You could also try using UDP which is less reliable on whether you'll know you sent the message fine, but it also sends it all at once. You also may want to develop a message format with a delimiter string and possibly a header that would let your receiver know the size of the message it should expect.


ok i shall give that a go... but with your code it keeps telling me im trying to convert an int to a booleen ....obviously its something to do with the equals ... but ye it seems to dislike me ...

#11 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 December 2012 - 13:27

also i copied and pasted your code in ? but im assume as you said it removed the another while statement you had previously.

I fixed the code, now just trying to get it to actually read the input...

import java.io.*;
import java.net.*;
public class chatServer
{
public static void main (String[] args) throws IOException
{
  ServerSocket serverSocket = null;
  try
  {
   serverSocket = new ServerSocket (4444);
  }
  catch (IOException e)
  {
   System.err.println ("Could not listen on port: 4444.");
   System.exit (1);
  }
  System.out.println ("Server - Listening on port 4444");
  Socket clientSocket = null;
  try
  {
   clientSocket = serverSocket.accept ();
  }
  catch (IOException e)
  {
   System.err.println ("Accept failed."); System.exit(1);
  }
  PrintWriter out = new PrintWriter (clientSocket.getOutputStream(), true);
  BufferedReader in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
  String toClient, fromClient;
  toClient = "Hello";
  System.out.println ("Server Message: " + toClient);
  out.println (toClient);
CharArrayWriter os = new CharArrayWriter();
char[] buf = new char[4096];
int read;
while ((read=in.read(buf, 0, 4096))==0) {
System.out.println("x");
   os.write(buf, 0, read);

}
fromClient = os.toString();
  System.out.println ("Client Message: " + fromClient);
  out.close ();
  in.close ();
  clientSocket.close ();
  serverSocket.close ();
}
}


#12 kjordan2001

kjordan2001

    Mystery Solver

  • Tech Issues Solved: 1
  • Joined: 27-May 02

Posted 06 December 2012 - 14:39

It should be while read greater than 0. For some reason Neowin is taking out the >.

#13 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 December 2012 - 15:24

It should be while read greater than 0. For some reason Neowin is taking out the >.




while ((read > in.read(buf, 0, 4096))==0) {
is that what you mean ? sorry I am not following what you are trying to say (i know what you are saying but I dont see where i have to put the > ... :p )

#14 kjordan2001

kjordan2001

    Mystery Solver

  • Tech Issues Solved: 1
  • Joined: 27-May 02

Posted 06 December 2012 - 16:19

while ((read=in.read(buf, 0, 4096)) > 0) {

#15 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 07 December 2012 - 03:24

while ((read=in.read(buf, 0, 4096)) > 0) {


it works :D if I can get my final program working cause of you....then I want to have your babies ... O.o I dont know how but im sure science will help us