ems - java zip to binary format and then decompress -
ems - java zip to binary format and then decompress -
i have task
read zip file local binary message
transfer binary message through ems string (done java api)
receive transferred binary message string (done java api)
decompress binary message , print out
the problem facing dataformatexception while decompress message.
i have no thought part went wrong.
i utilize read file binary message:
static string readfile_stream(string filename) throws ioexception { file file = new file(filename); byte[] filedata = new byte[(int) file.length()]; fileinputstream in = new fileinputstream(file); in.read(filedata); string content = ""; system.out.print("sent message: "); for(byte b : filedata) { system.out.print(getbits(b)); content += getbits(b); } in.close(); homecoming content; } static string getbits(byte b) { string result = ""; for(int = 0; < 8; i++) result = ((b & (1 << i)) == 0 ? "0" : "1") + result; homecoming result; }
i utilize decompress message:
private static byte[] tobytearray(string input) { byte[] bytearray = new byte[input.length()/8]; (int i=0;i<input.length()/8;i++) { string read_data = input.substring(i*8, i*8+8); short = short.parseshort(read_data, 2); bytearray[i] = (byte) a; } homecoming bytearray; } public static byte[] unzipbytearray(byte[] file) throws ioexception { byte[] byreturn = null; inflater oinflate = new inflater(false); oinflate.setinput(file); bytearrayoutputstream ozipstream = new bytearrayoutputstream(); seek { while (! oinflate.finished() ){ byte[] byread = new byte[4 * 1024]; int ibytesread = oinflate.inflate(byread); if (ibytesread == byread.length){ ozipstream.write(byread); } else { ozipstream.write(byread, 0, ibytesread); } } byreturn = ozipstream.tobytearray(); } grab (dataformatexception ex){ throw new ioexception("attempting unzip file not zipped."); } { ozipstream.close(); } homecoming byreturn; }
the message got
java.io.ioexception: attempting unzip file not zipped. @ com.sourcefreak.example.test.tibcoemsqueuereceiver.unzipbytearray(tibcoemsqueuereceiver.java:144) @ com.sourcefreak.example.test.tibcoemsqueuereceiver.main(tibcoemsqueuereceiver.java:54)
after check, binary message not corrupted after transmission. please help figure out problem.
have tried using inflaterinputstream? based on experience, using inflater straight rather tricky. can utilize started:
public static byte[] unzipbytearray(byte[] file) throws ioexception { inflaterinputstream iis = new inflaterinputstream(new bytearrayinputstream(file)); bytearrayoutputstream baos = new bytearrayoutputstream(); byte[] buffer = new byte[512]; int length = 0; while ((length = iis.read(buffer, 0, buffer.length) != 0) { baos.write(buffer, 0, length); } iis.close(); baos.close(); homecoming baos.tobytearray(); }
java ems
Comments
Post a Comment