Reading bit string from binary file in java - java

I have a string of bits with length 128 and I want to convert it to an byte array, then write it to a binary file and later read from the binary file and convert byte array to bit string. This is my code ( I use an input of length 16 for simplicity):
String stest = "0001010010000010";
//convert to byte array
short a = Short.parseShort(stest, 2);
ByteBuffer bytes = ByteBuffer.allocate(2).putShort(a);
byte[] wbytes = bytes.array();
System.out.println("Byte length: "+ wbytes.length);
System.out.println("Writing to binary file");
try {
FileOutputStream fos = new FileOutputStream("test.ai");
fos.write(wbytes);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading from binary file");
File inputFile = new File("test.ai");
byte[] rdata = new byte[(int) inputFile.length()];
//byte[] rdata = new byte[2];
FileInputStream fis;
String readstr = "";
try {
fis = new FileInputStream(inputFile);
fis.read(rdata, 0, rdata.length);
fis.close();
for(int i=0; i<rdata.length; i++){
Byte cb = new Byte(rdata[i]);
readstr += Integer.toBinaryString(cb.intValue());
}
System.out.println("Read data from file: " + readstr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However the string that I read from the file is not equal to the original string. This is the output:
String: 0001010010000010
Byte length: 2
Writing to binary file
Reading from binary file
Read data from file: 1010011111111111111111111111110000010

I would start to choose data type I use for both scenario. Lets think about, you choose byte. So, writing it is pretty easy.
byte data[] = ... //your data
FileOutputStream fo = new FileOutputStream("test.ai");
fo.write(data);
fo.close();
now, lets read stringized data from the file. As you know, 1 byte is 8 bit. So, you need to just read 8 chars from the file, and convert it to a byte. So,
FileInputStream fi = new FileInputStream("test2.ai"); // I assume this is different file
StringBuffer buf = new StringBuffer();
int b = fi.read();
int counter = 0;
ByteArrayOutputStream dataBuf = new ByteArrayOutputStream();
while (b != -1){
buf.append((char)b);
b = fi.read();
counter++;
if (counter%8 == 0){
int i = Integer.parseInt(buf.toString(),2);
byte b = (byte)(i & 0xff);
dataBuf.write(b);
buf.clear(0,buf.length());
}
}
byte data[] = dataBuf.toByteArray();
I think the problem in your code is converting string to byte. Your starting point is already wrong. You are converting your data to short that keeps only 2 byte. But, you say your file can keep 128 bit, that is 16 byte. So, you should not try to convert whole file to a data type, like short, integer or long. you have to convert every 8 bit to byte.

Related

Decompress Gzip file and store in variable

I have searched a lot for conversion from byte to string but my query is a little different, please read ahead.
Currently i have a gzip file which i can decompress using the code from http://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/.
This code helps me store my decompressed output in a file, but how do i store it in a variable? I am using this code currently:
public String unGunzipFile(String compressedFile, String decompressedFile) {
byte[] buffer = new byte[1024];
try {
FileInputStream fileIn = new FileInputStream(compressedFile);
GZIPInputStream gZIPInputStream = new GZIPInputStream(fileIn);
FileOutputStream fileOutputStream = new FileOutputStream(decompressedFile);
StringBuffer str = new StringBuffer();
int bytes_read;
while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
String s = new String(buffer);
str.append(s);
fileOutputStream.write(buffer, 0, bytes_read);
}
gZIPInputStream.close();
fileOutputStream.close();
System.out.println("The file was decompressed successfully!");
System.out.println(str);
String final_string = str.toString();
return final_string;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
Since i am converting bytes to string near the end when bytes_read is not 1024 in length i end up getting some weird data in my StringBuffer, but in the file there is no such data since fileOutputStream.write(buffer, 0, bytes_read); limits it to writing the updated part.
How do i fix this?
Thanks in advance.
Use the String(byte[] bytes, int offset, int length) constructor that lets you specify the length to be converted. i.e.
String s = new String(buffer, 0, bytes_read)
Instead of using String s = new String(buffer), i suggest use
public String(byte[] bytes,
int offset,
int length)
which might help you.

How to read /write XORed txt file UTF8 in java?

what i did so far :
I read a file1 with text, XORed the bytes with a key and wrote it back to another file2.
My problem: I read for example 'H' from file1 , the byte value is 72;
72 XOR -32 = -88
Now i wrote -88 in to the file2.
when i read file2 i should get -88 as first byte, but i get -3.
public byte[] readInput(String File) throws IOException {
Path path = Paths.get(File);
byte[] data = Files.readAllBytes(path);
byte[]x=new byte[data.length ];
FileInputStream fis = new FileInputStream(File);
InputStreamReader isr = new InputStreamReader(fis);//utf8
Reader in = new BufferedReader(isr);
int ch;
int s = 0;
while ((ch = in.read()) > -1) {// read till EOF
x[s] = (byte) (ch);
}
in.close();
return x;
}
public void writeOutput(byte encrypted [],String file) {
try {
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos,"UTF-8");//utf8
String s = new String(encrypted, "UTF-8");
out.write(s);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public byte[]DNcryption(byte[]key,byte[] mssg){
if(mssg.length==key.length)
{
byte[] encryptedBytes= new byte[key.length];
for(int i=0;i<key.length;i++)
{
encryptedBytes[i]=Byte.valueOf((byte)(mssg[i]^key[i]));//XOR
}
return encryptedBytes;
}
else
{
return null;
}
}
You're not reading the file as bytes - you're reading it as characters. The encrypted data isn't valid UTF-8-encoded text, so you shouldn't try to read it as such.
Likewise, you shouldn't be writing arbitrary byte arrays as if they're UTF-8-encoded text.
Basically, your methods have signatures accepting or returning arbitrary binary data - don't use Writer or Reader classes at all. Just write the data straight to the stream. (And don't swallow the exception, either - do you really want to continue if you've failed to write important data?)
I would actually remove both your readInput and writeOutput methods entirely. Instead, use Files.readAllBytes and Files.write.
In writeOutput method you convert encrypted byte array into UTF-8 String which changes the actual bytes you are writing later to the file. Try this code snippet to see what is happening when you try to convert byte array with negative values to UTF-8 String:
final String s = new String(new byte[]{-1}, "UTF-8");
System.out.println(Arrays.toString(s.getBytes("UTF-8")));
It will print something like [-17, -65, -67]. Try using OutputStream to write bytes to the file.
new FileOutputStream(file).write(encrypted);

Reading binary from any type of file

I'm looking for a way that I can read the binary data of a file into a string.
I've found one that reads the bytes directly and converts the bytes to binary, the only problem is that it takes up a significant amount of RAM.
Here's the code I'm currently using
try {
byte[] fileData = new byte[(int) sellect.length()];
FileInputStream in = new FileInputStream(sellect);
in.read(fileData);
in.close();
getBinary(fileData[0]);
getBinary(fileData[1]);
getBinary(fileData[2]);
} catch (IOException e) {
e.printStackTrace();
}
And the getBinary() method
public String getBinary(byte bite) {
String output = String.format("%8s", Integer.toBinaryString(bite & 0xFF)).replace(' ', '0');
System.out.println(output); // 10000001
return output;
}
Can you do something like this:
int buffersize = 1000;
int offset = 0;
byte[] fileData = new byte[buffersize];
int numBytesRead;
String string;
while((numBytesRead = in.read(fileData,offset,buffersize)) != -1)
{
string = getBinary(fileData);//Adjust this so it can work with a whole array of bytes at once
out.write(string);
offset += numBytesRead;
}
This way, you never store more information in the ram than the byte and string structures. The file is read 1000 bytes at a time, translated to a string 1 byte at a time, and then put into a new file as a string. Using read() returns the value of how many bytes it reads.
This link can help you :
File to byte[] in Java
public static byte[] toByteArray(InputStream input) throws IOException
Gets the contents of an InputStream as a byte[]. This method buffers
the input internally, so there is no need to use a
BufferedInputStream.
Parameters: input - the InputStream to read from Returns: the
requested byte array Throws: NullPointerException - if the input is
null IOException - if an I/O error occurs

Convert ZIP to byte array without saving the output to file

I have a ZIP file and when I convert it into byte array and encode it, I am unable to print the encoded format without writing it into file.
Could anyone help in solving this issue?
My code is
InputStream is = null;
OutputStream os = null;
is = new FileInputStream("C:/Users/DarkHorse/Desktop/WebServicesTesting/PolicyCredit.zip");
os = new FileOutputStream("D:/EclipseTestingFolder/EncodedFile1.txt");
int bytesRead = 0;
int chunkSize = 10000000;
byte[] chunk = new byte[chunkSize];
while ((bytesRead = is.read(chunk)) > 0)
{
byte[] ba = new byte[bytesRead];
for(int i=0;i<ba.length;i++)
{
ba[i] = chunk[i];
}
byte[] encStr = Base64.encodeBase64(ba);
os.write(encStr);
}
os.close();
is.close();
}
My Output in the file is
UEsDBBQAAAAIANGL/UboGxdAAQUAAK0WAAAQAAAAUG9saWN5Q3JlZGl0LnhtbJVY3Y6rNhC+r9R34AlqSPankSwkdtNskbLZKOk5Va8QC95d6wRIDZyeffszxgSMGUPKFcx8M/b8egwN87IWcZ6waF+cePLp//qLAw/d8BOL/mRxykRL6sk89T1KLq8adx1XLHp5i55YzkRc8SL3F6534y69O0oQpia6K6LiLTqwpBBpKdUPCRq
But when I am trying to print it on the screen, I am getting in this way
8569115686666816565656573657871764785981117112010065658185656575488765656581656565658571571159787785381517410890711084876110104116987486895189541147810467431145782515265108113838097110107831191071001167811510798769075791075386975681675753100541198273689012110110210211512212010383777185807570991205677479856101103119785655738799905411997704399101807611247471137665119471005666797647109821201211078276
You need to create a string representation of Base 64 encoded data.
System.out.println( new String(encStr, Charset.forName("UTF-8")));
Here are some other examples Base 64 Print Question
String Class
Assuming your result array byte[] encStr = Base64.encodeBase64(ba) is actually the encoded string, try the following:
System.out.println(new String(bytes, Charset.defaultCharset());
If you are using JDK 7 you can use Files.readAllBytes(path)
Your code would be much simpler like below:
Path path = Paths.get("C:/Users/DarkHorse/Desktop/WebServicesTesting/PolicyCredit.zip");
byte[] data = Files.readAllBytes(path);
byte[] encStr = Base64.encodeBase64(data);
System.out.println( new String(encStr));
Your will be able to print on console.

how to convert text file of base64 into xml file(or any other type of file)

I have these Base64 in a text file:
77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxSb290Pg0KICA8SXRl
bXM+DQogICAgPEl0ZW0gTmFtZT0iQ2xhaW1OdW1iZXIiIFZhbHVlPSI0ODY1MTQ4MDQiIC8+DQog
ICAgPEl0ZW0gTmFtZT0iRG9jVHlwZSIgVmFsdWU9IkxpdGlnYXRpb24iIC8+DQogICAgPEl0ZW0g
TmFtZT0iRG9jU3ViVHlwZSIgVmFsdWU9IklOVCBBbnN3ZXJzIiAvPg0KICAgIDxJdGVtIE5hbWU9
IkRvY0RhdGVzIiBWYWx1ZT0iNi8xNC8yMDExIDEyOjAwOjAwIEFNIiAvPg0KICAgIDxJdGVtIE5h
bWU9IkNvbW1lbnRzIiBWYWx1ZT0iUGx0ZiBhbnN3ZXJzIHRvIGludHMgYW5kIHN1bW1hcnkgYnkg
ZGVmZW5zZSBjb3Vuc2VsIiAvPg0KICAgIDxJdGVtIE5hbWU9IlNlY3VyaXR5R3JvdXAiIFZhbHVl
PSJTcGVjIEdlbiIgLz4NCiAgICA8SXRlbSBOYW1lPSJDbGFpbU9mZmljZSIgVmFsdWU9IkFsdCBN
a3RzIE5KIiAvPg0KICAgIDxJdGVtIE5hbWU9IkNsYWltYW50TmFtZSIgVmFsdWU9IkpvYW4gS2Vs
bGV5IiAvPg0KICAgIDxJdGVtIE5hbWU9Ikluc3VyZWROYW1lIiBWYWx1ZT0iQm96dXR0byAmYW1w
OyBBc3NvY2lhdGVzIiAvPg0KICAgIDxJdGVtIE5hbWU9IlByaXZpbGVnZWQiIFZhbHVlPSJObyIg
Lz4NCiAgICA8SXRlbSBOYW1lPSJEaXNwb3NpdGlvbiIgVmFsdWU9IlNlbnQgdG8gRmlsZSIgLz4N
CiAgICA8SXRlbSBOYW1lPSJGZWF0dXJlIiBWYWx1ZT0iIiAvPg0KICAgIDxJdGVtIE5hbWU9IlNl
bnRUbyIgVmFsdWU9IiIgLz4NCiAgICA8SXRlbSBOYW1lPSJGbGFnIiBWYWx1ZT0iMCIgLz4NCiAg
ICA8SXRlbSBOYW1lPSJSZXRhaW5JbWFnZSIgVmFsdWU9Ik5vIiAvPg0KICAgIDxJdGVtIE5hbWU9
IlJldGFpbk9yaWdpbmFsIiBWYWx1ZT0iRG8gTm90IFJldGFpbiIgLz4NCiAgICA8SXRlbSBOYW1l
PSJTb3VyY2UiIFZhbHVlPSJJTkRFWEVEIiAvPg0KICA8L0l0ZW1zPg0KICA8Um91dGU+DQogICAg
PHRvIC8+DQogICAgPGNjIC8+DQogICAgPE5vdGUgLz4NCiAgPC9Sb3V0ZT4NCjwvUm9vdD4=
I need to be able to take those base 64 charecters from the text file and output a new XML File. Currently, the InputStream is not being correctly converted to base 64
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\khurt\\Desktop\\xml.txt");
InputStream myScan = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
myScan.read(b);
String cowo = myScan.toString();
String decoded = DatatypeConverter.printBase64Binary(b);
String cat = b.toString();
System.out.println(decoded);
byte[] bArray = cat.getBytes();
OutputStream out = new FileOutputStream("C:\\Users\\gdfurt\\Desktop\\cow.xml");
out.write(b);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I used the System.out.println(decode); to check to see if the charecters matched the ones in the file and they do not. I can't figure out why, I have tried using a scanner and that throws it off more.
Data you have got is Base64 encoded. and you are not decoding it in your code. That is main reason behind other programs cannot read it as XML file.
Another is hidden in your Bytes of data. Start of Byte data is 77u/ which is saying data is BINARY data and becomes problem here.
Use Link to experience decoded data:
http://www.opinionatedgeek.com/dotnet/tools/base64decode/
If you will use 77u/ at start of data you will experience data is BINARY and will get downloaded as file. And if you do not use 77u/ it will show output online only.
Remove first 4 char while processing your data and then you are good to go inside java code only.
EDIT
Please use below code snippet. You are re-encoding byte array. You need to decode it. Also this process needs little bit conversions of String to Byte and vice-versa.
try {
File file = new File("C:\\Users\\ABC\\Desktop\\xml.txt");
InputStream myScan = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
myScan.read(b);
String cowo = new String(b);
System.out.println( cowo );
String decoded = new String(DatatypeConverter.parseBase64Binary(cowo));
String cat = b.toString();
System.out.println(decoded);
byte[] bArray = cat.getBytes();
OutputStream out = new FileOutputStream("C:\\Users\\ABC\\Desktop\\cow.xml");
out.write(decoded.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read the bytes:
byte[] b;
try (InputStream in = new FileInputStream(file)) {
b = new byte[(int) file.length()];
in.read(b);
} // Closes in
Which in Java 7 goes easier:
b = Files.readAllBytes(file.toPath());
or immediately with Path i.o. Fiile:
Path path = Paths.get("C:\\Users\\khurt\\Desktop\\xml.txt");
b = Files.readAllBytes(path);
As Base64 only uses ASCII do:
String encoded = new String(b, StandardCharsets.US_ASCII);
Parse Base64 text to byte[]
b = DatatypeConverter.parseBase64Binary(encoded);
If you want the XML as text:
String decoded = new String(b, StandardCharsets.UTF_8);
By the way, the XML starts with "\ufeff" the Unicode BOM character, which is redundand.
Addendum 2021-11-16
Nowadays there is one Base64 class in java SE:
b = Base64.getDecoder().decode(b);
or even (suitable for large files):
b = Base64.getDecoder().decode(Files.newInputStream(path));

Categories

Resources