List of usage examples for java.nio CharBuffer wrap
public static CharBuffer wrap(CharSequence chseq)
From source file:com.bconomy.autobit.Encryption.java
private static byte[] charsToBytes(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 Arrays.fill(chars, '\u0000'); // clear sensitive data return bytes; }
From source file:org.pentaho.di.cluster.HttpUtilTest.java
/** * https://www.securecoding.cert.org/confluence/display/java/IDS12-J.+Perform+lossless+conversion+ * of+String+data+between+differing+character+encodings * * @param in/*from ww w . j a v a 2s . c om*/ * string to encode * @return * @throws IOException */ private String canonicalBase64Encode(String in) throws IOException { Charset charset = Charset.forName(DEFAULT_ENCODING); CharsetEncoder encoder = charset.newEncoder(); encoder.reset(); ByteBuffer baosbf = encoder.encode(CharBuffer.wrap(in)); byte[] bytes = new byte[baosbf.limit()]; baosbf.get(bytes); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); gzos.write(bytes); gzos.close(); String encoded = new String(Base64.encodeBase64(baos.toByteArray())); return encoded; }
From source file:com.addthis.hydra.query.tracker.DetailedStatusHandler.java
private void onSuccess(QueryEntryInfo queryEntryInfo) { try {//from w w w. j ava2s . c o m JSONObject entryJSON = CodecJSON.encodeJSON(queryEntryInfo); writer.write(entryJSON.toString()); ByteBuf textResponse = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(writer.getBuilder()), CharsetUtil.UTF_8); HttpContent content = new DefaultHttpContent(textResponse); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, textResponse.readableBytes()); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } ctx.write(response); ctx.write(content); ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } } catch (Throwable t) { onFailure(t); } }
From source file:com.boylesoftware.web.impl.auth.CipherToolbox.java
/** * Create new instance./*from w w w . j av a2 s.com*/ * * @param pool Reference to the owning pool. * @param pooledObjectId Pooled object id. * @param secretKey The key. */ CipherToolbox(final FastPool<CipherToolbox> pool, final int pooledObjectId, final Key secretKey) { super(pool, pooledObjectId); this.secretKey = secretKey; try { this.cipher = Cipher.getInstance(ALGORITHM); } catch (final GeneralSecurityException e) { throw new RuntimeException("Error creating cipher.", e); } this.clearBuf = ByteBuffer.allocate(48); this.cipherBuf = ByteBuffer.allocate(48); this.base64Chars = new char[64]; this.base64Buf = CharBuffer.wrap(this.base64Chars); }
From source file:de.intranda.goobi.plugins.utils.SRUClient.java
/** * Converts a <code>String</code> from one given encoding to the other. * /*from w w w. j a v a 2 s. c om*/ * @param string The string to convert. * @param from Source encoding. * @param to Destination encoding. * @return The converted string. */ public static String convertStringEncoding(String string, String from, String to) { try { Charset charsetFrom = Charset.forName(from); Charset charsetTo = Charset.forName(to); CharsetEncoder encoder = charsetFrom.newEncoder(); CharsetDecoder decoder = charsetTo.newDecoder(); ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(string)); CharBuffer cbuf = decoder.decode(bbuf); return cbuf.toString(); } catch (CharacterCodingException e) { logger.error(e.getMessage(), e); } return string; }
From source file:com.amazonaws.services.dynamodbv2.online.index.AttributeValueConverter.java
public static AttributeValue parseFromBlankString(String attribtueType, String value) throws IllegalArgumentException { try {/* w w w. ja va 2s. c om*/ AttributeValue attributeValue = new AttributeValue(); if (attribtueType.equals("N")) { attributeValue.withN(value); } else if (attribtueType.equals("S")) { attributeValue.withS(value); } else if (attribtueType.equals("B")) { attributeValue.withB(encoder.encode(CharBuffer.wrap(value))); } else if (attribtueType.equals("NS")) { List<String> numberSet = mapper.readValue(value, new TypeReference<List<String>>() { }); attributeValue.withNS(numberSet); } else if (attribtueType.equals("SS")) { List<String> stringSet = mapper.readValue(value, new TypeReference<List<String>>() { }); attributeValue.withSS(stringSet); } else if (attribtueType.equals("BS")) { List<String> binaryStringSet = mapper.readValue(value, new TypeReference<List<String>>() { }); List<ByteBuffer> bianrySet = new ArrayList<ByteBuffer>(); for (String bss : binaryStringSet) { bianrySet.add(encoder.encode(CharBuffer.wrap(bss))); } attributeValue.withBS(bianrySet); } else { throw new IllegalArgumentException("Error: Invalid Attribute Value Type. "); } return attributeValue; } catch (CharacterCodingException cce) { throw new IllegalArgumentException("Error: Failed to encode binary into string."); } catch (IOException e) { throw new IllegalArgumentException("Error: Failed to parse set from string."); } }
From source file:co.cask.cdap.logging.gateway.handlers.ChunkedLogReaderCallback.java
@Override public void handle(LogEvent event) { String logLine = patternLayout.doLayout(event.getLoggingEvent()); logLine = escape ? StringEscapeUtils.escapeHtml(logLine) : logLine; try {//from w ww . ja v a 2s . c om // Encode logLine and send chunks encodeSend(CharBuffer.wrap(logLine), false); count.incrementAndGet(); } catch (IOException e) { // Just propagate the exception, the caller of this Callback should be handling it. throw Throwables.propagate(e); } }
From source file:com.addthis.hydra.query.web.GoogleDriveAuthentication.java
/** * Send an HTML formatted error message. *//*from w w w .ja v a 2 s .c om*/ private static void sendErrorMessage(ChannelHandlerContext ctx, String message) throws IOException { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().set(CONTENT_TYPE, "text/html; charset=utf-8"); StringBuilderWriter writer = new StringBuilderWriter(50); writer.append("<html><head><title>Hydra Query Master</title></head><body>"); writer.append("<h3>"); writer.append(message); writer.append("</h3></body></html>"); ByteBuf textResponse = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(writer.getBuilder()), CharsetUtil.UTF_8); HttpContent content = new DefaultHttpContent(textResponse); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, textResponse.readableBytes()); ctx.write(response); ctx.write(content); ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); lastContentFuture.addListener(ChannelFutureListener.CLOSE); }
From source file:pl.bristleback.server.bristle.engine.tomcat.servlet.TomcatServletWebsocketEngine.java
@Override public void sendMessage(WebsocketConnector connector, String contentAsString) throws Exception { CharBuffer buffer = CharBuffer.wrap(contentAsString); ((TomcatConnector) connector).getConnection().writeTextMessage(buffer); }
From source file:com.cloudera.sqoop.lib.RecordParser.java
/** * Return a list of strings representing the fields of the input line. * This list is backed by an internal buffer which is cleared by the * next call to parseRecord().//w ww. ja v a 2s. c om */ public List<String> parseRecord(CharSequence input) throws ParseError { if (null == input) { throw new ParseError("null input string"); } return parseRecord(CharBuffer.wrap(input)); }