private static Charset getCharset(String contentType) { // FIXME the spec default is ISO-8859-1 Charset encoding = Charset.forName("UTF-8"); // NOI18N if (contentType != null) { String[] parts = contentType.trim().split(";"); // NOI18N for (String p : parts) { String upper = p.toUpperCase(Locale.ENGLISH); if (upper.startsWith("CHARSET")) { // NOI18N int index = upper.indexOf("=", 7); // NOI18N if (index > 0 && index < upper.length() -1) { try { encoding = Charset.forName(upper.substring(index + 1).trim()); } catch (UnsupportedCharsetException ex) { // noop using the UTF-8 } } break; } } } return encoding; }
Impl(String name, String title, String MIMEType, Reader r) { this.name = name; this.title = title; this.MIMEType = MIMEType; this.r = r; this.readerSource = null; this.w = null; this.file = null; if (r instanceof InputStreamReader) { try { encoding = Charset.forName(((InputStreamReader) r).getEncoding()); } catch (UnsupportedCharsetException e) { // ignore, encoding will be null } } }
/** * 开始解析服务器返回参数 */ private void sendLogResponse(Response response) throws IOException { String rBody = ""; if (response != null && response.body() != null) { BufferedSource source = response.body().source(); source.request(Long.MAX_VALUE); // Buffer the entire body. Buffer buffer = source.buffer(); Charset charset = Charset.forName("UTF-8"); MediaType contentType = response.body().contentType(); if (contentType != null) { try { charset = contentType.charset(Charset.forName("UTF-8")); } catch (UnsupportedCharsetException e) { e.printStackTrace(); } } rBody = buffer.clone().readString(charset); } LogUtils.i("接收:" + rBody.toString()); }
static Charset findCharset(String alias) throws UnsupportedEncodingException { try { Charset cs = charsetsByAlias.get(alias); if (cs == null) { cs = Charset.forName(alias); Charset oldCs = charsetsByAlias.putIfAbsent(alias, cs); if (oldCs != null) { // if the previous value was recently set by another thread we return it instead of value we found here cs = oldCs; } } return cs; // We re-throw these runtimes for compatibility with java.io } catch (UnsupportedCharsetException uce) { throw new UnsupportedEncodingException(alias); } catch (IllegalCharsetNameException icne) { throw new UnsupportedEncodingException(alias); } catch (IllegalArgumentException iae) { throw new UnsupportedEncodingException(alias); } }
static Charset findCharset(String alias) throws UnsupportedEncodingException { try { Charset cs = charsetsByAlias.get(alias); if (cs == null) { cs = Charset.forName(alias); charsetsByAlias.putIfAbsent(alias, cs); } return cs; // We re-throw these runtimes for compatibility with java.io } catch (UnsupportedCharsetException uce) { throw new UnsupportedEncodingException(alias); } catch (IllegalCharsetNameException icne) { throw new UnsupportedEncodingException(alias); } catch (IllegalArgumentException iae) { throw new UnsupportedEncodingException(alias); } }
@Override public void setCharacterEncoding(final String env) throws UnsupportedEncodingException { if (readStarted) { return; } try { characterEncoding = Charset.forName(env); final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalServletPathMatch().getServletChain().getManagedServlet(); final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange); if (parser != null) { parser.setCharacterEncoding(env); } } catch (UnsupportedCharsetException e) { throw new UnsupportedEncodingException(); } }
/** * Get the entity content as a String, using the provided default character set * if none is found in the entity. * If defaultCharset is null, the default "ISO-8859-1" is used. * * @param entity must not be null * @param defaultCharset character set to be applied if none found in the entity, * or if the entity provided charset is invalid or not available. * @return the entity content as a String. May be null if * {@link HttpEntity#getContent()} is null. * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in * this instance of the Java virtual machine and no defaultCharset is provided. */ public static String toString( final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException { Args.notNull(entity, "Entity"); ru.radiomayak.http.entity.ContentType contentType = null; try { contentType = ru.radiomayak.http.entity.ContentType.get(entity); } catch (final UnsupportedCharsetException ex) { if (defaultCharset == null) { throw new UnsupportedEncodingException(ex.getMessage()); } } if (contentType != null) { if (contentType.getCharset() == null) { contentType = contentType.withCharset(defaultCharset); } } else { contentType = ru.radiomayak.http.entity.ContentType.DEFAULT_TEXT.withCharset(defaultCharset); } return toString(entity, contentType); }
/** * Creates a StringEntity with the specified content and content type. * * @param string content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case the default * MIME type {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine * @since 4.2 */ public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException { super(); Args.notNull(string, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } try { this.content = string.getBytes(charset.name()); } catch (final UnsupportedEncodingException ex) { // should never happen throw new UnsupportedCharsetException(charset.name()); } if (contentType != null) { setContentType(contentType.toString()); } }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); ResponseBody body = response.body(); long length = body.contentLength(); BufferedSource source = body.source(); source.request(Long.MAX_VALUE); Buffer buffer = source.buffer(); Charset charset = Charset.forName("UTF-8"); MediaType type = body.contentType(); if (type != null) { try { charset = type.charset(charset); } catch (UnsupportedCharsetException e) { e.printStackTrace(); return response; } } if (length != 0) { System.out.println("shenhua sout:--------------------------------------------开始打印返回数据----------------------------------------------------"); System.out.println("shenhua sout:" + buffer.clone().readString(charset)); } return response; }
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset, int alignmentForEncoding) { try { ctb = cache.getCharToByteConverter(codeset.getName()); if (ctb == null) { Charset tmpCharset = Charset.forName(codeset.getName()); ctb = tmpCharset.newEncoder(); cache.setConverter(codeset.getName(), ctb); } } catch(IllegalCharsetNameException icne) { // This can only happen if one of our Entries has // an invalid name. throw wrapper.invalidCtbConverterName(icne,codeset.getName()); } catch(UnsupportedCharsetException ucne) { // This can only happen if one of our Entries has // an unsupported name. throw wrapper.invalidCtbConverterName(ucne,codeset.getName()); } this.codeset = codeset; alignment = alignmentForEncoding; }
@Override protected Charset detectCharsetImpl(byte[] buffer) throws Exception { CharsetDetector detector = new CharsetDetector(); detector.setText(buffer); CharsetMatch match = detector.detect(); if(match != null && match.getConfidence() > threshold) { try { return Charset.forName(match.getName()); } catch(UnsupportedCharsetException e) { logger.info("Charset detected as " + match.getName() + " but the JVM does not support this, detection skipped"); } } return null; }
private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) { Charset charset = null; for (final NameValuePair param: params) { if (param.getName().equalsIgnoreCase("charset")) { final String s = param.getValue(); if (!TextUtils.isBlank(s)) { try { charset = Charset.forName(s); } catch (final UnsupportedCharsetException ex) { if (strict) { throw ex; } } } break; } } return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null); }
/** * Get the entity content as a String, using the provided default character set * if none is found in the entity. * If defaultCharset is null, the default "ISO-8859-1" is used. * * @param entity must not be null * @param defaultCharset character set to be applied if none found in the entity, * or if the entity provided charset is invalid or not available. * @return the entity content as a String. May be null if * {@link HttpEntity#getContent()} is null. * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in * this instance of the Java virtual machine and no defaultCharset is provided. */ public static String toString( final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException { Args.notNull(entity, "Entity"); ContentType contentType = null; try { contentType = ContentType.get(entity); } catch (final UnsupportedCharsetException ex) { if (defaultCharset == null) { throw new UnsupportedEncodingException(ex.getMessage()); } } if (contentType != null) { if (contentType.getCharset() == null) { contentType = contentType.withCharset(defaultCharset); } } else { contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset); } return toString(entity, contentType); }
/** * Convert stream to ByteArrayInputStream by given character set. * @param charset target character set. * @param file a file that contains no BOM head content. * @return a ByteArrayInputStream contains BOM heads and bytes in original * stream * @throws IOException I/O operation failed or unsupported character set. */ public static InputStream bomStream(String charset, String file) throws IOException { String localCharset = charset; if (charset.equals("UTF-16") || charset.equals("UTF-32")) { localCharset += ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? "BE" : "LE"; } if (!bom.containsKey(localCharset)) throw new UnsupportedCharsetException("Charset:" + localCharset); byte[] content = Files.readAllLines(Paths.get(file)).stream(). collect(Collectors.joining()).getBytes(localCharset); byte[] head = bom.get(localCharset); ByteBuffer bb = ByteBuffer.allocate(content.length + head.length); bb.put(head); bb.put(content); return new ByteArrayInputStream(bb.array()); }
public void setOutput(OutputStream os, String encoding) throws IOException, IllegalArgumentException, IllegalStateException { if (os == null) { throw new IllegalArgumentException(); } try { charset = Charset.forName(encoding).newEncoder(); } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { throw (UnsupportedEncodingException) (new UnsupportedEncodingException(encoding).initCause(e)); } out = os; }
/** * Tries to get {@link org.eclipse.core.resources.IWorkspaceRoot#getDefaultCharset() workspace default charset}. In * case of errors, will return {@link StandardCharsets#UTF_8} * * @return workspace charset or default one */ private Charset getWorkspaceCharsetOrUtf8() { try { return Charset.forName(super.getEncoding()); } catch (CoreException | UnsupportedCharsetException e) { LOGGER.error("Cannot obtain workspace charset", e); LOGGER.info("Exceptions when obtaining workspace default charset, fall back to the " + StandardCharsets.UTF_8.name(), e); return StandardCharsets.UTF_8; } }
@NonNull static Map<String,Object> createBaseCoSProperties( @NonNull final JavaActionProvider.Context ctx) { final String command = ctx.getCommand(); final Project project = ctx.getProject(); final UpdateHelper updateHelper = ctx.getUpdateHelper(); final PropertyEvaluator evaluator = ctx.getPropertyEvaluator(); final JavaPlatform jp = ctx.getActiveJavaPlatform(); final Map<String, Object> execProperties = new HashMap<>(); execProperties.put("nb.internal.action.name", command); copyMultiValue(evaluator, execProperties, ProjectProperties.RUN_JVM_ARGS); prepareWorkDir(updateHelper, evaluator, execProperties); execProperties.put(JavaRunner.PROP_PLATFORM, jp); execProperties.put(JavaRunner.PROP_PROJECT_NAME, ProjectUtils.getInformation(project).getDisplayName()); String runtimeEnc = evaluator.getProperty(ProjectProperties.RUNTIME_ENCODING); if (runtimeEnc != null) { try { Charset runtimeChs = Charset.forName(runtimeEnc); execProperties.put(JavaRunner.PROP_RUNTIME_ENCODING, runtimeChs); //NOI18N } catch (IllegalCharsetNameException ichsn) { LOG.log(Level.WARNING, "Illegal charset name: {0}", runtimeEnc); //NOI18N } catch (UnsupportedCharsetException uchs) { LOG.log(Level.WARNING, "Unsupported charset : {0}", runtimeEnc); //NOI18N } } Optional.ofNullable(evaluator.getProperty("java.failonerror")) //NOI18N .map((val) -> Boolean.valueOf(val)) .ifPresent((b) -> execProperties.put("java.failonerror", b)); //NOI18N return execProperties; }
private Charset getCharset() { try { return Charset.forName(encoding); } catch (UnsupportedCharsetException exception) { Charset defaultCharset = Charset.defaultCharset(); return defaultCharset; } }
@Override public T fromString(String str) throws IOException { try { byte[] bytes = Base64.decodeBase64(str.getBytes("UTF-8")); inBuf.reset(bytes, bytes.length); T restored = deserializer.deserialize(null); return restored; } catch (UnsupportedCharsetException ex) { throw new IOException(ex.toString()); } }
@Test(expected = UnsupportedCharsetException.class) public void invalidFileEncoding() throws Throwable { Map<String, Object> cfg = new HashMap<String, Object>() {{ put(TextFileReader.FILE_READER_TEXT_FIELD_NAME_VALUE, FIELD_NAME_VALUE); put(TextFileReader.FILE_READER_TEXT_ENCODING, "invalid_charset"); }}; getReader(fs, dataFile, cfg); }
@Test(expected = UnsupportedCharsetException.class) public void invalidFileEncoding() throws Throwable { Map<String, Object> cfg = new HashMap<String, Object>() {{ put(DelimitedTextFileReader.FILE_READER_DELIMITED_TOKEN, ","); put(DelimitedTextFileReader.FILE_READER_DELIMITED_HEADER, "true"); put(DelimitedTextFileReader.FILE_READER_DELIMITED_ENCODING, "invalid_charset"); put(AgnosticFileReader.FILE_READER_AGNOSTIC_EXTENSIONS_DELIMITED, getFileExtension()); }}; getReader(fs, dataFile, cfg); }
@Test(expected = UnsupportedCharsetException.class) public void invalidFileEncoding() throws Throwable { Map<String, Object> cfg = new HashMap<String, Object>() {{ put(DelimitedTextFileReader.FILE_READER_DELIMITED_TOKEN, ","); put(DelimitedTextFileReader.FILE_READER_DELIMITED_HEADER, "true"); put(DelimitedTextFileReader.FILE_READER_DELIMITED_ENCODING, "invalid_charset"); }}; getReader(fs, dataFile, cfg); }
private void addLiteral(byte[] bytes, StringBuffer response) { response.append('{'); response.append(bytes.length); response.append('}'); response.append("\r\n"); String encodedBytes = null; try { encodedBytes = new String(bytes, Charset.forName(EIGHT_BIT_ENCODING)); } catch (UnsupportedCharsetException ex) { encodedBytes = new String(bytes); } response.append(encodedBytes); }
/** * Returns the charset specified in the Content-Type of this header, or falls back to * UTF-8 if none can be found. * @param response network response * @return charset from header */ private Charset getResponseCharset(NetworkResponse response) { String charset = HttpHeaderParser.parseCharset(response.headers); try { return Charset.forName(charset); } catch (UnsupportedCharsetException e) { return Charset.forName("UTF-8"); } }
public void testGetCharset_unsupportedCharset() { MediaType mediaType = MediaType.parse( "text/plain; charset=utf-wtf"); try { mediaType.charset(); fail(); } catch (UnsupportedCharsetException expected) {} }
/** * {@inheritDoc} */ @Override public List<Event> getEvents(HttpServletRequest request) throws Exception { BufferedReader reader = request.getReader(); String charset = request.getCharacterEncoding(); //UTF-8 is default for JSON. If no charset is specified, UTF-8 is to //be assumed. if (charset == null) { LOG.debug("Charset is null, default charset of UTF-8 will be used."); charset = "UTF-8"; } else if (!(charset.equalsIgnoreCase("utf-8") || charset.equalsIgnoreCase("utf-16") || charset.equalsIgnoreCase("utf-32"))) { LOG.error("Unsupported character set in request {}. " + "JSON handler supports UTF-8, " + "UTF-16 and UTF-32 only.", charset); throw new UnsupportedCharsetException("JSON handler supports UTF-8, " + "UTF-16 and UTF-32 only."); } /* * Gson throws Exception if the data is not parseable to JSON. * Need not catch it since the source will catch it and return error. */ List<Event> eventList = new ArrayList<Event>(0); try { eventList = gson.fromJson(reader, listType); } catch (JsonSyntaxException ex) { throw new HTTPBadRequestException("Request has invalid JSON Syntax.", ex); } for (Event e : eventList) { ((JSONEvent) e).setCharset(charset); } return getSimpleEvents(eventList); }
@Test(expected = UnsupportedCharsetException.class) public void testError() throws Exception { String json = "[{\"headers\" : {\"a\": \"b\"},\"body\": \"random_body\"}]"; HttpServletRequest req = new FlumeHttpServletRequestWrapper(json, "ISO-8859-1"); handler.getEvents(req); Assert.fail(); }
private static boolean isValidForCharset(String id, String charsetName) { CharsetEncoder encoder = null; try { Charset charset = Charset.forName(charsetName); encoder = charset.newEncoder(); } catch (UnsupportedCharsetException e) { Log.w("Unsupported charset: " + charsetName); } if (encoder != null && !encoder.canEncode(id)) { Log.v("Invalid id (contains invalid characters): " + id); return false; } return true; }
/** * Create a decoder for the specified charset. */ public B2CConverter(String charset) throws IOException { try { decoder = Charset.forName(charset).newDecoder(); } catch (UnsupportedCharsetException e) { throw new UnsupportedEncodingException(charset); } byte[] left = new byte[4]; leftovers = ByteBuffer.wrap(left); }
/** * Create an encoder for the specified charset. */ public C2BConverter(String charset) throws IOException { try { encoder = Charset.forName(charset).newEncoder(); } catch (UnsupportedCharsetException e) { throw new UnsupportedEncodingException(charset); } }
/** * Parses textual representation of <code>Content-Type</code> value. * * @param s text * @return content type * @throws ParseException if the given text does not represent a valid * <code>Content-Type</code> value. */ public static ContentType parse( final String s) throws ParseException, UnsupportedCharsetException { if (s == null) { throw new IllegalArgumentException("Content type may not be null"); } HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null); if (elements.length > 0) { return create(elements[0]); } else { throw new ParseException("Invalid content type: " + s); } }
/** * Extracts <code>Content-Type</code> value from {@link HttpEntity} exactly as * specified by the <code>Content-Type</code> header of the entity. Returns <code>null</code> * if not specified. * * @param entity HTTP entity * @return content type * @throws ParseException if the given text does not represent a valid * <code>Content-Type</code> value. */ public static ContentType get( final HttpEntity entity) throws ParseException, UnsupportedCharsetException { if (entity == null) { return null; } Header header = entity.getContentType(); if (header != null) { HeaderElement[] elements = header.getElements(); if (elements.length > 0) { return create(elements[0]); } } return null; }
/** * @since 4.3 */ public StringBody(final String text, final ContentType contentType) { super(contentType); Args.notNull(text, "Text"); final Charset charset = contentType.getCharset(); final String csname = charset != null ? charset.name() : Consts.ASCII.name(); try { this.content = text.getBytes(csname); } catch (final UnsupportedEncodingException ex) { // Should never happen throw new UnsupportedCharsetException(csname); } }
ContentType( final String mimeType, final NameValuePair[] params) throws UnsupportedCharsetException { this.mimeType = mimeType; this.params = params; final String s = getParameter("charset"); this.charset = !TextUtils.isBlank(s) ? Charset.forName(s) : null; }