List of usage examples for java.nio.charset CodingErrorAction REPORT
CodingErrorAction REPORT
To view the source code for java.nio.charset CodingErrorAction REPORT.
Click Source Link
From source file:org.nuxeo.ecm.platform.filemanager.service.extension.NoteImporter.java
protected static String guessEncoding(Blob blob) throws IOException { // encoding already known? if (blob.getEncoding() != null) { return null; }//from w w w .j a v a 2 s .c om // bad mime type? String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { // not a text file, we shouldn't be in the Note importer 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) { // charset specified in MIME type 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 { // charset detected from the actual bytes CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } // now convert the string according to the charset, and fallback on others if not possible 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') { // remove BOM cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { // illegal charset } catch (CharacterCodingException e) { // could not decode } } // nothing worked, use platform return null; }
From source file:org.rapla.rest.gwtjsonrpc.server.JsonServlet.java
private String readBody(final ActiveCall call) throws IOException { if (!isBodyJson(call)) { throw new JsonParseException("Invalid Request Content-Type"); }/*from ww w.j av a2s . c om*/ if (!isBodyUTF8(call)) { throw new JsonParseException("Invalid Request Character-Encoding"); } final int len = call.httpRequest.getContentLength(); if (len < 0) { throw new JsonParseException("Invalid Request Content-Length"); } if (len == 0) { throw new JsonParseException("Invalid Request POST Body Required"); } if (len > maxRequestSize()) { throw new JsonParseException("Invalid Request POST Body Too Large"); } final InputStream in = call.httpRequest.getInputStream(); if (in == null) { throw new JsonParseException("Invalid Request POST Body Required"); } try { final byte[] body = new byte[len]; int off = 0; while (off < len) { final int n = in.read(body, off, len - off); if (n <= 0) { throw new JsonParseException("Invalid Request Incomplete Body"); } off += n; } final CharsetDecoder d = Charset.forName(JsonConstants.JSON_ENC).newDecoder(); d.onMalformedInput(CodingErrorAction.REPORT); d.onUnmappableCharacter(CodingErrorAction.REPORT); try { ByteBuffer wrap = ByteBuffer.wrap(body); CharBuffer decode = d.decode(wrap); return decode.toString(); } catch (CharacterCodingException e) { throw new JsonParseException("Invalid Request Not UTF-8", e); } } finally { in.close(); } }
From source file:org.rapla.server.jsonrpc.JsonServlet.java
private String readBody(final ActiveCall call) throws IOException { if (!isBodyJson(call)) { throw new JsonParseException("Invalid Request Content-Type"); }/*from w w w. j a v a2s . c o m*/ if (!isBodyUTF8(call)) { throw new JsonParseException("Invalid Request Character-Encoding"); } final int len = call.httpRequest.getContentLength(); if (len < 0) { throw new JsonParseException("Invalid Request Content-Length"); } if (len == 0) { throw new JsonParseException("Invalid Request POST Body Required"); } if (len > maxRequestSize()) { throw new JsonParseException("Invalid Request POST Body Too Large"); } final InputStream in = call.httpRequest.getInputStream(); if (in == null) { throw new JsonParseException("Invalid Request POST Body Required"); } try { final byte[] body = new byte[len]; int off = 0; while (off < len) { final int n = in.read(body, off, len - off); if (n <= 0) { throw new JsonParseException("Invalid Request Incomplete Body"); } off += n; } final CharsetDecoder d = Charset.forName(JsonConstants.JSON_ENC).newDecoder(); d.onMalformedInput(CodingErrorAction.REPORT); d.onUnmappableCharacter(CodingErrorAction.REPORT); try { return d.decode(ByteBuffer.wrap(body)).toString(); } catch (CharacterCodingException e) { throw new JsonParseException("Invalid Request Not UTF-8", e); } } finally { in.close(); } }
From source file:org.sonar.scanner.scan.filesystem.CharsetValidationTest.java
private byte[] encode(String txt, Charset charset) throws CharacterCodingException { CharsetEncoder encoder = charset.newEncoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); ByteBuffer encoded = encoder.encode(CharBuffer.wrap(txt)); byte[] b = new byte[encoded.remaining()]; encoded.get(b);/* ww w .j av a2s .c o m*/ return b; }