• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Byte Class in Java

imported_nautique

Senior member
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?
 
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.
 
That's single Byte object. So new Byte(byte) is a new Byte object wrapping a byte primitive - you want a byte[] or Byte[].
 
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?
 
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 🙂
 
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?
 
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?
 
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!!!!!!
 
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.
 
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.

 
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?
 
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 🙂
 
Back
Top