Convert bytes to string from a txt file - java

In our school project we are supposed to make a program which uses txt files to store data. I decided to simply store them using bytes, with UTF-8 encoding.
When I write to the file I first convert my string to bytes.
Then when i try to read info from file, I cannot do so because the bytes i have stored in the txt are in string format, and it's impossible to convert from String to Bytes without encoding.
Here are the parts of my code responisble for:
try
{
byte[] bytes=line.getBytes("UTF-8");
out.println(bytes);
}
catch(IOException q)
{
System.out.println("Cant encode pls");
}
^Writing
Reading:
try{
byte[] readbyte=in.readLine().getBytes("UTF-8");
String str=new String(readbyte, "UTF-8");
System.out.println(str+" Decoded");
model1.addElement(str);
}
Unfortunately the reading gives me only what i started with, since i simply encode the string from the txt and decode it back.
So how do i fix this?

Related

Cannot properly decode Base64 MIME image to byte array (Java)

I'm trying to write some selenium/java test that checks 2FA configuration process. Thus I have to scan some QR code from a page in order to process it with zxing. The image format is Base64 and I'm struggling with decoding it to the byte array. The following code should convert base64 string to byte array, and then write it to the file.
Here is the code I wrote:
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(",")[1];
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);
try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
stream.write(decoded);
}
This code compiles with no errors, but when I try to open generated png file I get only "Fatal error reading PNG image file: Decompression error in IDAT".
I know that base64 string is valid as I was able to convert it to the image using some online converter. Also, I checked the string with online validator and it said that this is a valid base64 MIME string.
Example of the base64 code below:
iVBORw0KGgoAAAANSUhEUgAAAeoAAAHqAQAAAADjFjCXAAAET0lEQVR4nO2dXYrrOgyAP50E5jGB%0AWUCX4uxgljScJd0dxEvpAgacx4KDzoPsxJ3hcqHppadUegiZxB9uQEjWjz2iHJD46wgNjjvuuOOO%0AO+64447fF5ciPbCUi8i4CnEEYBWZAJmWOnS63+yOvygeVFU1AfF0MV3TmU5lWnobob+lB+hUVVWv%0A8YOzO/6i+FLMl3yee2DI9kznzcx9pjK02MR7zu74a+H99wdx7LJAnyUoENJ7Fp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xvD3nZC/8A9TMnTabx8qL5PthHb8rvkez%0AZutYRSarza5iuvb75LbO8SPy84TYU0bCWcqpYXF6Uyx8HXJPnAAG35no+BH5flZnTZ+AMHz126md%0AaocTBwWBtb546m93/FH49xi2Pts26qheNRnvuyp8Xef4rSL632P+Xfy/1znuuOOOO+64447/Lfgf%0AFuoX02DU2vMAAAAASUVORK5CYII=
try this
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(";")[1].split(",")[1]; //Try this
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);
try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
stream.write(decoded);
}
Okay, so i figured it out, so now it works.
The thing is when I run:
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
it adds a neweline (%0A) characters to the string so before decoding it to byte array I need to run qrCodeImage = qrCodeImage.replaceAll("%0A", ""); in order to remove them.

Junk Characters Coming while Decoding Data in Base 64

I am getting PDF content which is Base 64 encoded. I tried to decode it using NIFI with Processor Base64EncodeContent. The Decoded file I am sending in mail. Below is small sample of output coming in mail.
enter image description here
"No data should be available in . ¹ Check if sent . . All documents are sent as pdf to* 9 : ’ ³: > < âA m¬‘²#%é‚ÇŽÇ¢|ÀÈ™$Éز§Uû÷LÒTB¨ l,îåù˜$â´º?6N¬JC¤ŒÃ°‰_Ïg -æ¿;ž‰ìÛÖYl`õ?èÓÌ[ ÿÿ PK"
How to extract the data in PDF as sent by third party?
I have tried to decode it using JAVA code and there also its failing. Not able to open the PDF, junk characters coming there too.
ConvertedJPGPDF.pdf file used below contains Base64 encoded String.
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file =
Files.readAllBytes(Paths.get(filePath+originalFileName));
// byte[] decodedBytes = Base64.getDecoder().decode(input_file);
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
FileOutputStream fos = new FileOutputStream(filePath+newFileName);
fos.write(decodedBytes1);
fos.flush();
fos.close();
You mentioned that the file contains base64 encoded string already.
ConvertedJPGPDF.pdf file used below contains Base64 encoded String.
So, you don't need to run this line:
byte[] encodedBytes = Base64.getEncoder().encode(input_file);
By doing so, you are trying to encode those bytes again.
Directly decode the input_file array and then save the obtained byte array into a .pdf file.
Update:
The ConvertedJPGPDF.pdf doesn't really have to be named .pdf. It's really a plain text file considering that it is base 64 encoded.
Anyway, the following piece of code is working for me:
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file = Files.readAllBytes(Paths.get(filePath+originalFileName));
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
Files.write(Paths.get(filePath+newFileName), decodedBytes1);
Hope this helps!

