public void setContent(byte[] cntnt) throws Exception { StringBuffer sb = new StringBuffer(isHTTP11 ? "HTTP/1.1" : "HTTP/1.0"); sb.append(OK_HDR); sb.append("Content-Type: "); sb.append(contentType); if (!isHTTP11 || !keepAlive || isMessages) { sb.append("\r\nConnection: close\r\nProxy-Connection: close"); } else { sb.append("\r\nConnection: Keep-Alive\r\nProxy-Connection: Keep-Alive"); sb.append("\r\nContent-Length: "); sb.append(cntnt.length); } sb.append("\r\n\r\n"); sb.trimToSize(); CharBuffer cb = CharBuffer.wrap(sb.toString()); ByteBuffer tempBuffer; try { tempBuffer = Charset.forName("iso-8859-1").newEncoder().encode(cb); } catch (CharacterCodingException cce) { Server.debug(this, "prepareForSending: ", cce, Server.MSG_ERROR, Server.LVL_MINOR); try { tempBuffer = ByteBuffer.wrap(cb.toString().getBytes(Server.srv.DEFAULT_CHARSET)); } catch (Exception e) { Server.debug(this, "prepareForSending: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); throw e; } } this.buf = ByteBuffer.allocate(tempBuffer.capacity() + cntnt.length); this.buf.put(tempBuffer.array()); this.buf.put(cntnt); this.buf.flip(); }
@SuppressWarnings("unchecked") private void sendMessageText(boolean last) throws WsIOException { if (textMsgHandler instanceof WrappedMessageHandler) { long maxMessageSize = ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize(); if (maxMessageSize > -1 && messageBufferText.remaining() > maxMessageSize) { throw new WsIOException( new CloseReason( CloseCodes.TOO_BIG, sm.getString( "wsFrame.messageTooBig", Long.valueOf(messageBufferText.remaining()), Long.valueOf(maxMessageSize)))); } } try { if (textMsgHandler instanceof MessageHandler.Partial<?>) { ((MessageHandler.Partial<String>) textMsgHandler) .onMessage(messageBufferText.toString(), last); } else { // Caller ensures last == true if this branch is used ((MessageHandler.Whole<String>) textMsgHandler).onMessage(messageBufferText.toString()); } } catch (Throwable t) { handleThrowableOnSend(t); } finally { messageBufferText.clear(); } }
/** * Read a 'n' bytes from buffer into a String where n is the framesize - offset so therefore * cannot use this if there are other objects after it because it has no delimiter. * * <p>Must take into account the text encoding defined in the Encoding Object ID3 Text Frames * often allow multiple strings seperated by the null char appropriate for the encoding. * * @param arr this is the buffer for the frame * @param offset this is where to start reading in the buffer for this field * @throws NullPointerException * @throws IndexOutOfBoundsException */ public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException { logger.finest("Reading from array from offset:" + offset); // Get the Specified Decoder String charSetName = getTextEncodingCharSet(); CharsetDecoder decoder = Charset.forName(charSetName).newDecoder(); decoder.reset(); // Decode sliced inBuffer ByteBuffer inBuffer; // #302 [dallen] truncating array manually since the decoder.decode() does not honor the offset // in the in buffer byte[] truncArr = new byte[arr.length - offset]; System.arraycopy(arr, offset, truncArr, 0, truncArr.length); inBuffer = ByteBuffer.wrap(truncArr); CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset); CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true); if (coderResult.isError()) { logger.warning("Decoding error:" + coderResult.toString()); } decoder.flush(outBuffer); outBuffer.flip(); // If using UTF16 with BOM we then search through the text removing any BOMs that could exist // for multiple values, BOM could be Big Endian or Little Endian if (charSetName.equals(TextEncoding.CHARSET_UTF_16)) { value = outBuffer.toString().replace("\ufeff", "").replace("\ufffe", ""); } else { value = outBuffer.toString(); } // SetSize, important this is correct for finding the next datatype setSize(arr.length - offset); logger.config("Read SizeTerminatedString:" + value + " size:" + size); }
// Check if DatagramChannel.send while connected can include // address without throwing private static void test1() throws Exception { DatagramChannel sndChannel = DatagramChannel.open(); sndChannel.socket().bind(null); InetSocketAddress sender = new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort()); DatagramChannel rcvChannel = DatagramChannel.open(); rcvChannel.socket().bind(null); InetSocketAddress receiver = new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort()); rcvChannel.connect(sender); sndChannel.connect(receiver); ByteBuffer bb = ByteBuffer.allocate(256); bb.put("hello".getBytes()); bb.flip(); int sent = sndChannel.send(bb, receiver); bb.clear(); rcvChannel.receive(bb); bb.flip(); CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed"); rcvChannel.close(); sndChannel.close(); }
private static String readFromFile(File file) throws IOException { Reader reader = new FileReader(file); CharBuffer charBuffer = CharBuffer.allocate(MAX_TEST_DATA_FILE_SIZE); reader.read(charBuffer); charBuffer.position(0); return charBuffer.toString().trim(); }
// Check if the datagramsocket adaptor can send with a packet // that has not been initialized with an address; the legacy // datagram socket will send in this case private static void test2() throws Exception { DatagramChannel sndChannel = DatagramChannel.open(); sndChannel.socket().bind(null); InetSocketAddress sender = new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort()); DatagramChannel rcvChannel = DatagramChannel.open(); rcvChannel.socket().bind(null); InetSocketAddress receiver = new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort()); rcvChannel.connect(sender); sndChannel.connect(receiver); byte b[] = "hello".getBytes("UTF-8"); DatagramPacket pkt = new DatagramPacket(b, b.length); sndChannel.socket().send(pkt); ByteBuffer bb = ByteBuffer.allocate(256); rcvChannel.receive(bb); bb.flip(); CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed"); // Check that the pkt got set with the target address; // This is legacy behavior if (!pkt.getSocketAddress().equals(receiver)) throw new RuntimeException("Test failed"); rcvChannel.close(); sndChannel.close(); }
/** Decode the specified array of bytes according to * a charset selection. This function tries * to decode a string from the given byte array * with the following charsets (in preferred order): * <ul> * <li>the current charset returned by {@link Charset#defaultCharset()},</li> * <li>OEM United States: IBM437,</li> * <li>West European: ISO-8859-1,</li> * <li>one of the charst returned by {@link Charset#availableCharsets()}.</li> * </ul> * <p> * The IBM437 charset was added to support several specific files (Dbase files) * generated from a GIS. * * @param bytes is the array of bytes to decode. * @return the decoded string with the appropriate charset set. */ public static String decodeString(byte[] bytes) { Charset default_charset = Charset.defaultCharset(); Charset west_european = Charset.forName("ISO-8859-1"); //$NON-NLS-1$ Charset utf = Charset.forName("UTF-8"); //$NON-NLS-1$ String refBuffer = new String(bytes); CharBuffer buffer = decodeString(bytes,default_charset,refBuffer.length()); if ((buffer==null)&&(!default_charset.equals(west_european))) { buffer = decodeString(bytes,west_european,refBuffer.length()); } if ((buffer==null)&&(!default_charset.equals(utf))) { buffer = decodeString(bytes,utf,refBuffer.length()); } if (buffer==null) { // Decode until one of the available charset replied a value for (Charset charset : Charset.availableCharsets().values()) { buffer = decodeString(bytes,charset,refBuffer.length()); if (buffer!=null) break; } } // Use the default encoding if (buffer==null) return refBuffer; return buffer.toString(); }
@Override protected void onTextMessage(CharBuffer message) throws IOException { String temp = message.toString(); JSONObject json = (JSONObject) JSONSerializer.toJSON(temp); String type = json.getString("type").toLowerCase().replaceAll(" ", ""); String location = json.getString("location"); String lat = json.getString("lat"); String lng = json.getString("lng"); String radius = json.getString("radius"); String keywords = json.getString("keywords"); String result = null; try { if (type.equals("overview")) result = getOverViewData(location, lat, lng, radius, keywords); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult.toString(); } if (result == null || result.equals("")) { JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult.toString(); } Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); CharBuffer uCharBuffer = CharBuffer.wrap(result); ByteBuffer bbuf = encoder.encode(uCharBuffer); CharBuffer cbuf = decoder.decode(bbuf); getWsOutbound().writeTextMessage(cbuf); }
@Override public String toString() { if (null == data) { cb.rewind(); data = cb.toString().substring(0, size); } return data; }
private void testControlCharsBuffer(boolean remove, String input, String output) throws Exception { try (Reader reader = new OverrunReader(new StringReader(input), -1, false, remove)) { CharBuffer buff = CharBuffer.allocate(10); Assert.assertEquals(output.length(), reader.read(buff)); buff.flip(); Assert.assertEquals(output, buff.toString()); } }
private BigDecimal toBigDecimal() { if (lineBuffer.hasArray()) { char[] array = lineBuffer.array(); int offset = lineBuffer.arrayOffset() + lineBuffer.position(); int length = lineBuffer.remaining(); return new BigDecimal(array, offset, length); } else { return new BigDecimal(lineBuffer.toString()); } }
private Status createStatusInLine(Reason reason, String expected) { return new Status( reason, path, currentPhysicalHeadLine, currentRecordNumber, cellBeginPositions.position(), expected, lineBuffer.toString()); }
public synchronized String getc(ThreadContext tc) { try { int maxBytes = (int) enc.maxBytesPerChar(); ByteBuffer toDecode = ByteBuffer.allocate(maxBytes); CharBuffer decoded = CharBuffer.allocate(1); for (int i = 0; i < maxBytes; i++) { /* Ensure we have a read buffer available. */ if (readBuffer == null) { readBuffer = ByteBuffer.allocate(32768); if (chan.read(readBuffer) == -1) { /* End of file. */ eof = true; if (i == 0) { /* Fine, just no char. */ return ""; } else { /* Malformed; following will likely throw. */ toDecode.position(0); dec.decode(toDecode, decoded, true).throwException(); return decoded.toString(); } } readBuffer.flip(); } /* Get a character from the read buffer. */ toDecode.position(i); toDecode.put(readBuffer.get()); if (readBuffer.remaining() == 0) readBuffer = null; /* Try to decode; if we fail, try another byte. */ toDecode.position(0); CoderResult cr = dec.decode(toDecode, decoded, false); if (!cr.isError()) { decoded.rewind(); return decoded.toString(); } } throw new MalformedInputException(maxBytes); } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
protected static String guessEncoding(Blob blob) throws IOException { // encoding already known? if (blob.getEncoding() != null) { return null; } // bad mime type? String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { // not a text file, we shouldn't be in the Note importer return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); // charset specified in MIME type? String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } // resort to auto-detection for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { // illegal charset } catch (CharacterCodingException e) { // could not decode } } // nothing worked, use platform return null; }
@SuppressWarnings("deprecation") @Override public void fill(StringOption option) throws CsvFormatException, IOException { seekBuffer(); if (lineBuffer.hasRemaining()) { String value = lineBuffer.toString(); option.modify(value); } else { option.setNull(); } }
/** Decode a string into a ByteBuffer */ public static String fromByteBuffer(ByteBuffer bb, CharsetDecoder dec) { if (bb.remaining() == 0) return ""; dec.reset(); CharBuffer cBuff = CharBuffer.allocate(bb.remaining()); CoderResult r = dec.decode(bb, cBuff, true); if (r.isOverflow()) throw new InternalErrorException("fromByteBuffer: decode overflow (1)"); r = dec.flush(cBuff); if (r.isOverflow()) throw new InternalErrorException("fromByteBuffer: decode overflow (2)"); cBuff.flip(); return cBuff.toString(); }
public static String byteBufferToString(ByteBuffer buffer) { CharBuffer charBuffer = null; try { Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); buffer.flip(); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
/** {@inheritDoc} */ @Override public void close() throws IOException { if (output == null) { output = CharBuffer.allocate(8); } else { output.clear(); } encoder.finalize(output); output.flip(); writer.write(output.toString()); writer.flush(); writer.close(); }
/** {@inheritDoc} */ @Override public void write(final byte[] b, final int off, final int len) throws IOException { final ByteBuffer input = ByteBuffer.wrap(b, off, len); final int required = encoder.outputSize(len - off); if (output == null || output.capacity() < required) { output = CharBuffer.allocate(required); } else { output.clear(); } encoder.encode(input, output); output.flip(); writer.write(output.toString()); writer.flush(); }
public String decode(ByteBuffer buffer) { Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return ""; } }
public static String stringDecode(ByteBuffer oByteBuffer) { CharBuffer oDecodedChat; Charset charset = Charset.forName("utf8"); CharsetDecoder decoder = charset.newDecoder(); try { oDecodedChat = decoder.decode(oByteBuffer); return oDecodedChat.toString(); } catch (CharacterCodingException ex) { ex.printStackTrace(); } return null; }
public static String getString(ByteBuffer buffer) { Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); // charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空 charBuffer = decoder.decode(buffer.asReadOnlyBuffer()); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return ""; } }
public void testAppendableInterface() { CharTermAttributeImpl t = new CharTermAttributeImpl(); Formatter formatter = new Formatter(t, Locale.ROOT); formatter.format("%d", 1234); assertEquals("1234", t.toString()); formatter.format("%d", 5678); assertEquals("12345678", t.toString()); t.append('9'); assertEquals("123456789", t.toString()); t.append((CharSequence) "0"); assertEquals("1234567890", t.toString()); t.append((CharSequence) "0123456789", 1, 3); assertEquals("123456789012", t.toString()); t.append((CharSequence) CharBuffer.wrap("0123456789".toCharArray()), 3, 5); assertEquals("12345678901234", t.toString()); t.append((CharSequence) t); assertEquals("1234567890123412345678901234", t.toString()); t.append((CharSequence) new StringBuilder("0123456789"), 5, 7); assertEquals("123456789012341234567890123456", t.toString()); t.append((CharSequence) new StringBuffer(t)); assertEquals("123456789012341234567890123456123456789012341234567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("0123456789".toCharArray(), 3, 5); assertEquals("34567", buf.toString()); t.setEmpty().append((CharSequence) buf, 1, 2); assertEquals("4", t.toString()); CharTermAttribute t2 = new CharTermAttributeImpl(); t2.append("test"); t.append((CharSequence) t2); assertEquals("4test", t.toString()); t.append((CharSequence) t2, 1, 2); assertEquals("4teste", t.toString()); try { t.append((CharSequence) t2, 1, 5); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.append((CharSequence) t2, 1, 0); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } t.append((CharSequence) null); assertEquals("4testenull", t.toString()); }
@Override public void endRecord() throws CsvFormatException, IOException { if (cellBeginPositions.remaining() > 1) { seekBuffer(); throw new CsvFormatException( new Status( Reason.TOO_LONG_RECORD, path, currentPhysicalHeadLine, currentRecordNumber, cellBeginPositions.position(), CHAR_END_OF_RECORD, lineBuffer.toString()), null); } }
public void testAppendableInterfaceWithLongSequences() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append((CharSequence) "01234567890123456789012345678901234567890123456789"); t.append( (CharSequence) CharBuffer.wrap("01234567890123456789012345678901234567890123456789".toCharArray()), 3, 50); assertEquals( "0123456789012345678901234567890123456789012345678934567890123456789012345678901234567890123456789", t.toString()); t.setEmpty().append((CharSequence) new StringBuilder("01234567890123456789"), 5, 17); assertEquals((CharSequence) "567890123456", t.toString()); t.append(new StringBuffer(t)); assertEquals((CharSequence) "567890123456567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("012345678901234567890123456789".toCharArray(), 3, 15); assertEquals("345678901234567", buf.toString()); t.setEmpty().append(buf, 1, 14); assertEquals("4567890123456", t.toString()); // finally use a completely custom CharSequence that is not catched by instanceof checks final String longTestString = "012345678901234567890123456789"; t.append( new CharSequence() { @Override public char charAt(int i) { return longTestString.charAt(i); } @Override public int length() { return longTestString.length(); } @Override public CharSequence subSequence(int start, int end) { return longTestString.subSequence(start, end); } @Override public String toString() { return longTestString; } }); assertEquals("4567890123456" + longTestString, t.toString()); }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("java ScalaInterpretNIO [script file]"); return; } FileChannel fc = new FileInputStream(args[0]).getChannel(); MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); CharBuffer chb = Charset.defaultCharset().decode(buffer); Interpreter p = new Interpreter(new Settings()); p.interpret(chb.toString()); p.close(); }
public String decode(byte[] bytes, int start, int length) { CharBuffer cbuffer; if (length > CHAR_THRESHOLD) { cbuffer = UTF8.decode(ByteBuffer.wrap(bytes, start, length)); } else { cbuffer = charBuffer; ByteBuffer buffer = byteBuffer; cbuffer.clear(); buffer.clear(); buffer.put(bytes, start, length); buffer.flip(); decoder.decode(buffer, cbuffer, true); cbuffer.flip(); } return cbuffer.toString(); }
/** * @param buf UTF-8 encoded bytes * @return String UTF-8 decoded result */ public static String asString(ByteBuffer buf) { if (buf == null) { return null; } if (buf.hasArray()) { return new String(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining(), UTF_8); } try { // Direct buffer CharsetDecoder decoder = UTF_8.newDecoder(); CharBuffer charBuf = decoder.decode(buf.duplicate()); return charBuf.toString(); } catch (CharacterCodingException e) { throw new RuntimeException(e); } }
/** * Decodes buffer containing a String * * @param buffer the buffer received in a message * @return the encodeed String */ public static String decodeString(Buffer buffer) { if (buffer == null || buffer.buffer() == null) { return null; } ByteBuffer nioBuffer = buffer.buffer().nioBuffer(); synchronized (decoder) { decoder.reset(); CharBuffer decoded; try { decoded = decoder.decode(nioBuffer); } catch (CharacterCodingException e) { return null; } decoder.flush(decoded); return decoded.toString(); } }
/** Gets String contents from channel and closes it. */ public static String getStringContents(ReadableByteChannel channel) throws IOException { // TODO Checks if a supplier would be nice try { ByteBuffer buffer = ByteBuffer.allocate(1024 * 8); StringBuilder sb = new StringBuilder(); int bytesRead = channel.read(buffer); while (bytesRead != -1) { buffer.flip(); CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer); sb.append(charBuffer.toString()); buffer.clear(); bytesRead = channel.read(buffer); } return sb.toString(); } finally { channel.close(); } }