writing byte[] to file giving corrupt file - java

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.

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]

how to convert string to collection

I have a string in the below format.
[[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028,
122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD,
MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG], [115, 1, 0123490,
63824005632, 0038380001, ,
01, N, 78, , 7481067028, 122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA,
OH, 45805, EM, APEX AFFINITY DISCOUNT CARD, MUCINEX DM 20 0056-32 TAB SA
12HR 600-30MG]]
I want to store in collection with each
[115, 1, 0123490, 63824005632, 0038380001, , 01, N, 78, , 7481067028,
122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, APEX AFFINITY
DISCOUNT CARD, MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG]
[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028,
122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD,
MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG]
How can I split or store in collection?
This should work as long the character ] does not appear as part of a value inside an entry:
public static void main(String[] args) {
String clob = "[[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028, 122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD, MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG], [115, 1, 0123490, 63824005632, 0038380001, , 01, N, 78, , 7481067028, 122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, APEX AFFINITY DISCOUNT CARD, MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG]]";
List<String> entries = new ArrayList<>();
int start = 1;
while (true) {
start = clob.indexOf("[", start);
int end = clob.indexOf("]", start);
if (start != -1 && end != -1) {
entries.add(clob.substring(start, end + 1));
start = end + 1;
} else {
break;
}
}
}
If you know the escape sequence for characters inside your entries (e.g. \]) you have to check if the found end index represents that escape sequence and if, read again, starting from the end index.
There are many ways to do it. Here’s my suggestion:
public static List<List<String>> stringTo2DList(String input) {
if (input.equals("[]")) {
return Collections.emptyList();
}
if (! input.startsWith("[[")) {
throw new IllegalArgumentException("Not a list of lists");
}
if (! input.endsWith("]]")) {
throw new IllegalArgumentException("Not a list of lists");
}
List<List<String>> result = new ArrayList<>();
String[] innerLists = input.substring(2, input.length() - 2).split("\\], \\[");
for (String innerList : innerLists) {
// check for empty inner list
if (innerList.isEmpty()) {
result.add(Collections.emptyList());
} else {
result.add(Arrays.asList(innerList.split(", ")));
}
}
return result;
}
Should your string contain [], I am interpreting it as an empty list even though it might be a list of one element, the empty string. If you prefer the latter, just skip the check for the empty list in for loop.

ArrayList changing when receiving info from socket Java

I am getting this bug which I just can't imagine the cause for. I have a receiver class which receives packets, within which is an ArrayList. No, the packets don't have an ArrayList; the class has one. When I receive a new packet, the ArrayList is altered, but before I alter it. In fact, before I even read the data from the packet into the object I am using the data. Not only that, but the ArrayList is changing all the objects within the array to the equivalent of the object which is created from the data in a later instruction...
Method which is producing the weird behaviour:
public void processPackets() {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
while(true) {
System.out.println("Start: " + outBuffer);
socket.receive(packet);
System.out.println("Before: " + outBuffer);
// Find out where the packets are coming from
if (sendHost == null) {
sendHost = packet.getAddress();
sendPort = packet.getPort();
}
// Grab the data from the packet
PacketInfo data = new PacketInfo(packet.getData());
System.out.println("Found: " + data);
if (this.packetWithinWindow(data.getSequenceNumber())) {
outBuffer.add(data);
System.out.println("After: " + outBuffer);
}
ackThePacket(data);
if (processData()) {break;}; // Returns true after last packet processed
System.out.println("End: " + outBuffer);
}
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
}
Given that outBuffer is an ArrayList<PacketInfo> and PacketInfo.toString() returns a sequence number and a list of the first 20 bytes, this is a sample of the weird behaviour that is then printed to the console (after several cycles through the code where outBuffer starts empty):
Start: []
Before: []
Found:
2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]
After: [
2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]]
End: [
2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]]
Start: [
2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]]
Before: [
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]]
Found:
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]
After: [
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48],
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]]
End: [
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48],
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]]
Start: [
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48],
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]]
Before: [
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Found:
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
After: [
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
End: [
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
This is perplexing me so any help is appreciated. If it's any help, this is compiled using Java 6.

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();
}

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