@Test public void test() throws Exception { Blob blob; blob = Blobs.createBlob(new byte[0], null, "somecharset"); assertNull(NoteImporter.guessEncoding(blob)); blob = Blobs.createBlob(new byte[0]); assertNull(NoteImporter.guessEncoding(blob)); blob = Blobs.createBlob(new byte[0], "foo"); assertNull(NoteImporter.guessEncoding(blob)); blob = Blobs.createBlob(new byte[] { (byte) 0xe9 }, "text/plain; charset=iso-8859-1; x=y"); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = Blobs.createBlob(new byte[] { (byte) 0xe9 }, "text/plain"); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = Blobs.createBlob(new byte[] { (byte) 0xc3, (byte) 0xa9 }, "text/plain"); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = Blobs.createBlob(new byte[] { (byte) 0xff, (byte) 0xfe, (byte) 0xe9, (byte) 0x00 }, "text/plain"); String s = NoteImporter.guessEncoding(blob); assertEquals("\u00e9", s); assertEquals("text/plain", blob.getMimeType()); blob = Blobs.createBlob(new byte[] { (byte) 0xe9 }, "text/plain; charset=utf-8; x=y"); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); }
protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; } }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  @Override String getDefaultDocType(); @Override boolean isOverwriteByTitle(); @Override boolean updateDocumentIfPossible(DocumentModel doc, Blob content); @Override void updateDocument(DocumentModel doc, Blob content); }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  @Override String getDefaultDocType(); @Override boolean isOverwriteByTitle(); @Override boolean updateDocumentIfPossible(DocumentModel doc, Blob content); @Override void updateDocument(DocumentModel doc, Blob content);  }