Any help appreciated.
I have file in JSON format.
{
"identifier": 123;
"MessageID: "xyzzzzzz"
"Data": "xmlformattedstring"
}
I would like to just Gzip the ("xmlformattedstring") keeping the JSON body intact.
Is it possible to
Sure, you could set the data with something like:
String data = "xmlformattedstring";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(baos);
gzos.write(data.getBytes("UTF-8"));
gzos.close();
String base64CompressedString = Base64.getEncoder().encodeToString(baos.toByteArray());
and then use base64CompressedString in your JSON. Basically all this does is compress your string and convert it to base64. Base64 won't be as efficient as pure GZIP but it will be needed to send as an ASCII string like in JSON.
Related
I have http response object "httpResponse" which is instance of class javax.ws.rs.core.Response. I have to convert it into a human readable string object for debugging. How can I achieve this?
final InputStream inputStream = (InputStream) httpResponse.getEntity();
final ByteArrayDataSource dataSource = new ByteArrayDataSource(inputStream, ContentType.MULTIPART_FORM_DATA.getMimeType());
final MimeMultipart multipartResponse = new MimeMultipart(dataSource);
I tried doing the below using the inputStream object. The output is all binary. Do I need to do base64 decoding? Thanks.
IOUtils.toString(inputStream, StandardCharsets.UTF_8))
As you said you need decoding.
// string to byte[]
byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
// byte[] to string
String s = new String(bytes, StandardCharsets.UTF_8);
Source:
https://mkyong.com/java/how-do-convert-byte-array-to-string-in-java/
Here it is explained not only the conversion from bytes to text, but also the conversion from bytes to image or any kind of binary type.
I got some legacy text data which is utf-8 encoded, but against a gzipped byte array.
I'm wondering whether I can get the raw data back
something like:
String text = "Hello World!";
byte[] binData = text.getBytes("UTF-8");
byte[] compressData = gzip(binData);//via GZIPOutputStream
//this is what I have
String encodedString = new String(compressData, "UTF-8");
assertEquals(text, smartDecode(encodedString));
Is it possible to provide a function like smartDecode to help me retrieve the original text 'Hello World!' back?
There is a way to convert an InputStream to a String, and encode it to base64, right?
In my function, I get InputStream parameter, and need to insert it into the BLOB field in my Oracle database table.
Is there a way to do that?
(My database object contains string field to save the image, but I don't find any way to convert the InputStream to string in base 64 format.)
There is nice way to do this is using IOUtils to convert the InputStream into a Byte Array...
something like
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
Here you can use Base64 to convert Byte Array to String.
Sample Code
String encoded = Base64.getEncoder().encodeToString(bytes);
Now you can use your String.
You can try something like this using the Base64 API.
InputStream finput = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
finput.read(imageBytes, 0, imageBytes.length);
finput.close();
String imageStr = Base64.encodeBase64String(imageBytes);
Use this:
http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html
The simplest way would be to use IOUtils from apache-commons to do that:
String result= IOUtils.toString(inputStream, ENCODING);
From the documentation:
toString(byte[] input, String encoding)
Gets the contents of a byte[] as a String using the specified character encoding.
After that To Encode/Decode in Base64:
// Encode
String resultBase64Encoded = Base64.getEncoder().encodeToString(result.getBytes("utf-8"));
// Decode
byte[] asBytes = Base64.getDecoder().decode(resultBase64Encoded);
String resultAsStringAgain= String(asBytes, "utf-8")
Note: I'm assuming you use JDK 8 for the Encode/Decode part.
Apparently the OP wants just to persist an InputStream to the DB. You can do that directly using JDBC:
InputStream inputStream = ......;
String sql = "INSERT INTO TABLE_NAME(COLUMN_NAME) values (?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setBlob(1, inputStream);
statement.executeUpdate();
As I mentioned, you shouldn't use String for binary data. The base64-encoded data can be stored as a String though. But since your database column is a blob, I would continue to work with a byte[].
Use IOUtils to get a byte[] from the InputStream:
byte[] bytes = IOUtils.toByteArray(yourInputStream);
byte[] encoded = java.util.Base64.getEncoder().encode(bytes);
Then write it to the database. Writing a blob using jdbc and a PreparedStatement looks like this:
yourPreparedStatement.setBytes(nIndex, encoded);
In case someone is looking for solution without external libraries. Java8 adds Base64 utility class which contains getEncoder() method. Java9 adds fancy method on InputStream called readAllBytes() Link to api
So now you without any external libraries your code may look like
import java.io.InputStream;
import java.util.Base64;
public String encode(InputStream stream) throws IOException {
final var bytes = stream.readAllBytes();
return Base64.getEncoder().encodeToString(bytes);
}
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
I'm getting a response XML from web service and one of the field contains a string that should be convertable to pdf file.
This is example string that I extract from xml response:
JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDwvUHJvY1NldFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldL0ZvbnQ8PC9GMSA2IDAgUi9GMiA3IDAgUj4+Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDg0MS44OSA1OTUu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gMyAwIFI+PgplbmRvYmoKMTMgMCBvYmoKPDwvUHJvZHVjZXIoaVRleHQgMi4xLjcgYnkgMVQzWFQpL01vZERhdGUoRDoyMDE1MDMwOTEwMzcwOSswMScwMCcpL0NyZWF0aW9uRGF0ZShEOjIwMTUwMzA5MTAzNzA5KzAxJzAwJyk+PgplbmRvYmoKeHJlZgowIDE0CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNDU5OSAwMDAwMCBuIAowMDAwMDA3OTY3IDAwMDAwIG4gCjAwMDAwMDgxMjkgMDAwMDAgbiAKMDAwMDAwNDQ4MiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQzMDEgMDAwMDAgbiAKMDAwMDAwNDM5NCAwMDAwMCBuIAowMDAwMDA3ODUwIDAwMDAwIG4gCjAwMDAwMDQ3NjEgMDAwMDAgbiAKMDAwMDAwNzY2NyAwMDAwMCBuIAowMDAwMDA3NzYxIDAwMDAwIG4gCjAwMDAwMDgxOTggMDAwMDAgbiAKMDAwMDAwODI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8L1Jvb3QgMTIgMCBSL0lEIFs8NGYyYjMyZmU2NjQ4MzBlZjYxODliMzJhMjNiY2E1MTg+PDIwZGUyZmJkMjc1NzVhMzMyNmNkYTQ0OGNmYmE0OTMyPl0vSW5mbyAxMyAwIFIvU2l6ZSAxND4+CnN0YXJ0eHJlZgo4MzY3CiUlRU9GCg==
now I'm trying to convert it into proper pdf file like this:
byte[] bytes = THAT_LONG_STRING.getBytes();
BufferedOutputStream bos = null;
FileOutputStream fos = new FileOutputStream(new File("a.pdf");
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.flush();
bos.close();
Sadly this does not work for me, output file is just file with that long string, pdf viewers cannot open it.
Any advices are welcome :)
Your string is in Base64 format. You should decode it to a byte array first, then write it to a file.
Just replace this line:
byte[] bytes = THAT_LONG_STRING.getBytes();
With this one (Apache Commons Codec required):
byte[] bytes = Base64.decodeBase64(THAT_LONG_STRING);