Refine search
@Override public void handle(HttpExchange httpExchange) throws IOException { httpExchange.getResponseHeaders().set("Content-type", this.contentType); ByteBuffer buffer = Charset.forName("UTF-8").encode(content); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); httpExchange.sendResponseHeaders(HTTP_OK, bytes.length); httpExchange.getResponseBody().write(bytes); httpExchange.close(); } } // end static class FileHandler
private byte[] toBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data return bytes; }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); if (value == null) { return null; } ByteBuffer encoded; if (encoder != null){ try { encoded = encoder.encode(CharBuffer.wrap(value)); } catch (CharacterCodingException e) { throw new HiveException(e); } } else { encoded = Charset.forName( PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); } result.setSize(encoded.limit()); encoded.get(result.getBytes(), 0, encoded.limit()); return result; }
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); } }
@Test public void utf16WithBOM() throws IOException { final ByteBuffer utf16str = Charset.forName("UTF-16").encode("hello"); byte[] bytes = new byte[utf16str.remaining()]; utf16str.get(bytes, 0, bytes.length); new TestableTextAnalyzer().analyze(new Document(), getStreamSource(bytes), null); assertEquals("UTF-16", encoding); assertEquals("hello", contents); }
static byte[] toBytes(char[] chars) { CharBuffer charBuffer=CharBuffer.wrap(chars); ByteBuffer byteBuffer=Charset.forName("UTF-8").encode(charBuffer); byte[] bytes=Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); // Arrays.fill(charBuffer.array(), '\u0000'); // clear the cleartext Arrays.fill(byteBuffer.array(), (byte) 0); // clear the ciphertext return bytes; }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); if (value == null) { return null; } ByteBuffer encoded; if (encoder != null){ try { encoded = encoder.encode(CharBuffer.wrap(value)); } catch (CharacterCodingException e) { throw new HiveException(e); } } else { encoded = Charset.forName( PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); } result.setSize(encoded.limit()); encoded.get(result.getBytes(), 0, encoded.limit()); return result; }
Charset utf8charset = Charset.forName("UTF-8"); Charset iso88591charset = Charset.forName("ISO-8859-1"); ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2}); // decode UTF-8 CharBuffer data = utf8charset.decode(inputBuffer); // encode ISO-8559-1 ByteBuffer outputBuffer = iso88591charset.encode(data); byte[] outputData = outputBuffer.array();
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); } }
@Test public void utf16WithBOMAlternate() throws IOException { final ByteBuffer utf16str = Charset.forName("UTF-16").encode("hello"); byte[] bytes = new byte[utf16str.remaining()]; utf16str.get(bytes, 0, bytes.length); for (int i = 0; i < bytes.length; i += 2) { byte b = bytes[i]; bytes[i] = bytes[i + 1]; bytes[i + 1] = b; } new TestableTextAnalyzer().analyze(new Document(), getStreamSource(bytes), null); assertEquals("UTF-16", encoding); assertEquals("hello", contents); }
/** * Returns a {@code byte[]} containing the value of the provided {@code char[]} without using {@code new String(chars).getBytes()} which would put sensitive data (the password) in the String pool. * * @param chars the characters to convert * @return the byte[] */ private static byte[] convertCharsToBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); return Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); } }
@FastMethod @Signature({ @Arg("string"), @Arg("charset") }) public static Memory encode(Environment env, Memory... args) { Charset charset; try { charset = Charset.forName(args[1].toString()); } catch (Exception e) { return Memory.FALSE; } ByteBuffer buffer = charset.encode(args[0].toString()); return new BinaryMemory(Arrays.copyOf(buffer.array(), buffer.limit())); }