Java: How create a package with bytes?
i'm working in a project where i need to read some values and send through socket connection. This is format of the package that i must create:
i must read these values (i don't know what king of type it must be each value, int or string, etc): type : type of operation (how get 4 bits only ? ) reserverd : i will fill with 0's origin: who is sending the message receiver: who is to receive the message algorithm: which algorithm is goona to be used to encrypt the message padding: use it in the encrypt algorithm mode : which mode to encrypt
i must read this values and create this package that must have 7 bytes only.
how can i do that ?
it must be something like this i think:
byte[] r = new byte[]{
type+reserverd,
origin_1_byte, origin_2_byte,
receiver_1_byte, receiver_2_byte,
algorithm+padding,
mode};
UPDATE:
ByteBuffer buffer = ByteBuffer.allocate(100);
// read data into buffer
buffer.rewind();
buffer.order(ByteOrder.LITTLE_ENDIAN);
// 0xf and co mask off sign extension -> handle byte as unsigned
int type = (buffer.get() >> 4) & 0xf; // 15 in decimal
buffer.rewind();
buffer.put((byte)(type << 4));
Syste开发者_开发百科m.out.println("type = " + type);
output : 0 (why ?)
Any ideas ?
Just use a ByteBuffer and the nio package. That way you can easily read the data from the network and don't have to worry about Endianess (that's important! If you use streams you pretty much have to implement the shifting yourself - although that could be a good exercise). Don't forget to set the buffers endianness to the correct value. We use ints internally because a) all mathematical operations return ints anyway and b) you are able to handle the values as unsigned.
ByteBuffer buffer = ByteBuffer.allocate(100);
// read data into buffer
buffer.rewind();
buffer.order(ByteOrder.LITTLE_ENDIAN);
// 0xf and co mask off sign extension -> handle byte as unsigned
int type = (buffer.get() >> 4) & 0xf;
int origin = buffer.getShort() & 0xffff;
int receiver = buffer.getShort() & 0xffff;
// and so on
to write the data back you pretty much do the reverse:
ByteBuffer buffer = ByteBuffer.allocate(100);
buffer.rewind();
buffer.put((byte)(type << 4));
buffer.putShort((short)origin);
// and so on
Edit: Usually you'd read the data directly into a ByteBuffer from the Network channel and use the Java NIO package. A quick tutorial is here. But since you already have the Socket we'll make it a bit easier. Note that I've ignored all error checking for breviety. Using an OutputStream and WritableByteChannel (with write) works in the other direction.
InputStream is = socket.getInputStream();
ReadableByteChannel source = Channels.newChannel(istream);
ByteBuffer buffer = ByteBuffer.allocateDirect(headerSize);
source.read(buffer);
// now header is in buffer and can be used as seen above.
Look into the ByteBuffer
.
Also look into bit operations, such as "shifting." If you need to store 3 into type
and 7 into reserved
, then you just need to store them into an 8 bit, byte
by shifting.
byte typeReserved = (3 << 4) | 7;
Want to get the value out?
byte type = (typeReserved >> 4) & 0xF;
byte reserved = typeReserved & 0xF;
I had the same kind of problem using bytes, also I discovered that there are no "unsigned bytes" in Java, so the byte values will only go from -128 to 127 if I'm correct. I was constrained to use shorts as if they were bytes so I could use values up to 255.
I'm sure there's a way out though, but at the time I just didn't have enough time to figure it out
you'd use bit shifts (using <<
) and bitwise ors (using |
)
new byte[]{
type<<4|reserverd,
origin_1_byte, origin_2_byte,
receiver_1_byte, receiver_2_byte,
algorithm<<4 | padding,
mode};
to extract them you'll need masks and shifts (buff[0]&0xf0)>>>4
will extract the type for example)
ByteBuffer, as suggested in other answers, or DataOutputStream.
精彩评论