MimeBodyPart.restrictEncoding(part, part.getEncoding()); if (encoding != null) return MimeUtility.decode(is, encoding); else return is;
MimeBodyPart.restrictEncoding(part, part.getEncoding()); if (encoding != null) return MimeUtility.decode(is, encoding); else return is;
public static byte[] base64Decode(byte[] value) throws IOException, MessagingException { ByteArrayInputStream bais = new ByteArrayInputStream(value); InputStream b64is = MimeUtility.decode(bais, BASE64); byte[] tmp = new byte[value.length]; int n = b64is.read(tmp); byte[] res = new byte[n]; System.arraycopy(tmp, 0, res, 0, n); return res; }
public static byte[] fromReadableString(String str) throws Exception { byte[] b = str.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); InputStream printableis = MimeUtility.decode(bais, "quoted-printable"); byte[] tmp = new byte[b.length]; int n = printableis.read(tmp); byte[] res = new byte[n]; System.arraycopy(tmp, 0, res, 0, n); return res; }
public static byte[] fromReadableString(String str) throws Exception { byte[] b = str.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); InputStream printableis = MimeUtility.decode(bais, "quoted-printable"); byte[] tmp = new byte[b.length]; int n = printableis.read(tmp); byte[] res = new byte[n]; System.arraycopy(tmp, 0, res, 0, n); return res; }
private InputStream decodeIfNeeded(Attachment multipart, InputStream is) { String value = multipart.getHeader("Content-Transfer-Encoding"); if ("base64".equals(value) || "quoted-printable".equals(value)) { try { is = MimeUtility.decode(is, value); } catch (Exception ex) { LOG.warning("Problem with decoding an input stream, encoding : " + value); } } return is; }
/** * Decode string representation of Base64 encoded bytes. */ public static byte[] decode(String str) { try { byte[] bytes = str.getBytes("utf8"); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); InputStream is = MimeUtility.decode(bais, "Base64"); byte[] tmp = new byte[bytes.length]; int n = is.read(tmp); byte[] res = new byte[n]; System.arraycopy(tmp, 0, res, 0, n); return res; } catch (Exception x) { throw new RuntimeException("Base64 decoding failed", x); } }
private static String decode(String body, String encoding, String charset) { try { // Second time around. Apparently some are slipping through. charset = Parsing.stripQuotes(charset); // Sometimes the charset prefix slips through as well. Sigh. if (charset.startsWith("charset=")) charset = charset.substring("charset=".length()); return CharStreams.toString( new InputStreamReader(MimeUtility.decode(new ByteArrayInputStream(body.getBytes(charset)), encoding), charset)); } catch (UnsupportedEncodingException e) { // In this case, just return it as is and look it up later. log.warn("Encountered unknown encoding '{}'. Treating it as a raw string.", charset, e); return body; } catch (IOException e) { throw new RuntimeException(e); } catch (MessagingException e) { throw new RuntimeException(e); } }
/** * Decode binary data from String using base64. * * @see #encodeBytes(byte[]) */ public final static byte[] decodeBytes(String str) throws IOException { try { ByteArrayInputStream encodedStringStream = new ByteArrayInputStream(str.getBytes()); InputStream decoder = MimeUtility.decode(encodedStringStream, "base64"); ByteArrayOutputStream decodedByteStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (true) { int read = decoder.read(buffer); if (read == -1) { break; } decodedByteStream.write(buffer, 0, read); } decodedByteStream.flush(); return decodedByteStream.toByteArray(); } catch (MessagingException me) { throw new IOException("Cannot decode data."); } }
/** * Decode binary data from String using base64. * * @see #encodeBytes(byte[]) */ public final static byte[] decodeBytes(String str) throws IOException { try { ByteArrayInputStream encodedStringStream = new ByteArrayInputStream(str.getBytes()); InputStream decoder = MimeUtility.decode(encodedStringStream, "base64"); ByteArrayOutputStream decodedByteStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (true) { int read = decoder.read(buffer); if (read == -1) { break; } decodedByteStream.write(buffer, 0, read); } decodedByteStream.flush(); return decodedByteStream.toByteArray(); } catch (MessagingException me) { throw new IOException("Cannot decode data."); } }
private static byte[] readBodyAsBytes(String transferEncoding, ListIterator<String> iterator, String boundary, String charset, AtomicInteger errorCount, boolean gropeForTruncator) { byte[] bytes = new byte[0]; try { bytes = readBodyAsString(iterator, boundary, gropeForTruncator).getBytes(charset); } catch (UnsupportedEncodingException e) { log.warn("Could not decode body as string due to encoding, using default encoding.", e); bytes = readBodyAsString(iterator, boundary, gropeForTruncator).getBytes(); } // Decode if this is encoded as binary-to-text. if (null != transferEncoding) try { bytes = ByteStreams.toByteArray(MimeUtility.decode(new ByteArrayInputStream(bytes), transferEncoding)); } catch (MessagingException e) { log.error("Unable to decode message body, proceeding with raw bytes.", e); errorCount.incrementAndGet(); } catch (IOException e) { log.error("Unable to decode message body, proceeding with raw bytes.", e); errorCount.incrementAndGet(); } return bytes; }
/** * Change the data in this field to base64. * WARNING - This requires 64bit encoding found in javax.mail! * @param field The field containing the binary data to decode. * @param The string to decode using base64. */ public static void decodeFieldData(BaseField field, String string) { if ((string == null) || (string.length() == 0)) return; ByteArrayOutputStream baOut = new ByteArrayOutputStream(); OutputStreamWriter os = new OutputStreamWriter(baOut); try { os.write(string, 0, string.length()); os.flush(); } catch (IOException ex) { ex.printStackTrace(); } try { InputStream is = new ByteArrayInputStream(baOut.toByteArray()); InputStream enIn = javax.mail.internet.MimeUtility.decode(is, BINARYENCODING); DataInputStream daIn = new DataInputStream(enIn); field.read(daIn, false); } catch (javax.mail.MessagingException ex) { ex.printStackTrace(); } } }
/** * Change the data in this field to base64. * WARNING - This requires 64bit encoding found in javax.mail! * @param field The field containing the binary data to decode. * @param The string to decode using base64. */ public static void decodeFieldData(BaseField field, String string) { if ((string == null) || (string.length() == 0)) return; ByteArrayOutputStream baOut = new ByteArrayOutputStream(); OutputStreamWriter os = new OutputStreamWriter(baOut); try { os.write(string, 0, string.length()); os.flush(); } catch (IOException ex) { ex.printStackTrace(); } try { InputStream is = new ByteArrayInputStream(baOut.toByteArray()); InputStream enIn = javax.mail.internet.MimeUtility.decode(is, BINARYENCODING); DataInputStream daIn = new DataInputStream(enIn); field.read(daIn, false); } catch (javax.mail.MessagingException ex) { ex.printStackTrace(); } } }
/** * Decode base64 encoded String * * @param b64string * base64 String * @return reader the BufferedReader which holds the decoded base64 text * @throws MessagingException * get thrown when an error was detected while trying to decode * the String */ public static BufferedReader decode(String b64string) throws MessagingException { return new BufferedReader(new InputStreamReader(MimeUtility.decode( new ByteArrayInputStream(b64string.getBytes()), "base64"))); }
return MimeUtility.decode(stream, encoding);
public InputStream getInputStream() throws IOException { try { InputStream stream; if (part instanceof MimeMessage) { stream = ((MimeMessage) part).getContentStream(); } else if (part instanceof MimeBodyPart) { stream = ((MimeBodyPart) part).getContentStream(); } else { throw new MessagingException("Unknown part"); } String encoding = part.getEncoding(); return encoding == null ? stream : MimeUtility.decode(stream, encoding); } catch (MessagingException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } }
if(encoding.toLowerCase().contains(MimeStandard.TransferEncodingQuoted)) { try { InputStream decoded = MimeUtility.decode(mime.getInputStream(),MimeStandard.TransferEncodingQuoted); body = IOUtils.toByteArray(decoded);
return MimeUtility.decode(is, encoding); else return is;
MimeBodyPart.restrictEncoding(part, part.getEncoding()); if (encoding != null) return MimeUtility.decode(is, encoding); else return is;
MimeBodyPart.restrictEncoding(part, part.getEncoding()); if (encoding != null) return MimeUtility.decode(is, encoding); else return is;