Search in sources :

Example 76 with CharBuffer

use of java.nio.CharBuffer in project jphp by jphp-compiler.

the class CharArrayMemory method put.

public void put(int index, String s) {
    int len = s.length();
    int sLen = buffer.limit();
    if (index < 0)
        return;
    char ch = len == 0 ? '\0' : s.charAt(0);
    if (index < sLen)
        buffer.put(index, ch);
    else {
        int cnt = index - sLen;
        CharBuffer tmp = CharBuffer.allocate(sLen + cnt + 1);
        tmp.put(buffer.array());
        buffer = tmp;
        for (int i = 0; i < cnt; i++) {
            buffer.append('\32');
        }
        buffer.append(ch);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 77 with CharBuffer

use of java.nio.CharBuffer in project Android-Terminal-Emulator by jackpal.

the class ShortcutEncryption method decrypt.

/**
     * Decrypts a string encrypted using this algorithm and verifies that the
     * contents have not been tampered with.
     *
     * @param encrypted The string to decrypt, in the format described above.
     * @param keys The keys to verify and decrypt with.
     * @return The decrypted data.
     *
     * @throws GeneralSecurityException if the data is invalid, verification fails, or an error occurs during decryption.
     */
public static String decrypt(String encrypted, Keys keys) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance(ENC_SYSTEM);
    String[] data = COLON.split(encrypted);
    if (data.length != 3) {
        throw new GeneralSecurityException("Invalid encrypted data!");
    }
    String mac = data[0];
    String iv = data[1];
    String cipherText = data[2];
    // Verify that the ciphertext and IV haven't been tampered with first
    String dataToAuth = iv + ":" + cipherText;
    if (!computeMac(dataToAuth, keys.getMacKey()).equals(mac)) {
        throw new GeneralSecurityException("Incorrect MAC!");
    }
    // Decrypt the ciphertext
    byte[] ivBytes = decodeBase64(iv);
    cipher.init(Cipher.DECRYPT_MODE, keys.getEncKey(), new IvParameterSpec(ivBytes));
    byte[] bytes = cipher.doFinal(decodeBase64(cipherText));
    // Decode the plaintext bytes into a String
    CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    /*
         * We are coding UTF-8 (guaranteed to be the default charset on
         * Android) to Java chars (UTF-16, 2 bytes per char).  For valid UTF-8
         * sequences, then:
         *     1 byte in UTF-8 (US-ASCII) -> 1 char in UTF-16
         *     2-3 bytes in UTF-8 (BMP)   -> 1 char in UTF-16
         *     4 bytes in UTF-8 (non-BMP) -> 2 chars in UTF-16 (surrogate pair)
         * The decoded output is therefore guaranteed to fit into a char
         * array the same length as the input byte array.
         */
    CharBuffer out = CharBuffer.allocate(bytes.length);
    CoderResult result = decoder.decode(ByteBuffer.wrap(bytes), out, true);
    if (result.isError()) {
        /* The input was supposed to be the result of encrypting a String,
             * so something is very wrong if it cannot be decoded into one! */
        throw new GeneralSecurityException("Corrupt decrypted data!");
    }
    decoder.flush(out);
    return out.flip().toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) GeneralSecurityException(java.security.GeneralSecurityException) CharBuffer(java.nio.CharBuffer) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) CoderResult(java.nio.charset.CoderResult)

Example 78 with CharBuffer

use of java.nio.CharBuffer in project Android-Terminal-Emulator by jackpal.

the class TerminalEmulator method handleUTF8Sequence.

private boolean handleUTF8Sequence(byte b) {
    if (mUTF8ToFollow == 0 && (b & 0x80) == 0) {
        // ASCII character -- we don't need to handle this
        return false;
    }
    if (mUTF8ToFollow > 0) {
        if ((b & 0xc0) != 0x80) {
            /* Not a UTF-8 continuation byte (doesn't begin with 0b10)
                   Replace the entire sequence with the replacement char */
            mUTF8ToFollow = 0;
            mUTF8ByteBuffer.clear();
            emit(UNICODE_REPLACEMENT_CHAR);
            /* The Unicode standard (section 3.9, definition D93) requires
                 * that we now attempt to process this byte as though it were
                 * the beginning of another possibly-valid sequence */
            return handleUTF8Sequence(b);
        }
        mUTF8ByteBuffer.put(b);
        if (--mUTF8ToFollow == 0) {
            // Sequence complete -- decode and emit it
            ByteBuffer byteBuf = mUTF8ByteBuffer;
            CharBuffer charBuf = mInputCharBuffer;
            CharsetDecoder decoder = mUTF8Decoder;
            byteBuf.rewind();
            decoder.reset();
            decoder.decode(byteBuf, charBuf, true);
            decoder.flush(charBuf);
            char[] chars = charBuf.array();
            if (chars[0] >= 0x80 && chars[0] <= 0x9f) {
                /* Sequence decoded to a C1 control character which needs
                       to be sent through process() again */
                process((byte) chars[0], false);
            } else {
                emit(chars);
            }
            byteBuf.clear();
            charBuf.clear();
        }
    } else {
        if ((b & 0xe0) == 0xc0) {
            // 0b110 -- two-byte sequence
            mUTF8ToFollow = 1;
        } else if ((b & 0xf0) == 0xe0) {
            // 0b1110 -- three-byte sequence
            mUTF8ToFollow = 2;
        } else if ((b & 0xf8) == 0xf0) {
            // 0b11110 -- four-byte sequence
            mUTF8ToFollow = 3;
        } else {
            // Not a valid UTF-8 sequence start -- replace this char
            emit(UNICODE_REPLACEMENT_CHAR);
            return true;
        }
        mUTF8ByteBuffer.put(b);
    }
    return true;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 79 with CharBuffer

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

the class AsciiStringCharacterTest method caseInsensitiveHasherCharBuffer.

@Test
public void caseInsensitiveHasherCharBuffer() {
    String s1 = new String("TRANSFER-ENCODING");
    char[] array = new char[128];
    final int offset = 100;
    for (int i = 0; i < s1.length(); ++i) {
        array[offset + i] = s1.charAt(i);
    }
    CharBuffer buffer = CharBuffer.wrap(array, offset, s1.length());
    assertEquals(AsciiString.hashCode(s1), AsciiString.hashCode(buffer));
}
Also used : CharBuffer(java.nio.CharBuffer) Test(org.junit.Test)

Example 80 with CharBuffer

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

the class AdversarialReader method read.

@Override
public int read(CharBuffer target) throws IOException {
    if (adversary.injectFailureOrMischief(IOException.class, BufferOverflowException.class, IndexOutOfBoundsException.class)) {
        CharBuffer dup = target.duplicate();
        dup.limit(Math.max(target.limit() / 2, 1));
        return reader.read(dup);
    }
    return reader.read(target);
}
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