Java Code Examples for java.nio.charset.Charset

The following are top voted examples for showing how to use java.nio.charset.Charset. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: safecharge-java   File: SafechargeRequestExecutor.java   View source code 9 votes vote down vote up
/**
 * Sends a POST request to the service at {@code serviceUrl} with a payload of {@code request}.
 * The request type is determined by the {@code headers} param.
 *
 * @param request    A {@link String} representation of a request object. Can be JSON object, form data, etc...
 * @param serviceUrl The service URL to sent the request to
 * @param headers    An array of {@link Header} objects, used to determine the request type
 * @return {@link String} response from the service (representing JSON object)
 * @throws IOException if the connection is interrupted or the response is unparsable
 */
public String executeRequest(String request, String serviceUrl, Header[] headers) throws IOException {
    HttpPost httpPost = new HttpPost(serviceUrl);
    httpPost.setHeaders(headers);
    httpPost.setEntity(new StringEntity(request, Charset.forName("UTF-8")));

    if (logger.isDebugEnabled()) {
        logger.debug("Sent " + request);
    }

    HttpResponse response = httpClient.execute(httpPost);

    String responseJSON = EntityUtils.toString(response.getEntity(), UTF8_CHARSET);
    if (logger.isDebugEnabled()) {
        logger.debug("Received " + responseJSON);
    }
    return responseJSON;
}
 
Example 2
Project: lams   File: IOUtils.java   View source code 8 votes vote down vote up
/**
 * Writes the <code>toString()</code> value of each item in a collection to
 * an <code>OutputStream</code> line by line, using the specified character
 * encoding and the specified line ending.
 *
 * @param lines  the lines to write, null entries produce blank lines
 * @param lineEnding  the line separator to use, null is system default
 * @param output  the <code>OutputStream</code> to write to, not null, not closed
 * @param encoding  the encoding to use, null means platform default
 * @throws NullPointerException if the output is null
 * @throws IOException if an I/O error occurs
 * @since 2.3
 */
public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
        throws IOException {
    if (lines == null) {
        return;
    }
    if (lineEnding == null) {
        lineEnding = LINE_SEPARATOR;
    }
    Charset cs = Charsets.toCharset(encoding);
    for (Object line : lines) {
        if (line != null) {
            output.write(line.toString().getBytes(cs));
        }
        output.write(lineEnding.getBytes(cs));
    }
}
 
