Search in sources :

Example 81 with CharBuffer

use of java.nio.CharBuffer in project jersey by jersey.

the class UriComponent method decodeOctets.

/**
     * Decodes octets to characters using the UTF-8 decoding and appends
     * the characters to a StringBuffer.
     *
     * @return the index to the next unchecked character in the string to decode
     */
private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) {
    // If there is only one octet and is an ASCII character
    if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) {
        // Octet can be appended directly
        sb.append((char) bb.get(0));
        return i + 2;
    } else {
        //
        final CharBuffer cb = UTF_8_CHARSET.decode(bb);
        sb.append(cb.toString());
        return i + bb.limit() * 3 - 1;
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 82 with CharBuffer

use of java.nio.CharBuffer in project jersey by jersey.

the class UriComponent method appendUTF8EncodedCharacter.

private static void appendUTF8EncodedCharacter(final StringBuilder sb, final int codePoint) {
    final CharBuffer chars = CharBuffer.wrap(Character.toChars(codePoint));
    final ByteBuffer bytes = UTF_8_CHARSET.encode(chars);
    while (bytes.hasRemaining()) {
        appendPercentEncodedOctet(sb, bytes.get() & 0xFF);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 83 with CharBuffer

use of java.nio.CharBuffer in project hackpad by dropbox.

the class LenientFormatter method format.

/**
     * Writes a formatted string to the output destination of the {@code Formatter}.
     * 
     * @param l
     *            the {@code Locale} used in the method. If {@code locale} is
     *            {@code null}, then no localization will be applied. This
     *            parameter does not influence the {@code Locale} specified during
     *            construction.
     * @param format
     *            a format string.
     * @param args
     *            the arguments list used in the {@code format()} method. If there are
     *            more arguments than those specified by the format string, then
     *            the additional arguments are ignored.
     * @return this {@code Formatter}.
     * @throws IllegalFormatException
     *             if the format string is illegal or incompatible with the
     *             arguments, or if fewer arguments are sent than those required by
     *             the format string, or any other illegal situation.
     * @throws FormatterClosedException
     *             if the {@code Formatter} has been closed.
     */
public LenientFormatter format(Locale l, String format, Object... args) {
    checkClosed();
    CharBuffer formatBuffer = CharBuffer.wrap(format);
    ParserStateMachine parser = new ParserStateMachine(formatBuffer);
    Transformer transformer = new Transformer(this, l);
    int currentObjectIndex = 0;
    Object lastArgument = null;
    boolean hasLastArgumentSet = false;
    while (formatBuffer.hasRemaining()) {
        parser.reset();
        FormatToken token = parser.getNextFormatToken();
        String result;
        String plainText = token.getPlainText();
        if (token.getConversionType() == (char) FormatToken.UNSET) {
            result = plainText;
        } else {
            plainText = plainText.substring(0, plainText.indexOf('%'));
            Object argument = null;
            if (token.requireArgument()) {
                int index = token.getArgIndex() == FormatToken.UNSET ? currentObjectIndex++ : token.getArgIndex();
                argument = getArgument(args, index, token, lastArgument, hasLastArgumentSet);
                lastArgument = argument;
                hasLastArgumentSet = true;
            }
            result = transformer.transform(token, argument);
            result = (null == result ? plainText : plainText + result);
        }
        // if output is made by formattable callback
        if (null != result) {
            try {
                out.append(result);
            } catch (IOException e) {
                lastIOException = e;
            }
        }
    }
    return this;
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException)

Example 84 with CharBuffer

use of java.nio.CharBuffer in project cryptomator by cryptomator.

the class WindowsProtectedKeychainAccess method loadPassphrase.

@Override
public char[] loadPassphrase(String key) {
    loadKeychainEntriesIfNeeded();
    KeychainEntry entry = keychainEntries.get(key);
    if (entry == null) {
        return null;
    }
    byte[] cleartext = dataProtection.unprotect(entry.ciphertext, entry.salt);
    if (cleartext == null) {
        return null;
    }
    CharBuffer buf = UTF_8.decode(ByteBuffer.wrap(cleartext));
    char[] passphrase = new char[buf.remaining()];
    buf.get(passphrase);
    Arrays.fill(cleartext, (byte) 0x00);
    Arrays.fill(buf.array(), (char) 0x00);
    return passphrase;
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 85 with CharBuffer

use of java.nio.CharBuffer in project netty by netty.

the class Unpooled method copiedBuffer.

/**
     * Creates a new big-endian buffer whose content is a subregion of
     * the specified {@code string} encoded in the specified {@code charset}.
     * The new buffer's {@code readerIndex} and {@code writerIndex} are
     * {@code 0} and the length of the encoded string respectively.
     */
public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }
    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
        }
        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(buf, charset);
    }
    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
}
Also used : CharBuffer(java.nio.CharBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 InputStream (java.io.InputStream)6