How to decompress lzo compressed byte array in java? - java

I am new to LZO compression and decompression. I'm trying to use this lzo-java library.
Input Information :
I have one byte array which is in compressed format. This byte array I want to decompress and finally I want decompressed byte array.
NOTE : I don't know the how much size should give to decompress byte array because I don't know exact size of decompressed byte.
I created below code but it is not giving any exception and not even decompressing single byte.
Program :
InputStream stream = new ByteArrayInputStream(src);
int b1 = stream.read();
int b2 = stream.read();
int b3 = stream.read();
int b4 = stream.read();
int dstLength = ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);// decompressed byte array length not known
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, LzoConstraint.SAFETY);
byte dst[] = new byte[dstLength];
lzo_uintp lzo = new lzo_uintp(dstLength);
int decompress = decompressor.decompress(src, 0, src.length, dst, 0, lzo);
System.out.println("decompessed : " + decompress);//it is giving me 0
What I'm missed in above code? Any suggestions will be helpful for me!

Related

storing an integer in a specific number of bytes in an array of bytes - JAVA

I'm trying to implement a database in java using slotted pages , so basically what I want to do is to store my data in a specific number of bytes .
so this is the page where I have to store it .
protected byte[] myData = new byte[PAGE_SIZE*1024]; //array for storing my data
now I want to store an Integer in the first 4 bytes of myData , when I do that automatically is stored in just one byte if the Integer doesn't exceed 255 , but what I want to do is use 4 bytes for my Integer it doesn't matter if it's 1 or one billion .
my question is , is it possible to do that in java ? to control how many bytes my data must allocate , like I assign 3 to the first 4 bytes of my byte array ?.
if (recordFitsIntoPage(record)) {
byte [] fix_rec = new byte [record.getFixedLength()];
byte [] var_rec= new byte [record.getVariableLength()];
var_rec = var_rec(record);
fix_rec = fix_rec(record);
byte [] box = { (byte) record.getVariableLength() ,(byte) offsetEnd };
System.arraycopy(fix_rec, 0,data,offset,record.getFixedLength());
System.arraycopy(var_rec, 0,data,offsetEnd,record.getVariableLength());
read_bytes(data);
this.numRecords++;
}else {
throw new Exception("no more space left");
}
I have a fixed-sized variables that I need to store them in my case for example in 12 bytes , I have been using System.arraycopy() but it's not relevant in my case , after I execute the code I get out of bound exception "last source index 12 out of bounds for byte[9]"
because it uses just 9 bytes to store my Data not 12 .
This method creates an array of 32 bytes of any integer given - be it 1 or one billion:
private static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
byte[] src = b.toByteArray();
byte[] dest = new byte[numBytes];
boolean isFirstByteOnlyForSign = src[0] == 0;
int length = isFirstByteOnlyForSign ? src.length - 1 : src.length;
int srcPos = isFirstByteOnlyForSign ? 1 : 0;
int destPos = numBytes - length;
System.arraycopy(src, srcPos, dest, destPos, length);
return dest;
}
You have an array of byte ready to store:
byte[] myData = new byte[PAGE_SIZE*1024];
You have a hand-picked integer as well:
BigInteger myInteger = new BigInteger("50000000000");
Then we change our integer to 32-length byte[]
byte[] bytesOfInteger = bigIntegerToBytes(myInteger,32);
Finally, you copy first 4 bytes of integer to your byte[] myData
System.arraycopy(bytesOfInteger, 0, myData, 0, 3);
So this shows that you can allocate any decent big integer into a fixed 32 byte[].

Why method for ByteBuffer putShort(value) does not working correctly in my case?

My task is convert short[] array to byte[] array, because need send bytes via socket. This is bytes for AudioTrack (Android)
For converting use this post, specifically this and this
This method gives only white noise, when try to convert short to byte array:
val sampleBuffer = decoder.decodeFrame(frameHeader, bitstream) as SampleBuffer
val pcm = sampleBuffer.buffer //pcm is short[] array
byteBuf = ByteBuffer.allocate(pcm.size * 2) // because 1 short = 2 bytes
while (pcm.size > i) {
byteBuf.putShort(pcm[i])
i++
}
auddioTrack.write(byteBuf.array(), 0, byteBuf.limit());
But this convert works fine:
var i = 0
val byteBuf = ByteBuffer.allocate(pcm.size * 2)
val buff = ByteBuffer.allocate(2)
//pcm size equals 2304
while (pcm.size > i) {
// byteBuf.putShort(pcm[i])
byteBuf.put(byteArrayOf((pcm[i].toInt() and 0x00FF).toByte(), ((pcm[i].toInt() and 0xFF00) shr (8)).toByte()))
i++
}
auddioTrack.write(byteBuf.array(), 0, byteBuf.limit());
Why has it happened?
byteBuf.array().size will return the size of the buffer (pcm.size * 2) regardless of whether that many bytes were written into the buffer. You probably want byteBuf.limit() instead.

How to reverse byte[]<=>double[] in java

