Java: encode string in quotation marks

I am looking for a quoted-printable encode a string in Java, just like php native quoted_printable_encode() .

I tried using the JavaMails MimeUtility library. But I can not get the encode(java.io.OutputStream os, java.lang.String encoding) method encode(java.io.OutputStream os, java.lang.String encoding) as it accepts the input stream OutputStream instead of String (I used the getBytes() function to convert String) and outputs what I cannot return to String (I'm Java noob :)

Can someone give me advice on how to write a wrapper that converts a string to an OutputStream and outputs the result as a string after encoding it?

+1
source share
1 answer

To use this MimeUtility method, you must create a ByteArrayOutputStream that will accumulate the bytes recorded on it, which can then be restored. For example, to encode the string original :

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream encodedOut = MimeUtility.encode(baos, "quoted-printable"); encodedOut.write(original.getBytes()); String encoded = baos.toString(); 

The encodeText function from the same class will work with strings, but it produces Q-encoding, which is similar to quotation marks, but not quite the same :

 String encoded = MimeUtility.encodeText(original, null, "Q"); 
+4
source

Source: https://habr.com/ru/post/1213126/


All Articles