@Test public void testNonAsciiEncoding() throws Exception { DataSource ds = new ByteArrayDataSource(utf16beBytes, "text/plain; charset=utf-16be"); String en = MimeUtility.getEncoding(ds); assertEquals("non-ASCII encoding", "base64", en); }
public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; if (ds instanceof EncodingAware) { encoding = ((EncodingAware)ds).getEncoding(); if (encoding != null) return encoding; } try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); boolean isText = cType.match("text/*"); int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; break; case MOSTLY_ASCII: if (isText && nonAsciiCharset(cType)) encoding = "base64"; else encoding = "quoted-printable"; break; default: encoding = "base64"; break; } } catch (Exception ex) { return "base64"; } finally { try { if (is != null) is.close(); } catch (IOException ioex) { } } return encoding; }
MimeUtility { public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; if (ds instanceof EncodingAware) { encoding = ((EncodingAware)ds).getEncoding(); if (encoding != null) return encoding; } try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); boolean isText = cType.match("text/*"); int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; break; case MOSTLY_ASCII: if (isText && nonAsciiCharset(cType)) encoding = "base64"; else encoding = "quoted-printable"; break; default: encoding = "base64"; break; } } catch (Exception ex) { return "base64"; } finally { try { if (is != null) is.close(); } catch (IOException ioex) { } } return encoding; } }
MimeUtility { public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; if (ds instanceof EncodingAware) { encoding = ((EncodingAware)ds).getEncoding(); if (encoding != null) return encoding; } try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); boolean isText = cType.match("text/*"); int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; break; case MOSTLY_ASCII: if (isText && nonAsciiCharset(cType)) encoding = "base64"; else encoding = "quoted-printable"; break; default: encoding = "base64"; break; } } catch (Exception ex) { return "base64"; } finally { try { if (is != null) is.close(); } catch (IOException ioex) { } } return encoding; } private  MimeUtility(); }
MimeUtility { public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; if (ds instanceof EncodingAware) { encoding = ((EncodingAware)ds).getEncoding(); if (encoding != null) return encoding; } try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); boolean isText = cType.match("text/*"); int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; break; case MOSTLY_ASCII: if (isText && nonAsciiCharset(cType)) encoding = "base64"; else encoding = "quoted-printable"; break; default: encoding = "base64"; break; } } catch (Exception ex) { return "base64"; } finally { try { if (is != null) is.close(); } catch (IOException ioex) { } } return encoding; } private  MimeUtility(); static String getEncoding(DataSource ds); static String getEncoding(DataHandler dh); static InputStream decode(InputStream is, String encoding); static OutputStream encode(OutputStream os, String encoding); static OutputStream encode(OutputStream os, String encoding,
                                      String filename); static String encodeText(String text); static String encodeText(String text, String charset,
				    String encoding); static String decodeText(String etext); static String encodeWord(String word); static String encodeWord(String word, String charset, 
				    String encoding); static String decodeWord(String eword); static String quote(String word, String specials); static String fold(int used, String s); static String unfold(String s); static String javaCharset(String charset); static String mimeCharset(String charset); static String getDefaultJavaCharset(); }
MimeUtility { public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; if (ds instanceof EncodingAware) { encoding = ((EncodingAware)ds).getEncoding(); if (encoding != null) return encoding; } try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); boolean isText = cType.match("text/*"); int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; break; case MOSTLY_ASCII: if (isText && nonAsciiCharset(cType)) encoding = "base64"; else encoding = "quoted-printable"; break; default: encoding = "base64"; break; } } catch (Exception ex) { return "base64"; } finally { try { if (is != null) is.close(); } catch (IOException ioex) { } } return encoding; } private  MimeUtility(); static String getEncoding(DataSource ds); static String getEncoding(DataHandler dh); static InputStream decode(InputStream is, String encoding); static OutputStream encode(OutputStream os, String encoding); static OutputStream encode(OutputStream os, String encoding,
                                      String filename); static String encodeText(String text); static String encodeText(String text, String charset,
				    String encoding); static String decodeText(String etext); static String encodeWord(String word); static String encodeWord(String word, String charset, 
				    String encoding); static String decodeWord(String eword); static String quote(String word, String specials); static String fold(int used, String s); static String unfold(String s); static String javaCharset(String charset); static String mimeCharset(String charset); static String getDefaultJavaCharset(); static final int ALL; }