Convert IBuffer to Byte Array in Java - java

I have used Xuggler tutorial to decode and play video.
I need to see the raw data for every frame.
I have used the method getData as follows:
IBuffer img = picture.getData();
How to convert IBuffer img to Byte Array in Java?
Thanks
EDIT
I have found the following commands:
java.nio.ByteBuffer buffer = img.getByteBuffer(0, bufSize);//bytebuffer
byte[] bytes = new byte[10]; // Create a byte array
ByteBuffer buf=buffer.wrap(bytes); // Wrap a byte array into a buffer
bytes = new byte[buf.remaining()]; // Retrieve bytes between the position and limit
// (see Putting Bytes into a ByteBuffer)
buf.get(bytes, 0, bytes.length); // transfer bytes from this buffer into the given destination array
buf.clear(); // Retrieve all bytes in the buffer
bytes = new byte[buf.capacity()];
// transfer bytes from this buffer into the given destination array
buf.get(bytes, 0, bytes.length);
System.out.println(bytes);
The Output are:
[B#1ccd51b
[B#215eee
[B#1e579dc
[B#14793a8
[B#1082746
.....
They don't have the same length. Is that correct?
I have used:
System.out.println(Arrays.toString(bytes));
to print out the byte array content, but the output look like this:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
How can I solve this problem?
THANKS

Related

getting the binary data represented by the hexadecimal string back in java vs python

I know that in python binascii.unhexlify(initValue)
return the binary data represented by the hexadecimal string back.
I am trying to convert binascii.unhexlify(initValue) to java.
I tried the following code lines in java but I am getting different results then the code in python:
DatatypeConverter.parseHexBinary(value);
I run the following example:
my input - hexadecimal string:
value = '270000f31d32d1051400000000000000000000000006000000000000000000000000000000000000'
when running in python:
result = binascii.unhexlify(value)
I am getting:
result = "'\x00\x00\xf3\x1d2\xd1\x05\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
when running in java:
byte[] bytes = DatatypeConverter.parseHexBinary(value);
I am getting:
bytes = [39, 0, 0, -13, 29, 50, -47, 5, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1.why I am getting different results?
why do I get the output in python with '\' marks?
The first hex of your result, "'" is exactly 39 in signed char. In python, you can use built-in function ord("'") to get 39.
You can probably get what you want in this python code
value = '270000f31d32d1051400000000000000000000000006000000000000000000000000000000000000'
result = binascii.unhexlify(value)
bytes = [ord(x) for x in result]
You will be getting this unsigned char:
[39, 0, 0, 243, 29, 50, 209, 5, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

writing byte[] to file giving corrupt file

I am getting byte[] in request parameter in servlet which I am fetching in string and then again converting it into byte[] :
String encodingScheme = "UTF-8";
request.setCharacterEncoding(encodingScheme);
String requestStr = request.getParameter("inputstream");
byte[] rawRequestMsg = requestStr.getBytes(encodingScheme);
Now this byte[] I am trying to write to a .docx file as this byte[] which I am using is byte[] representation of a docx file only. Code for writing this to file is like :
String uploadedFileLocation = fileLocation;
FileOutputStream fileOuputStream = new FileOutputStream("path till .docx file");
fileOuputStream.write(byteArray);
fileOuputStream.close();
The problem is the .docx file being created is corrupt and unable to open, but when I change it to .doc then I can open it but instead of seeing the text content I see only the byte[] sequence there like below :
80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33, 0, -84, -122, 80, 87, -114, 1, 0, 0, -64, 5, 0, 0, 19, 0, 8, 2, 91, 67, 111, 110, 116, 101, 110, 116, 95, 84, 121, 112, 101, 115, 93, 46, 120, 109, 108, 32, -94, 4, 2, 40, -96, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dont know how to write that correctly.
Need help.
Thanks,
Samir
Actually the code below used to work which is of a REST webservice
#
POST# Path("/binaryfileupload/{filename}")# Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response upload(byte[] input, #PathParam("filename") String filename) {
FileOutputStream fileOuputStream = new FileOutputStream(uploadedFileLocation);
fileOuputStream.write(input);
fileOuputStream.close();
}
Only change I made is from here this input which is byte[] I am sending to servlet and in servlet want to write file instead of writing in my webservice(which was working correctly).
You are not writing a .doc file. You're just writing a simple text file and naming it as .doc or .docx.
For it to work as a word document file, you need to use a library such as Apache POI to do it for you.
For more info about Apache POI, you can see here: https://poi.apache.org/
You can also refer this link How can I create a simple docx file with Apache POI?
I finally fixed it. I was making a small mistake. In the code
String requestStr = request.getParameter("inputstream");
byte[] rawRequestMsg = requestStr.getBytes(encodingScheme);
I am actually converting the String to byte even though its already in byte. thats why the value of requestStr is different than rawRequestMsg. Finally I used below code which simply takes the string into array and creates byte[] from it by individually separating each number :
String requestStr = request.getParameter("inputstream");
requestStr = requestStr.substring(1, requestStr.length() - 1);
String dataArray[] = requestStr.split(",");
byte[] rawRequestMsg = new byte[dataArray.length];
int count = 0;
for (String str: dataArray) {
str = str.trim();
rawRequestMsg[count++] = Byte.parseByte(str);
}
The trim function is used to remove whitespaces because its coming as 75, -84, 3 .... like this. And the substring is used to remove the [ from the begining and ] from the end.
Thanks everyone for helping me.
Hope this helps someone.

Read string from USB HID RFID Reader with Java

I'm trying to read a String from a via USB connected RFID-Reader. The Reader is recognized correctly inside my appliaction. But I do not know how to read the transferred characters into a String.
If I do not detach the device, the String is printed like by a Keyboard (as you would expect from a HID). What I want is to catch that String inside my Java application only. This is the reason why I detach the USB device.
For example my application prints '"''#$&' to the console (see code below) or something like this
[0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
But what I wanted to read is 05006790.
I think that there is a stupid false in my attempt. I hope that someone can help me to figure out how I have to read the Bytes into a String correctly.
Thank you very much.
CODE FOLLOWS HERE
Context context = new Context();
int result = LibUsb.init(context);
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
for (Device device: list)
{
int address = LibUsb.getDeviceAddress(device);
int busNumber = LibUsb.getBusNumber(device);
DeviceDescriptor descriptor = new DeviceDescriptor();
DeviceHandle handle = new DeviceHandle();
int resultOpen = LibUsb.open(device, handle);
// if (resultOpen < 0) // handle = null;
int resultDescriptor = LibUsb.getDeviceDescriptor(device, descriptor);
// if (resultDescriptor< 0) // handle = null;
if(descriptor.idVendor() == 0x8ff && descriptor.idProduct() == 0x0009)
{
System.out.println("found");
LibUsb.detachKernelDriver(handle, 0);
}
}
UsbServices services = UsbHostManager.getUsbServices();
UsbDevice deviceHigh = findDevice(services.getRootUsbHub(), (short) 0x8ff, (short) 0x0009);
if(deviceHigh != null)
{
System.out.println("found high");
UsbConfiguration configuration = deviceHigh.getActiveUsbConfiguration();
UsbInterface iface = configuration.getUsbInterface((byte) 0x00);
iface.claim();
UsbEndpoint endpoint = iface.getUsbEndpoint((byte) 0x81);
UsbPipe pipe = endpoint.getUsbPipe();
pipe.open();
byte[] buffer = new byte[128];
int rx = 0;
rx = pipe.syncSubmit(buffer);
System.out.printf("%d bytes received\n", rx);
System.out.println(Arrays.toString(buffer));
iface.release();
}

extract string component to store as hash map key within function, pattern matcher?

I have data stored in files at paths such as:
/home/yamada/data/train/atheism/file_name.txt
I use this data to populate a hash map, storing the origin of the data and its contents as follows.
/home/yamada/data/test/sports/t.s_1.txt, [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0]
/home/yamada/data/test/politics/t.p_0.txt, [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
/home/yamada/data/test/atheism/t.a_0.txt, [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/home/yamada/data/test/science/t.s_0.txt, [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
However, I want only to store the path to the point of the directory, not the specific file, like so:
/home/yamada/data/train/atheism
The following regex command is capable of extracting the component I'm interested in according to regex101.com:
(home\/yamada\/data\/train\/atheism)
How could I use the java pattern matcher to ensure that only the string mentioned earlier, the path up to and including the directory, but not the file name, is saved to the hash map?
Is the pattern matcher the best choice for this operation?
Below is the method that populates the hash map.
public static void perceptron_data_struc_generateur(Set<String> GLOBO_DICT,
Map<File, ArrayList<String> > fileDict,
Map<File, int[] > perceptron_input)
{
//create a new entry in the array list 'perceptron_input'
//with the key as the file name from fileDict
//create a new array which is the length of GLOBO_DICT
//iterate through the indicies of GLOBO_DICT
//for all words in globo dict, if that word appears in fileDict,
//increment the perceptron_input index that corresponds to that
//word in GLOBO_DICT by the number of times that word appears in fileDict
//so i can get the index later
List<String> GLOBO_DICT_list = new ArrayList<>(GLOBO_DICT);
for (Map.Entry<File, ArrayList<String>> entry : fileDict.entrySet())
{
int[] cross_czech = new int[GLOBO_DICT_list.size()];
//initialize to zero
Arrays.fill(cross_czech, 0);
for (String s : GLOBO_DICT_list)
{
for(String st : entry.getValue())
{
if( st.equals(s) )
{
cross_czech[ GLOBO_DICT_list.indexOf( s ) ] = cross_czech[ GLOBO_DICT_list.indexOf( s ) ] +1;
}
}
}
perceptron_input.put( entry.getKey() , cross_czech);
}
}
It's quite a bit simpler than that:
String dir = filename.replaceAll("/[^/]*$", "");
If I understand your question correctly you want to find only parts which ends with / (file name will not have it). In that case
(\w+/)+
should do the trick (BTW we don't escape / in Java's regex)
But if your data is always in form path/to/file and you only want to extract path/to then you don't need regex, you can use File class and its getParent method like
String data = new File("/home/yamada/data/train/atheism/file_name.txt").getParent();
System.out.println(data);
This will return \home\yamada\data\train\atheism so you will have / instead of \, but this shouldn't be a problem if you want to use this data in Java (File accepts both separators).

Eliminate default zeros while creating string from byte array

I am getting bytes from IOStream and converting it to string. From that string i am extracting a sequence using substring api.
Size of ByteArray is 128 bytes. If the stream contains only 10 bytes and remaining are filled with zero[initially filled]. I am converting the byte array to string by passing to a string constructor new String(byte[]) and checking the length. The length is 128. Why it is showing 128? Actually it should show for 10 byte character length.
How to eliminate the zero while converting to string. Is there any api's to eliminate the default zeros in byte array. It's creating problem while creating a substring from the constructed string.
byte[] b = { 99, 116, 101, 100, 46, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0}
System.out.println("byte length = " + b.length);
String str;
try {
str = new String(b, "UTF-8");
System.out.println("String length = " + str.length());
System.out.println(str);
System.out.println(" ## substring = " + str.substring(0));
System.out.println(" substring length = "
+ str.substring(0).length());
System.out.println("Done......");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}0, 0, 0 };
To create a String from part of a byte array, use the constructor String(byte[] bytes, int offset, int length, String charsetName). Example:
// uses the first 10 bytes of b
str = new String(b, 0, 10, "UTF-8");
Also, if you're compiling for Java 7 you might as well use StandardCharsets (from the java.nio.charset package), and avoid having to handle UnsupportedEncodingException. Example:
str = new String(b, 0, 10, StandardCharsets.UTF_8);
When you read from an InputStream, it will tell you how many bytes were read. The length of the byte[] itself is mostly irrelevant (other than defining the max number of bytes which could be read in a single call). There should be no need to later go examine the byte[] to try and determine how much of the data is relevant. Pay attention to the return value from read and use that when creating a String.
Additionally, if all of your data is text, consider using an InputStreamReader, perhaps in combination with a BufferedReader.
First an explanation.
Not every byte sequence is valid UTF-8. A binary byte 0 (0x00) is valid, and does not terminate a String as in C.
In fact a terminating \0 was later deplored by either C's Kernighan or Ritchie, as being suboptimal.
To prevent problems, not only Unicode code points above U+007F (0x7f) are multi-byte encoded (whith high bits of bytes set), but also U+0000 in Java's UTF-8, DataOutputSream.
byte[] bytes = get UTF-8 bytes from string
Now bytes could have a multi-byte sequence for the code point 0.
So you might either clean up the bytes, a small loop, or clean up the string:
str = str.replace("\u0000", ""); // All bytes 0
str = str.replaceFirst("\u0000+$", ""); // Only trailing bytes 0, regex
Your code would be like this
byte[] b = { 99, 116, 101, 100, 46, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0};
int nonZeroPos=0;
for (int i = b.length-1; i >0; i--) {
if(b[i]!=0){
nonZeroPos=i;
break;
}
}
System.out.println("byte length = " + b.length);
String str;
try {
str = new String(b, 0, nonZeroPos, "UTF-8");
System.out.println("String length = " + str.length());
System.out.println(str);
System.out.println(" ## substring = " + str.substring(0));
System.out.println(" substring length = "
+ str.substring(0).length());
System.out.println("Done......");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You also could have it done this way -
String zerostring = new String(new byte[]{0});
str=new String(b).replace(zerostring , "");
System.out.println(str);
But disadvantage of this is it will replace 0s coming in the word.

Categories