In java.
I had some file (*.wav) from where I extracted byte[]. After that I converted this to double[] by this code:
for (int i = 0; i < bytesIn.length && idx < buffer.length; i += 2)
{
byte lowByte = bytesIn[i];
byte highByte = bytesIn[i+1];
//Little endian
buffer[idx++] = (lowByte & 0xFF | highByte << 8);
}
Where bytesIn = byte[] of file
and buffer = double[]
After this I did some operation on buffer used fast Fourier transform and inverse. Now after inverse operation from fft I have double[] but I dont know how to get back to byte[].
I found this:
byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(value);
but bytes store different value than I expected. Changed buffer after IFFT is same as original one. Can anyone write how to reverse code from first code block?

Efficient way to convert a String (representing HEX) to a byte array? Android

I'm working on a project that involves receiving a byte array over wireless, the Android app reads this as a String over a TCP connection:
input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
...
...
//Loop
String read = input.readLine();
//Do something meaningful with String read...
The String will always be of a fixed format i.e. the first 3 characters will be an ID and the next following 20 characters will be the message data. The amount of characters will not change (3+20 characters = 23, with a starting and ending character '[' and ']' so that's 25 characters in total.
An example of a String received by the application would be [01A01020304050A0B0C0D]
ID - 0x01A
Byte0 0x01
Byte1 0x02
Byte2 0x03
Byte3 0x04
Byte4 0x05
Byte5 0x0A
Byte6 0x0B
Byte7 0x0C
Byte 8 0x0D
I would guess that I would have to use the substring operation, but I'm having trouble converting the substring to a byte value (note: the app is expecting byte[] and not Byte[]) and I feel I'm not doing it efficiently. I came across this piece of code that I've been using:
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
This is returning a byte array of size 1 and will have to be run 9 times (9 bytes) per message. I'm a bit concerned that this may be a bit too strenuous on processing, especially when the application is receiving messages very frequently (roughly about 10-15 messages per second)
I appreciate any thoughts and many thanks in advance!
just use this :
byte[] decodedString = Base64.decode(your_string, Base64.DEFAULT);
byte[] b = string.getBytes();
byte[] b = string.getBytes(Charset.forName("UTF-8"));
byte[] b = string.getBytes("UTF-8");
There is no way to be more effecient than using this methods.
Best and simple way:
String myString = "This is my string";
byte[] myByteArray = myString.getBytes("UTF-8");
Now, you able to access id, message whatever; easily from myByteArray.
Just write your data like
byte[] data = yourData.getBytes();
os.write(data, 0, data.length) // data is of 23 bytes
os.flush();
what about reading through InputStream, as you mentioned in your question that String is of 23 characters just do like
public byte[] readData(InputStream is) {
byte[] data = new byte[23];
int read = is.read(data);
System.out.println("Read: " + read);
return data;
}
When you have data then you can split data like this
byte[] tempId = new byte[3];
System.arrayCopy(data, 0, id, 0, id.length);
byte[] tempMessage = new byte[20];
System.arrayCopy(data, 3, message, 0, message.length);
String id = new String(tempId);
String message = new String(tempMessage);
Now you id and message separated and converted into String.
byte[] array = String.getBytes("UTF-8");

Java - is there something like binary stream?

I had one question.
Is there library or etc to compose int & strings to byte array ?
Like :
byte temparray[] = new byte[10];
int a = 10;
int b = 10;
temparray << new String("12") << a << b;
Thanks.
edit
byte[] buffer = new byte[649];
byte[] charname = this.getName().getBytes();
System.arraycopy(charname, 0 , buffer, 0, charname.length);
for(int i=0;i<16;i++) //mystery crs 16 zeros
{
buffer[i+17] = (byte)0x30;
}
buffer[34] = this.faction;
if(this.characterClass == 2)
{
buffer[40] = 2;
} else
{
buffer[40] = 1;
}
System.arraycopy(BitTools.shortToByteArray(face), 0, buffer, 42, 2);
buffer[44] = 1;
buffer[48] = (byte)this.characterClass; //class byte
buffer[52] = 2; explanation yet
buffer[54] = (byte)this.getLevel();
This is an example of my packet generator and i wanted to simplify it, but in packet i use only shorts, ints and strings.
java.io.ByteArrayOutputStream is a stream implementation that collects content on an internal byte array, and you can wrap it in a java.io.OutputStreamWriter to write character content to it.
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(byteOut, "UTF-8"); // Uses UTF-8 encoding
out.write("12");
out.write(10);
out.write(10);
out.close();
byte[] bytes = byteOut.toByteArray();
After this, bytes.length is just long enough for the bytes written to byteOut.
Yes. See java.io.ByteArrayOutputStream. Note that you can wrap this stream to support writing of other types like String: PrintWriter pw = new PrintWriter(yourByteArrayOutputStream); pw.print("Hello");
And afterwards use yourByteArrayOutputStream.toByteArray(); to get the byte array.
http://docs.oracle.com/javase/7/docs/api/
Integer.byteValue();
Double.byteValue();
String.getBytes();
// etc.
Take a look at String#getBytes and ByteBuffer. Charsets and byte order might be important depending on your use case.

Categories

Resources