Java Code Examples for org.apache.commons.lang.StringUtils.isAsciiPrintable()

The following are Jave code examples for showing how to use isAsciiPrintable() of the org.apache.commons.lang.StringUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: IPE-LWM2M   File: CoapMessage.java   Source Code and License Vote up 4 votes
private CoapMessage(boolean incoming, Type type, int mId, String token, OptionSet options, byte[] payload) {
    this.incoming = incoming;
    this.timestamp = System.currentTimeMillis();
    this.type = type.toString();
    this.mId = mId;
    this.token = token;

    if (options != null) {
        List<Option> opts = options.asSortedList();
        if (!opts.isEmpty()) {
            Map<String, List<String>> optMap = new HashMap<>();
            for (Option opt : opts) {
                String strOption = OptionNumberRegistry.toString(opt.getNumber());
                List<String> values = optMap.get(strOption);
                if (values == null) {
                    values = new ArrayList<>();
                    optMap.put(strOption, values);
                }
                values.add(opt.toValueString());
            }

            StringBuilder builder = new StringBuilder();
            for (Entry<String, List<String>> e : optMap.entrySet()) {
                if (builder.length() > 0) {
                    builder.append(" - ");
                }
                builder.append(e.getKey()).append(": ").append(StringUtils.join(e.getValue(), ", "));
            }
            this.options = builder.toString();

        }
    }
    if (payload != null && payload.length > 0) {
        String strPayload = new String(payload, StandardCharsets.UTF_8);
        if (StringUtils.isAsciiPrintable(strPayload)) {
            this.payload = strPayload;
        } else {
            this.payload = "Hex:" + Hex.encodeHexString(payload);
        }
    }
}