Example 3
Project: springboot-shiro-cas-mybatis   File: ShibbolethCompatiblePersistentIdGenerator.java   View source code 7 votes vote down vote up
@Override
public String generate(final Principal principal, final Service service) {
    try {
        final MessageDigest md = MessageDigest.getInstance("SHA");
        final Charset charset = Charset.defaultCharset();
        md.update(service.getId().getBytes(charset));
        md.update(CONST_SEPARATOR);
        md.update(principal.getId().getBytes(charset));
        md.update(CONST_SEPARATOR);

        final String result = CompressionUtils.encodeBase64(md.digest(convertSaltToByteArray()));
        return result.replaceAll(System.getProperty("line.separator"), "");
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Project: incubator-netbeans   File: MessagesHandlerTest.java   View source code 6 votes vote down vote up
public void testMessageRepeat() throws Exception {
    clearWorkDir();
    System.setProperty("netbeans.user", getWorkDirPath());
    File logs = new File(getWorkDir(), "logs");
    logs.mkdirs();
    
    MessagesHandler mh = new MessagesHandler(logs, 2, 10000);
    
    mh.publish(new LogRecord(Level.INFO, "Hi"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.flush();
    File log = logs.listFiles()[0];
    List<String> allLines = Files.readAllLines(log.toPath(), Charset.defaultCharset());
    assertTrue(allLines.get(allLines.size() - 1), allLines.get(allLines.size() - 1).endsWith(MessagesHandler.getRepeatingMessage(1, 1)));
    assertTrue(allLines.get(allLines.size() - 2), allLines.get(allLines.size() - 2).endsWith("Hello"));
    assertTrue(allLines.get(allLines.size() - 3), allLines.get(allLines.size() - 3).endsWith("Hi"));
    
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.flush();
    allLines = Files.readAllLines(log.toPath(), Charset.defaultCharset());
    assertTrue(allLines.get(allLines.size() - 1), allLines.get(allLines.size() - 1).endsWith(MessagesHandler.getRepeatingMessage(2, 2)));
}
 
Example 5
Project: hadoop   File: ApplicationCLI.java   View source code 6 votes vote down vote up
/**
 * Lists the containers matching the given application attempts
 * 
 * @param appAttemptId
 * @throws YarnException
 * @throws IOException
 */
private void listContainers(String appAttemptId) throws YarnException,
    IOException {
  PrintWriter writer = new PrintWriter(
      new OutputStreamWriter(sysout, Charset.forName("UTF-8")));

  List<ContainerReport> appsReport = client
      .getContainers(ConverterUtils.toApplicationAttemptId(appAttemptId));
  writer.println("Total number of containers " + ":" + appsReport.size());
  writer.printf(CONTAINER_PATTERN, "Container-Id", "Start Time",
      "Finish Time", "State", "Host", "Node Http Address", "LOG-URL");
  for (ContainerReport containerReport : appsReport) {
    writer.printf(
        CONTAINER_PATTERN,
        containerReport.getContainerId(),
        Times.format(containerReport.getCreationTime()),
        Times.format(containerReport.getFinishTime()),      
        containerReport.getContainerState(), containerReport
            .getAssignedNode(), containerReport.getNodeHttpAddress() == null
                ? "N/A" : containerReport.getNodeHttpAddress(),
        containerReport.getLogUrl());
  }
  writer.flush();
}
 
Example 6
Project: springboot-shiro-cas-mybatis   File: EncryptedMapDecorator.java   View source code 6 votes vote down vote up
/**
 * Decrypt the value.
 *
 * @param value the value
 * @param hashedKey the hashed key
 * @return the string
 */
protected String decrypt(final String value, final String hashedKey) {
    if (value == null) {
        return null;
    }

    try {
        final Cipher cipher = getCipherObject();
        final byte[] ivCiphertext = CompressionUtils.decodeBase64ToByteArray(value);
        final int ivSize = byte2int(Arrays.copyOfRange(ivCiphertext, 0, INTEGER_LEN));
        final byte[] ivValue = Arrays.copyOfRange(ivCiphertext, INTEGER_LEN, (INTEGER_LEN + ivSize));
        final byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, INTEGER_LEN + ivSize, ivCiphertext.length);
        final IvParameterSpec ivSpec = new IvParameterSpec(ivValue);
        
        cipher.init(Cipher.DECRYPT_MODE, this.key, ivSpec);

        final byte[] plaintext = cipher.doFinal(ciphertext);

        return new String(plaintext, Charset.defaultCharset());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Project: openjdk-jdk10   File: HttpInputStreamTest.java   View source code 6 votes vote down vote up
/**
 * Examine the response headers to figure out the charset used to
 * encode the body content.
 * If the content type is not textual, returns an empty Optional.
 * Otherwise, returns the body content's charset, defaulting to
 * ISO-8859-1 if none is explicitly specified.
 * @param headers The response headers.
 * @return The charset to use for decoding the response body, if
 *         the response body content is text/...
 */
public static Optional<Charset> getCharset(HttpHeaders headers) {
    Optional<String> contentType = headers.firstValue("Content-Type");
    Optional<Charset> charset = Optional.empty();
    if (contentType.isPresent()) {
        final String[] values = contentType.get().split(";");
        if (values[0].startsWith("text/")) {
            charset = Optional.of(Stream.of(values)
                .map(x -> x.toLowerCase(Locale.ROOT))
                .map(String::trim)
                .filter(x -> x.startsWith("charset="))
                .map(x -> x.substring("charset=".length()))
                .findFirst()
                .orElse("ISO-8859-1"))
                .map(Charset::forName);
        }
    }
    return charset;
}
 
Example 8
Project: cornerstone   File: LinuxInfoUtil.java   View source code 6 votes vote down vote up
public static Map<String,String> getMemInfo() throws IOException {

        Map<String,String> rtn = new HashMap<>();
        Path meminfoPath = Paths.get("/proc/meminfo");
        if (Files.exists(meminfoPath)) {
            List<String> lines = Files.readAllLines(meminfoPath, Charset.defaultCharset());
            for(String line :lines){
                String[] parts = line.split(":");
                if(parts.length>1) {
                    rtn.put(parts[0], parts[1].trim());
                }

            }


        }
        return rtn;
    }
 
Example 9
Project: lighthouse   File: EntityUtils.java   View source code 6 votes vote down vote up
/**
 * 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 &gt; 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);
}
 
Example 10
Project: talchain   File: RLPXTest.java   View source code 6 votes vote down vote up
@Test(expected = Exception.class)// failure on MDC
public void test5() {

    byte[] id = HashUtil.sha3("+++".getBytes(Charset.forName("UTF-8")));
    ECKey key = ECKey.fromPrivate(BigInteger.TEN);

    Message findNode = FindNodeMessage.create(id, key);
    logger.info("{}", findNode);

    byte[] wire = findNode.getPacket();
    wire[64]++;

    FindNodeMessage findNode2 = (FindNodeMessage) Message.decode(wire);
    logger.info("{}", findNode2);

    assertEquals(findNode.toString(), findNode2.toString());
}
 
Example 11
Project: OpenJSharp   File: Source.java   View source code 6 votes vote down vote up
private static char[] byteToCharArray(final byte[] bytes) {
    Charset cs = StandardCharsets.UTF_8;
    int start = 0;
    // BOM detection.
    if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
        start = 2;
        cs = StandardCharsets.UTF_16BE;
    } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
        start = 2;
        cs = StandardCharsets.UTF_16LE;
    } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
        start = 3;
        cs = StandardCharsets.UTF_8;
    } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) {
        start = 4;
        cs = Charset.forName("UTF-32LE");
    } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
        start = 4;
        cs = Charset.forName("UTF-32BE");
    }

    return new String(bytes, start, bytes.length - start, cs).toCharArray();
}
 
Example 12
Project: log4j2-jsonevent-layout   File: JSONLog4j2LayoutTest.java   View source code 5 votes vote down vote up
/**
 * Test filled multi line event.
 */
@Test
public void testFilledMultiLineWithUserFieldsAndContextMapAsRootEvent(){
	UserField[] fields = new UserField[2];
	fields[0] = new UserField("A1","B1");
	fields[1] = new UserField("A2","B2");
	JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,true,fields,Charset.forName("UTF-8"));
	DummyFilledLogEvent event = new DummyFilledLogEvent(LONG_STRING);
	event.getContextMap().put("CT1", "VALUE");
	event.getContextMap().put("CT2", "VALUE");
	System.out.println(layout.toSerializable(event));
}
 
Example 13
Project: jdk8u-jdk   File: Exceptions.java   View source code 5 votes vote down vote up
private static void getBytes() {
    System.out.println("getChars.(int srcBegin, int srcEnd, char dst[], "
                       + " int dstBegin");
    tryCatch("  1, 2, null, 1", NullPointerException.class, new Runnable() {
            public void run() {
                "foo".getBytes(1, 2, null, 1);
            }});

    System.out.println("getBytes.(String charsetName)"
                       + " throws UnsupportedEncodingException");
    tryCatch("  null", NullPointerException.class, new Runnable() {
            public void run() {
                try {
                    "foo".getBytes((String)null);
                } catch (UnsupportedEncodingException x) {
                    throw new RuntimeException(x);
                }
            }});

    System.out.println("getBytes.(Charset charset)");
    tryCatch("  null", NullPointerException.class, new Runnable() {
            public void run() {
                "foo".getBytes((Charset)null);
            }});
}
 
Example 14
Project: rapidminer   File: SimpleExampleSource.java   View source code 5 votes vote down vote up
public static ExampleSet createExampleSet(File file, boolean firstRowAsColumnNames, double sampleRatio, int maxLines,
		String separatorRegExpr, char[] comments, int dataRowType, boolean useQuotes, boolean trimLines,
		boolean skipErrorLines, char decimalPointCharacter, Charset encoding, String labelName, int labelColumn,
		String idName, int idColumn, String weightName, int weightColumn) throws IOException, UserError,
		IndexOutOfBoundsException {
	// create attribute data sources and guess value types (performs a data scan)
	AttributeDataSourceCreator adsCreator = new AttributeDataSourceCreator();

	adsCreator.loadData(file, comments, separatorRegExpr, decimalPointCharacter, useQuotes, '"', '\\', trimLines,
			firstRowAsColumnNames, -1, skipErrorLines, encoding, null);

	List<AttributeDataSource> attributeDataSources = adsCreator.getAttributeDataSources();

	// set special attributes
	resetAttributeType(attributeDataSources, labelName, labelColumn, Attributes.LABEL_NAME);
	resetAttributeType(attributeDataSources, idName, idColumn, Attributes.ID_NAME);
	resetAttributeType(attributeDataSources, weightName, weightColumn, Attributes.WEIGHT_NAME);

	// read data
	FileDataRowReader reader = new FileDataRowReader(new DataRowFactory(dataRowType, decimalPointCharacter),
			attributeDataSources, sampleRatio, maxLines, separatorRegExpr, comments, useQuotes, '"', '\\', trimLines,
			skipErrorLines, encoding, RandomGenerator.getGlobalRandomGenerator());
	if (firstRowAsColumnNames) {
		reader.skipLine();
	}

	AttributeSet attributeSet = new AttributeSet(new AttributeDataSources(attributeDataSources, file, encoding));

	// create table and example set
	ExampleTable table = new MemoryExampleTable(attributeSet.getAllAttributes(), reader);
	ExampleSet result = table.createExampleSet(attributeSet);

	return result;
}
 
Example 15
Project: sample-zuul-filters   File: ModifyResponseDataStreamFilter.java   View source code 5 votes vote down vote up
public Object run() {
	try {
		RequestContext context = getCurrentContext();
		InputStream stream = context.getResponseDataStream();
		String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
		body = "Modified via setResponseDataStream(): " + body;
		context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8")));
	}
	catch (IOException e) {
		rethrowRuntimeException(e);
	}
	return null;
}
 
Example 16
Project: boohee_v5.6   File: RequestBody.java   View source code 5 votes vote down vote up
public static RequestBody create(MediaType contentType, String content) {
    Charset charset = Util.UTF_8;
    if (contentType != null) {
        charset = contentType.charset();
        if (charset == null) {
            charset = Util.UTF_8;
            contentType = MediaType.parse(contentType + "; charset=utf-8");
        }
    }
    return create(contentType, content.getBytes(charset));
}
 
Example 17
Project: jdk8u-jdk   File: MimeTypesFileTypeDetector.java   View source code 5 votes vote down vote up
/**
 * Parse the mime types file, and store the type-extension mappings into
 * mimeTypeMap. The mime types file is not loaded until the first probe
 * to achieve the lazy initialization. It adopts double-checked locking
 * optimization to reduce the locking overhead.
 */
private void loadMimeTypes() {
    if (!loaded) {
        synchronized (this) {
            if (!loaded) {
                List<String> lines = AccessController.doPrivileged(
                    new PrivilegedAction<List<String>>() {
                        @Override
                        public List<String> run() {
                            try {
                                return Files.readAllLines(mimeTypesFile,
                                                          Charset.defaultCharset());
                            } catch (IOException ignore) {
                                return Collections.emptyList();
                            }
                        }
                    });

                mimeTypeMap = new HashMap<>(lines.size());
                String entry = "";
                for (String line : lines) {
                    entry += line;
                    if (entry.endsWith("\\")) {
                        entry = entry.substring(0, entry.length() - 1);
                        continue;
                    }
                    parseMimeEntry(entry);
                    entry = "";
                }
                if (!entry.isEmpty()) {
                    parseMimeEntry(entry);
                }
                loaded = true;
            }
        }
    }
}
 
Example 18
Project: stroom-query   File: StreamUtil.java   View source code 5 votes vote down vote up
/**
 * Takes a string and writes it to a file.
 */
public static void stringToFile(final String string, final Path path, final Charset charset) {
    try {
        if (Files.isRegularFile(path)) {
            Files.delete(path);
        }
        Files.createDirectories(path.getParent());

        try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
            writer.write(string);
        }
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 19
Project: lams   File: BlockingWriterSenderImpl.java   View source code 5 votes vote down vote up
@Override
public void send(final String data, final Charset charset, final IoCallback callback) {
    if (inCall) {
        queue(new ByteBuffer[]{ByteBuffer.wrap(data.getBytes(charset))}, callback);
        return;
    }
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
    } else {
        invokeOnComplete(callback);
    }
}
 
Example 20
Project: Open-DM   File: ConfigurationHelper.java   View source code 5 votes vote down vote up
private static void testUTF8Support() {
    // Test for UTF8 Support. Add warning to the logs if it is not supported
    try {
        if (!Charset.isSupported(HCPMoverConstants.SUPPORTED_CHARSET)) {
            LOG.log(Level.WARNING, HCPMoverConstants.SUPPORTED_CHARSET
                    + " Encoder not available.  Please consult the Java internationalization documentation.");
        }
    } catch (IllegalCharsetNameException e) {
        LOG.log(Level.WARNING,
                HCPMoverConstants.SUPPORTED_CHARSET
                        + " Encoder not available.  Please consult the Java internationalization documentation.",
                e);
    }
}
 
Example 21
Project: incubator-netbeans   File: PropertiesEditorSupport.java   View source code 5 votes vote down vote up
/**
 * Reads the file from the stream, filter the guarded section
 * comments, and mark the sections in the editor. Overrides superclass method. 
 * @param document the document to read into
 * @param inputStream the open stream to read from
 * @param editorKit the associated editor kit
 * @throws <code>IOException</code> if there was a problem reading the file
 * @throws <code>BadLocationException</code> should not normally be thrown
 * @see #saveFromKitToStream
 */
@Override
protected void loadFromStreamToKit(StyledDocument document, InputStream inputStream, EditorKit editorKit) throws IOException, BadLocationException {
    final Charset c = getCharset();
    final Reader reader = new BufferedReader(new InputStreamReader(inputStream, c));

    try {
        editorKit.read(reader, document, 0);
    } finally {
        reader.close();
    }
}
 
Example 22
Project: lams   File: URLEncodedUtils.java   View source code 5 votes vote down vote up
/**
 * Emcode/escape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * 
 * @param content the portion to decode
 * @param charset the charset to use
 * @param blankAsPlus if {@code true}, then convert space to '+' (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urlencode(
        final String content,
        final Charset charset,
        final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}
 
Example 23
Project: openjdk-jdk10   File: DataFlavorUtil.java   View source code 5 votes vote down vote up
/**
 * Converts an arbitrary text encoding to its canonical name.
 */
public static String canonicalName(String encoding) {
    if (encoding == null) {
        return null;
    }
    try {
        return Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException icne) {
        return encoding;
    } catch (UnsupportedCharsetException uce) {
        return encoding;
    }
}
 
Example 24
Project: apache-tomcat-7.0.73-with-comment   File: InternalAprInputBuffer.java   View source code 5 votes vote down vote up
private void skipLine(int start) throws IOException {
    boolean eol = false;
    int lastRealByte = start;
    if (pos - 1 > start) {
        lastRealByte = pos - 1;
    }

    while (!eol) {

        // Read new bytes if needed
        if (pos >= lastValid) {
            if (!fill())
                throw new EOFException(sm.getString("iib.eof.error"));
        }

        if (buf[pos] == Constants.CR) {
            // Skip
        } else if (buf[pos] == Constants.LF) {
            eol = true;
        } else {
            lastRealByte = pos;
        }
        pos++;
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("iib.invalidheader", new String(buf, start,
                lastRealByte - start + 1, Charset.forName("ISO-8859-1"))));
    }
}
 
Example 25
Project: gitplex-mit   File: ContentDetector.java   View source code 5 votes vote down vote up
/**
 * Get text from specified content bytes, optionally with help of file name.
 * 
 * @param contentBytes
 * 			content bytes to construct text from
 * @param fileName
 * 			file name to help deciding if supplied content bytes represents text
 * @return
 * 			text representation of content bytes, or <tt>null</tt> if content 
 * 			can not be converted to text
 */
@Nullable
public static String convertToText(byte[] contentBytes, @Nullable String fileName) {
	if (!isBinary(contentBytes, fileName)) {
		Charset charset = detectCharset(contentBytes);
		if (charset != null)
			return new String(contentBytes, charset);
		else
			return new String(contentBytes);
	} else {
		return null;
	}
}
 
Example 26
Project: GitHub   File: ResponseBody.java   View source code 5 votes vote down vote up
/**
 * Returns a new response body that transmits {@code content}. If {@code contentType} is non-null
 * and lacks a charset, this will use UTF-8.
 */
public static ResponseBody create(MediaType contentType, String content) {
  Charset charset = UTF_8;
  if (contentType != null) {
    charset = contentType.charset();
    if (charset == null) {
      charset = UTF_8;
      contentType = MediaType.parse(contentType + "; charset=utf-8");
    }
  }
  Buffer buffer = new Buffer().writeString(content, charset);
  return create(contentType, buffer.size(), buffer);
}
 
Example 27
Project: mpd-2017-i41d   File: HttpServer.java   View source code 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, IOException {
    Charset utf8 = Charset.forName("utf-8");
    resp.setContentType(String.format("text/html; charset=%s", utf8.name()));

    String respBody = handler.apply(req);

    byte[] respBodyBytes = respBody.getBytes(utf8);
    resp.setStatus(200);
    resp.setContentLength(respBodyBytes.length);
    OutputStream os = resp.getOutputStream();
    os.write(respBodyBytes);
    os.close();
}
 
Example 28
Project: OpenJSharp   File: IndentingWriter.java   View source code 5 votes vote down vote up
/**
 * Check if encode can handle the chars in this string.
 *
 */
protected boolean canEncode(String s) {
    final CharsetEncoder encoder =
        Charset.forName(System.getProperty("file.encoding")).newEncoder();
    char[] chars = s.toCharArray();
    for (int i=0; i<chars.length; i++) {
        if(!encoder.canEncode(chars[i])) {
            return false;
        }
    }
    return true;
}
 
Example 29
Project: NeiHanDuanZiTV   File: RequestInterceptor.java   View source code 5 votes vote down vote up
/**
 * 解析服务器响应的内容
 *
 * @param responseBody
 * @param encoding
 * @param clone
 * @return
 */
private String parseContent(ResponseBody responseBody, String encoding, Buffer clone) {
    Charset charset = Charset.forName("UTF-8");
    MediaType contentType = responseBody.contentType();
    if (contentType != null) {
        charset = contentType.charset(charset);
    }
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {//content使用gzip压缩
        return ZipHelper.decompressForGzip(clone.readByteArray(), convertCharset(charset));//解压
    } else if (encoding != null && encoding.equalsIgnoreCase("zlib")) {//content使用zlib压缩
        return ZipHelper.decompressToStringForZlib(clone.readByteArray(), convertCharset(charset));//解压
    } else {//content没有被压缩
        return clone.readString(charset);
    }
}
 
Example 30
Project: convertigo-engine   File: XMLUtils.java   View source code 5 votes vote down vote up
public static Charset getEncoding(byte[] bytes, Charset charset) {
	try {
		String encoding = new String(bytes, 0, Math.min(bytes.length, 128), "ASCII").replaceFirst("[\\d\\D]*encoding=\"(.*?)\"[\\d\\D]*", "$1");
		charset = Charset.forName(encoding);
	} catch (Exception e) { }
	
	return charset == null ? Charset.defaultCharset() : charset;
}
 
Example 31
Project: ibm-cos-sdk-java   File: DateUtilsTest.java   View source code 5 votes vote down vote up
@Test
public void testNumericNoQuote() {
    StructuredJsonGenerator jw = new SdkJsonGenerator(new JsonFactory(), null);
    jw.writeStartObject();
    jw.writeFieldName("foo").writeValue(new Date());
    jw.writeEndObject();
    String s = new String(jw.getBytes(), Charset.forName("UTF-8"));
    // Something like: {"foo":1408378076.135}.
    // Note prior to the changes, it was {"foo":1408414571}
    // (with no decimal point nor places.)
    System.out.println(s);
    final String prefix = "{\"foo\":";
    assertTrue(s, s.startsWith(prefix));
    final int startPos = prefix.length();
    // verify no starting quote for the value
    assertFalse(s, s.startsWith("{\"foo\":\""));
    assertTrue(s, s.endsWith("}"));
    // Not: {"foo":"1408378076.135"}.
    // verify no ending quote for the value
    assertFalse(s, s.endsWith("\"}"));
    final int endPos = s.indexOf("}");
    final int dotPos = s.length()-5;
    assertTrue(s, s.charAt(dotPos) == '.');
    // verify all numeric before '.'
    char[] a = s.toCharArray();
    for (int i=startPos; i < dotPos; i++)
        assertTrue(a[i] <= '9' && a[i] >= '0' );
    int j=0;
    // verify all numeric after '.'
    for (int i=dotPos+1; i < endPos; i++) {
        assertTrue(a[i] <= '9' && a[i] >= '0' );
        j++;
    }
    // verify decimal precision of exactly 3
    assertTrue(j == 3);
}
 
Example 32
Project: phone-simulator   File: SmsSignalInfoImpl.java   View source code 5 votes vote down vote up
public SmsSignalInfoImpl(SmsTpdu tpdu, Charset gsm8Charset) throws MAPException {
    if (tpdu == null)
        throw new MAPException("SmsTpdu must not be null");

    this.setGsm8Charset(gsm8Charset);
    this.data = tpdu.encodeData();
}
 
Example 33
Project: sonar-css-plugin   File: CssCheckVerifier.java   View source code 5 votes vote down vote up
/**
 * See {@link CssCheckVerifier#issuesOnScssFile(CssCheck, File)}
 *
 * @param charset Charset of the file to test.
 */
public static CheckMessagesVerifier issuesOnScssFile(CssCheck check, File file, Charset charset) {
  if (check instanceof CharsetAwareVisitor) {
    ((CharsetAwareVisitor) check).setCharset(charset);
  }
  return CheckMessagesVerifier.verify(TreeCheckTest.getIssues(file.getAbsolutePath(), check, ScssParser.createParser(charset)));
}
 
Example 34
Project: jdk8u-jdk   File: Johab_OLD.java   View source code 5 votes vote down vote up
public Decoder(Charset cs) {
    super(cs,
          index1,
          index2,
          0x20,
          0xFE);
}
 
Example 35
Project: mv-android-client   File: WebPlayerActivity.java   View source code 5 votes vote down vote up
private Bootstrapper(Player player) {
    Context context = player.getContext();
    player.addJavascriptInterface(this, Bootstrapper.INTERFACE);

    mPlayer = player;
    mURIBuilder = Uri.fromFile(new File(context.getString(R.string.mv_project_index))).buildUpon();
    mPlayer.loadData(new String(Base64.decode(context.getString(R.string.webview_default_page), Base64.DEFAULT), Charset.forName("UTF-8")));
}
 
Example 36
Project: CryptoKnight   File: HashFactory.java   View source code 5 votes vote down vote up
public static String CommonHash(String p, String alg) throws NoSuchAlgorithmException
{
    MessageDigest md = MessageDigest.getInstance(alg);
    
    md.reset();
    
    md.update(p.getBytes(Charset.forName("UTF-8")));
    byte[] resBytes = md.digest();
    String res = new String(Hex.encodeHex(resBytes));
    
    return res;
}
 
Example 37
Project: BibliotecaPS   File: CharsetMapping.java   View source code 5 votes vote down vote up
/**
 * Constructs MysqlCharset object
 * 
 * @param charsetName
 *            MySQL charset name
 * @param mblen
 *            Max number of bytes per character
 * @param priority
 *            MysqlCharset with highest lever of this param will be used for Java encoding --> Mysql charsets conversion.
 * @param javaEncodings
 *            List of Java encodings corresponding to this MySQL charset; the first name in list is the default for mysql --> java data conversion
 */
public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEncodings) {
    this.charsetName = charsetName;
    this.mblen = mblen;
    this.priority = priority;

    for (int i = 0; i < javaEncodings.length; i++) {
        String encoding = javaEncodings[i];
        try {
            Charset cs = Charset.forName(encoding);
            addEncodingMapping(cs.name());

            Set<String> als = cs.aliases();
            Iterator<String> ali = als.iterator();
            while (ali.hasNext()) {
                addEncodingMapping(ali.next());
            }
        } catch (Exception e) {
            // if there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets
            if (mblen == 1) {
                addEncodingMapping(encoding);
            }
        }
    }

    if (this.javaEncodingsUc.size() == 0) {
        if (mblen > 1) {
            addEncodingMapping("UTF-8");
        } else {
            addEncodingMapping("Cp1252");
        }
    }
}
 
Example 38
Project: openjdk-jdk10   File: IBM942_OLD.java   View source code 5 votes vote down vote up
public Encoder(Charset cs) {
    super(cs);
    super.mask1 = 0xFFE0;
    super.mask2 = 0x001F;
    super.shift = 5;
    super.index1 = index1;
    super.index2 = index2;
    super.index2a = index2a;
}
 
Example 39
Project: incubator-netbeans   File: InputReaders.java   View source code 5 votes vote down vote up
/**
 * Returns the input reader for the given file. To convert read bytes
 * to characters specified charset is used.
 * <p>
 * Returned reader will never call reset on {@link InputProcessor} while
 * reading.
 * <p>
 * Returned reader is <i>not thread safe</i> so it can't be used in
 * multiple instances of {@link InputReaderTask}.
 *
 * @param file file to read from
 * @param charset bytes to characters conversion charset
 * @return input reader for the given file
 */
@NonNull
public static InputReader forFile(@NonNull File file, @NonNull Charset charset) {
    Parameters.notNull("file", file);
    Parameters.notNull("charset", charset);

    final FileInput fileInput = new FileInput(file, charset);
    return forFileInputProvider(new FileInput.Provider() {

        @Override
        public FileInput getFileInput() {
            return fileInput;
        }
    });
}
 
Example 40
Project: ditb   File: SaslClientHandler.java   View source code 5 votes vote down vote up
/**
 * Get the read status
 *
 * @param inStream to read
 * @throws org.apache.hadoop.ipc.RemoteException if status was not success
 */
private static void readStatus(ByteBuf inStream) throws RemoteException {
  int status = inStream.readInt(); // read status
  if (status != SaslStatus.SUCCESS.state) {
    throw new RemoteException(inStream.toString(Charset.forName("UTF-8")),
        inStream.toString(Charset.forName("UTF-8")));
  }
}