Issues in converting base64 decoded byte array to String in java

Issues in converting base64 decoded byte array to String in java :
public static String decode(String strcontent) throws Exception
{
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(strcontent);
return new String(imgBytes);
}
With the above code; was trying to create a string out of the Base 64 decoded byte array (imgBytes ) & input strcontent is base 64 encoded string. For text files it working fine , but for PDF and image files the string conversion is having issues. Have tried different encoding as UTF-8 , UTF 16 etc. But no use. The returned string is different than the original one.
When tried to write the byte array to a file like :
OutputStream out = new FileOutputStream(##path);
out.write(imgBytes);
out.close();
File is getting created properly without any issues.
I tried the below code:
byte[] imgBytes= ( new String(imgBytes1)).getBytes(); //Converting to String and back to bytes
OutputStream out = new FileOutputStream(##Filename);
out.write(imgBytes); out.close();
This time the image file is corrupted.
Please suggest.

Desired Result Data is not comming from compressed base64 string by scanning QR code from android phone?

I have extracted base64 string from an image, then perform compression on the base64 string.Then generate QR code using this compressed string.
But when I scan(QR CODE) the result using android phone I am getting value like 17740296 which is not value I want. My purpose is that after getting the scanned value I will decompress it and display the image using bitmap from base64. What is the wrong in my code. I am using java code to generate QR CODE.(I have tried UTF-8 also but not working). This code is working for String but not for the image.
Compressing Code is -
public static String compressBD(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
Decompress Code is -
public static String decompressBD(String str) throws Exception {
if (str == null || str.length() == 0) {
return str.toString();
}
// System.out.println("Input String length : " + str.toString().length());
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1")));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1"));
String outStr = "";
String line;
while ((line=bf.readLine())!=null) {
outStr += line;
}
//System.out.println("Output String lenght : " + outStr.length());
return outStr;
}
this wont work.
Base64 ist an encoding that can be used entirely in strings. You can print it on a piece of paper, type it into your machine and decode an image fromt it.
However, if you use a gzip compression and transform this to a string, the compression generates bytes that are outside the string encoding and can not be printed or presented as string.
Base64 is meant to be the "compressed" encoding for strings. I would really encourage you not to use string as storage, but directly store the binary data, or transmitt it. This would also be considerably faster, since base64 encoding is very slow.
Its purpose is entirely to store binary content, that contains non printable bytes, in text messages. For what ever reason.
I hope it is understandable, but it basically means, you cant store a base64 zip content in a string. You have to store the binary representation if you want to compress.

java apache IOUtils breaks file content

I need to encode/decode pdf file into Base64 format.
So I read file from disk into String(because I will receive file in String Base64 format in future);
String pdfString = IOUtils.toString(new FileInputStream(new
File("D:\\vrpStamped.pdf")));
byte[] encoded = Base64.encodeBase64(pdfString.getBytes());
byte[] newPdfArray = Base64.decodeBase64(encoded);
FileOutputStream imageOutFile = new FileOutputStream(
"D:\\1.pdf");
imageOutFile.write(newPdfArray);
imageOutFile.close();
imageOutFile.flush();
So my D:\\1.pdf doesnt opens in AdobeReader, but if I read file straight to byte array, using IOUtils.toByteArray(..) instead ,all works fine and my D:\\1.pdf file sucessfuly opens in Adobe Reader:
byte[] encoded = Base64.encodeBase64(IOUtils.toByteArray(new FileInputStream(new File("D:\\vrpStamped.pdf"))););
It seems to me thath IOUtils.toString(..) change something inside file content. So how can I convert file to String with not content breaking?
How to encode a pdf...
byte[] bytes = IOUtils.toByteArray(new FileInputStream(new File("/home/fschaetz/test.pdf")));
byte[] encoded = Base64.encode(bytes);
String str = new String(encoded);
...now do something with this encoded String, for example, send it via a Rest service.
And now, if you receive an encoded String, you can decode and save it like this...
byte[] decoded = Base64.decode(str.getBytes());
FileOutputStream output = new FileOutputStream(new File("/home/fschaetz/result.pdf"));
output.write(decoded);
output.close();
Works perfectly fine with all files, not limited to images or pdfs.
What your example is doing is...
Read the pdf into a String (which pretty much destroys the data, since you are reading binary data into a String)
Encode this spring (which is in all likelyhood not a valid representation of the original pdf anymore)
Decode it and save it to disk

Categories

Resources