byteArray = new byte[10000];
-- some code here ----
byteBuffer.wrap(byteArray);
for (int i=0; byteBuffer.hasRemaining(); i++)
{
shortArray[i] = byteBuffer.getShort();
System.out.println(shortArray[i]);
}
The byteBuffer.hasRemaining() gets flagged with a NullPointerException although I have provided it with a backing array.
What is the problem here?
Please, check how you initialize byteBuffer it should be something like this since wrap is a static method
byte[] byteArray = new byte[10000];
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
The code seems OK. I suspect this is (due to some bug) because byteBuffer variable = null
Related
In C#, I use the SerialPort Read function as so:
byte[] buffer = new byte[100000];
int bytesRead = serial.Read(buffer, 0, 100000);
In Processing, I use readBytes as so:
byte[] buffer = new byte[100000];
int bytesRead = serial.readBytes(buffer);
In Processing, I'm getting the incorrect byte values when I loop over the buffer array from the readBytes function, but when I just use the regular read function I get the proper values, but I can't grab the data into a byte array. What am I doing wrong in the Processing version of the code that's leading me to get the wrong values in the buffer array?
I print out the data the same way in both versions:
for(int i=0; i<bytesRead; i++){
println(buffer[i]);
}
C# Correct Output:
Processing Incorrect Output:
Java bytes are signed, so any value over 128 will overflow.
A quick solution is to do
int anUnsignedByte = (int) aSignedByte & 0xff;
to each of your bytes.
I am trying to write a bunch of integers and a string into the byte buffer. Later this byte array will be written to the hard drive. Everything seems to be fine except when I am writing the string in the loop only the last character is written. The parsing of the string appears correct as I have checked that.
It appears to be the way I use the bbuf.put statement. Do I need to flush it after, and why does the .putInt statement work fine and not .put
//write the PCB from memory to file system
private static void _tfs_write_pcb()
{
int c;
byte[] bytes = new byte[11];
//to get the bytes from volume name
try {
bytes = constants.filename.getBytes("UTF-8"); //convert to bytes format to pass to function
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ByteBuffer bbuf = ByteBuffer.allocate(bl_size);
bbuf = bbuf.putInt(rt_dir_start);
bbuf = bbuf.putInt(first_free_data_bl);
bbuf = bbuf.putInt(num_bl_fat);
bbuf = bbuf.putInt(bl_size);
bbuf = bbuf.putInt(max_rt_entries);
bbuf = bbuf.putInt(ft_copies);
for (c=0; c < vl_name.length(); c++) {
System.out.println((char)bytes[c]);
bbuf = bbuf.put(bytes[c]);
}
_tfs_write_block(1, bbuf.array());
}
ByteBuffer has a method for put'ting an array of byte. Is there a reason to put them one at a time? I note that put(byte) is abstract as well.
So the for loop is simplified to:
bbuf = bbuf.put(bytes, 6, bytes.length);
http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#put-byte:A-
EDIT: The Javadoc specifies that put(byte[]) begins at index 0, so use the form put(byte[], index, length) instead.
public final ByteBuffer put(byte[] src)
Relative bulk put method (optional operation).
This method transfers the entire content of the given source byte array
into this buffer. An invocation of this method of the form dst.put(a)
behaves in exactly the same way as the invocation
dst.put(a, 0, a.length)
Of course, it really should not matter HOW you insert the String bytes. I am just suggesting discovery experimentation.
I have a binary file, in my jar, and I want to slurp its contents in binary mode, not into a string of characters. Following this example
private byte[] readBinaryFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int read = input.read(); read >= 0; read = input.read())
output.write(read);
byte[] buffer = output.toByteArray();
input.close ();
output.close();
return buffer;
}
It's pretty trivial, but the calling context is expecting and Object. How do I pass this binary contents back to the caller, but not as a primitive array? I am trying to deliver this binary data as a response to a web service using jaxrs.
As #Jon notes, the caller should be just fine:
byte[] b = new byte[10];
Object o = b;
That works because as he points out a byte[] is an instance of Object.
Don't confuse bytes themselves, which are indeed primitives, with the array. All arrays are objects no matter what they contain.
So the caller should receive his Object and then send it back to his caller as application/octet-stream.
I have a arraylist of Bytes and i am converting them into a byte array.I have used the following method.However it gives me the following error:
E/AndroidRuntime(5228): java.lang.NoClassDefFoundError: com.google.common.primitives.Bytes
ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
byte[] bytes_song_byte;
for(int i=0;i<int_arraylist.size();i++)
{
bytes_song_byte=Bytes.toArray(byteArrayList_song);
}
Looks like Google Guava is not on your class path, also you should remove the for loop from the above code, as that is what the Guava function does for you.
ArrayList<Byte> byteArrayList_song = new ArrayList<Byte>();
byte[] bytes_song_byte = Bytes.toArray(byteArrayList_song);
You can do this conversion without external libs
byte[] bytes_song_byte = new byte[byteArrayList_song.size()];
for (int i = 0; i < byteArrayList_song.size(); i++) {
bytes_song_byte[i] = byteArrayList_song.get(i);
}
note that if byteArrayList_song has any null elements this code will throw a NullPointerException
Try the following
ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
byte[] bytes_song_byte;
bytes_song_byte=byteArrayList_song.toArray(new Byte[byteArrayList_song.size()]);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Creating a byte[] from a List<Byte>
I have List list. How to get byte[] ( subarray of list ) from startIndex to endIndex id list ?
List<Byte> theList= new ArrayList<Byte>();
Byte[] your_bytes = theList.subList(startIndex,endIndex).toArray(new Byte[0]);
If finally you need to work with byte (the primitive) then I recommend Apache Commons Collections toPrimitive utility
byte[] your_primitive_bytes = ArrayUtils.toPrimitive(your_bytes);
For most cases you certainly can get by with Byte (object).
ArrayList<Byte> list = new ArrayList<Byte>();
ArrayList<Byte> subList = (ArrayList<Byte>) list.subList(fromIndex, toIndex); //(0,5)
Byte[] array = (Byte[]) subList.toArray();
Well, since the original question actually asks for a sublist containing a byte[] (not Byte[]) here goes:
List<Byte> byteList = .... some pre-populated list
int start = 5;
int end = 10;
byte[] bytes = new byte[end-start]; // OP explicitly asks for byte[] (unless it's a typo)
for (int i = start; i < end; i++) {
bytes[i-start] = byteList.get(i).byteValue();
}
If you need byte[]:
byte[] byteArray = ArrayUtils.toPrimitive(list.subList(startIndex, endIndex).toArray(new Byte[0]));
ArrayUtils.toPrimitive