List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream
public ByteBufInputStream(ByteBuf buffer)
From source file:org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(), recordsRead, lastRecordBytes); if (lastRecordBytes == 0) { if (buf.readableBytes() < 4) { return; //wait for more data }//from w w w . j a v a 2s . c o m skipSpaces(buf); byte[] buff = new byte[4]; buf.getBytes(buf.readerIndex(), buff); ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4); JsonEncoding jsonEncoding = strapper.detectEncoding(); if (!JsonEncoding.UTF8.equals(jsonEncoding)) { throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported"); } } int i = lastRecordBytes + buf.readerIndex(); for (; i < buf.writerIndex(); i++) { switch (buf.getByte(i)) { case '{': if (!inS) leftCurlies++; break; case '}': if (!inS) rightCurlies++; break; case '"': { if (buf.getByte(i - 1) != '\\') inS = !inS; break; } default: break; } if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) { ByteBuf slice = buf.readSlice(1 + i - buf.readerIndex()); JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice)); JsonNode root = jp.readValueAsTree(); out.add(root); leftCurlies = rightCurlies = lastRecordBytes = 0; recordsRead++; break; } if (i - buf.readerIndex() >= maxFrameLength) { fail(ctx, i - buf.readerIndex()); } } // end of stream, save the incomplete record index to avoid reexamining the whole on next run if (i >= buf.writerIndex()) { lastRecordBytes = buf.readableBytes(); return; } }
From source file:org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder.java
License:Open Source License
@Override public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException { if (in.isReadable()) { if (LOG.isTraceEnabled()) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); }/*from w w w . j a v a 2 s . co m*/ /* According to the XML 1.0 specifications, when there is an XML declaration * at the beginning of an XML document, it is invalid to have * white spaces before that declaration (reminder: a XML declaration looks like: * <?xml version="1.0" encoding="UTF-8"?>). In contrast, when there is no XML declaration, * it is valid to have white spaces at the beginning of the document. * * When they send a NETCONF message, several NETCONF servers start with a new line (either * LF or CRLF), presumably to improve readability in interactive sessions with a human being. * Some NETCONF servers send an XML declaration, some others do not. * * If a server starts a NETCONF message with white spaces and follows with an XML * declaration, XmlUtil.readXmlToDocument() will fail because this is invalid XML. * But in the spirit of the "NETCONF over SSH" RFC 4742 and to improve interoperability, we want * to accept those messages. * * To do this, the following code strips the leading bytes before the start of the XML messages. */ // Skip all leading whitespaces by moving the reader index to the first non whitespace character while (in.isReadable()) { if (!isWhitespace(in.readByte())) { // return reader index to the first non whitespace character in.readerIndex(in.readerIndex() - 1); break; } } // Warn about leading whitespaces if (in.readerIndex() != 0 && LOG.isWarnEnabled()) { final byte[] strippedBytes = new byte[in.readerIndex()]; in.getBytes(0, strippedBytes, 0, in.readerIndex()); LOG.warn("XML message with unwanted leading bytes detected. Discarded the {} leading byte(s): '{}'", in.readerIndex(), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(strippedBytes))); } } if (in.isReadable()) { out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)))); } else { LOG.debug("No more content in incoming buffer."); } }
From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(), recordsRead, lastRecordBytes); if (lastRecordBytes == 0) { if (buf.readableBytes() < 4) { return; //wait for more data }//from w ww . j a va2 s . co m skipSpaces(buf); byte[] buff = new byte[4]; buf.getBytes(buf.readerIndex(), buff); ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4); JsonEncoding jsonEncoding = strapper.detectEncoding(); if (!JsonEncoding.UTF8.equals(jsonEncoding)) { throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported"); } } int index = lastRecordBytes + buf.readerIndex(); for (; index < buf.writerIndex(); index++) { switch (buf.getByte(index)) { case '{': if (!inS) { leftCurlies++; } break; case '}': if (!inS) { rightCurlies++; } break; case '"': if (buf.getByte(index - 1) != '\\') { inS = !inS; } break; default: break; } if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) { ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex()); JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice)); JsonNode root = jp.readValueAsTree(); out.add(root); leftCurlies = rightCurlies = lastRecordBytes = 0; recordsRead++; break; } if (index - buf.readerIndex() >= maxFrameLength) { fail(ctx, index - buf.readerIndex()); } } // end of stream, save the incomplete record index to avoid reexamining the whole on next run if (index >= buf.writerIndex()) { lastRecordBytes = buf.readableBytes(); return; } }
From source file:org.quartzpowered.protocol.codec.v1_8_R1.play.client.UpdateEntityNbtCodec.java
License:Open Source License
@Override public void decode(Buffer buffer, UpdateEntityNbtPacket packet) { packet.setEntityId(buffer.readVarInt()); try {//from w w w . ja va 2 s.c om NBTInputStream nis = new NBTInputStream(new ByteBufInputStream(buffer)); packet.setTag(nis.readTag()); nis.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.ratpackframework.http.internal.DefaultRequest.java
License:Apache License
@Override public InputStream getInputStream() { return new ByteBufInputStream(getBuffer()); }
From source file:org.redisson.client.codec.JsonJacksonMapValueCodec.java
License:Apache License
@Override public Decoder<Object> getMapValueDecoder() { return new Decoder<Object>() { @Override/*w ww . j a v a 2 s. c o m*/ public Object decode(ByteBuf buf, State state) throws IOException { return mapper.readValue(new ByteBufInputStream(buf), typeReference); } }; }
From source file:org.redisson.codec.TypedJsonJacksonCodec.java
License:Apache License
private Decoder<Object> createDecoder(final Class<?> valueClass, final TypeReference<?> valueTypeReference) { return new Decoder<Object>() { @Override/*ww w.j a v a 2 s .com*/ public Object decode(ByteBuf buf, State state) throws IOException { if (valueClass != null) { return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), valueClass); } if (valueTypeReference != null) { return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), valueTypeReference); } return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), Object.class); } }; }
From source file:org.restexpress.Request.java
License:Apache License
/** * Returns the request body as an InputStream. * /* ww w .jav a 2s . c om*/ * @return an InputStream */ public InputStream getBodyAsStream() { return new ByteBufInputStream(getBody()); }
From source file:org.restexpress.serialization.json.GsonJsonProcessor.java
License:Apache License
@Override public <T> T deserialize(ByteBuf buffer, Class<T> type) { return gson.fromJson(new InputStreamReader(new ByteBufInputStream(buffer), ContentType.CHARSET), type); }
From source file:org.restexpress.serialization.json.JacksonJsonProcessor.java
License:Apache License
@Override public <T> T deserialize(ByteBuf buffer, Class<T> type) { try {/*from w ww. ja v a2 s. c o m*/ return (buffer == null || buffer.readableBytes() == 0 ? null : mapper.readValue(new InputStreamReader(new ByteBufInputStream(buffer), ContentType.CHARSET), type)); } catch (JsonProcessingException e) { throw new DeserializationException(e); } catch (IOException e) { throw new DeserializationException(e); } }