Byte Class in Java

imported_nautique

Senior member
Jul 14, 2004
346
0
0
I am trying to create a byte object in Java. I do not understand the byte constructor.
This is what the parameters are:

Byte(byte value)
Constructs a newly allocated Byte object that represents the specified byte value.

I do not understand what "byte value" is to be. I thought it was going to be a number or value of how many bytes you want in the array.

I am reading in bytes from a server that are coming to my proxy and then I am passing them on to the browser and I am using the DataInputStream to read the bytes that are coming to me from the server itself. It has a method .read(byte) so I am trying to create an array of bytes.

Any suggestions?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
a Byte object is not an array. It's an Object encapsulating a byte primitive (note that capitaization is important - a "Byte" is an Object and a "byte" is a primitive).

So you have a byte, which contains some 8 bit value, like the number 127 (or some other number) and you need an Object (for instance, to store bytes in a Vector, they need to be objects).

So, you do this: new Byte(byte); and now you have a Byte object that has the same value (127, or whatever) as your byte primitive, and you can use the object anywhere that an Object is required.
 

znaps

Senior member
Jan 15, 2004
414
0
0
That's single Byte object. So new Byte(byte) is a new Byte object wrapping a byte primitive - you want a byte[] or Byte[].
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Well what I did was byte byte_data[] = new byte[80];
int num_bytes_stored;

while((num_bytes_stored = inToProxyFromServer.read(byte_data)) != -1)
{
outToClient.write(num_bytes_stored);
}
but all that does is output PPPPPPPPPPPPP PPPPPPPPPPPPP and so on for the entire page.

I think i am passing the wrong value back to the browser? Am I?
 

znaps

Senior member
Jan 15, 2004
414
0
0
It's printing "P'" because what you're doing is repeatedly sending the number of bytes which were read back to the client, not the actual bytes themselves :)
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
Originally posted by: znaps
It's printing "P'" because what you're doing is repeatedly sending the number of bytes which were read back to the client, not the actual bytes themselves :)

Why would that result in "P" being printed though?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
well now I created the byte[] array, and used the .write(byte[] data, 0, num_byte_stored); it is sending the information but still not all of the pictures are being dispalyed at once. Most but not all. The connection stays open as well though. Any ideas?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
This is what I have come up with and I think it should work but its not. If anyone can suggest anything else I would appreciate it.

byte byte_data[] = new byte[80];
int num_bytes_stored;
while((num_bytes_stored = inToProxyFromServer.read(byte_data)) != -1)
{
outToclient.write(byte_data, 0, num_bytes_stored); // outToClient is a DataOutputStream
}

Please help...this is getting to me!!!!!!
 

Ryland

Platinum Member
Aug 9, 2001
2,810
13
81
Originally posted by: jman19
Originally posted by: znaps
It's printing "P'" because what you're doing is repeatedly sending the number of bytes which were read back to the client, not the actual bytes themselves :)

Why would that result in "P" being printed though?

It would print 'P' because 'P' is the ascii representation of decimal 80 which is the width that he made it.
 

Ryland

Platinum Member
Aug 9, 2001
2,810
13
81
Originally posted by: nautique
This is what I have come up with and I think it should work but its not. If anyone can suggest anything else I would appreciate it.

byte byte_data[] = new byte[80];
int num_bytes_stored;
while((num_bytes_stored = inToProxyFromServer.read(byte_data)) != -1)
{
outToclient.write(byte_data, 0, num_bytes_stored); // outToClient is a DataOutputStream
}

Please help...this is getting to me!!!!!!

How is it not working? What are you seeing?

One thing to note is that you whle loop is going to run forever because the .read is going to read in 80 bytes, every time because thats what you told it to do.

 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
ok it is working for all google sites and links (except some pictures do not display). There are no more P's. Not sure why though.
Now though...sometimes the connection doesn't stay open. It will run fine for a while and then it will give me an error:

socket write error and give the line of
outToclient.write(byte_data, 0, num_bytes_stored);

So I think i see what you are saying that I am always in that while loop. But I thought that once the page was done sending it would return a -1 and the while loop would break. Am I not understand that correctly and is that why sometimes my proxy is crashing?
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
Originally posted by: Ryland
Originally posted by: jman19
Originally posted by: znaps
It's printing "P'" because what you're doing is repeatedly sending the number of bytes which were read back to the client, not the actual bytes themselves :)

Why would that result in "P" being printed though?

It would print 'P' because 'P' is the ascii representation of decimal 80 which is the width that he made it.

Ah, thank you :)