use of io.netty.buffer.ByteBufInputStream in project intellij-community by JetBrains.
the class RestService method createJsonReader.
@NotNull
protected static JsonReader createJsonReader(@NotNull FullHttpRequest request) {
JsonReader reader = new JsonReader(new InputStreamReader(new ByteBufInputStream(request.content()), CharsetToolkit.UTF8_CHARSET));
reader.setLenient(true);
return reader;
}
use of io.netty.buffer.ByteBufInputStream in project intellij-community by JetBrains.
the class XmlRpcServerImpl method process.
@Override
public boolean process(@NotNull String path, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context, @Nullable Map<String, Object> handlers) {
if (!(path.isEmpty() || (path.length() == 1 && path.charAt(0) == '/') || path.equalsIgnoreCase("/rpc2"))) {
return false;
}
if (request.method() != HttpMethod.POST) {
return false;
}
ByteBuf content = request.content();
if (content.readableBytes() == 0) {
Responses.send(HttpResponseStatus.BAD_REQUEST, context.channel(), request);
return true;
}
ByteBuf result;
try (ByteBufInputStream in = new ByteBufInputStream(content)) {
XmlRpcServerRequest xmlRpcServerRequest = new XmlRpcRequestProcessor().decodeRequest(in);
if (StringUtil.isEmpty(xmlRpcServerRequest.getMethodName())) {
LOG.warn("method name empty");
return false;
}
Object response = invokeHandler(getHandler(xmlRpcServerRequest.getMethodName(), handlers == null ? handlerMapping : handlers), xmlRpcServerRequest);
result = Unpooled.wrappedBuffer(new XmlRpcResponseProcessor().encodeResponse(response, CharsetToolkit.UTF8));
} catch (SAXParseException e) {
LOG.warn(e);
Responses.send(HttpResponseStatus.BAD_REQUEST, context.channel(), request);
return true;
} catch (Throwable e) {
context.channel().close();
LOG.error(e);
return true;
}
Responses.send(Responses.response("text/xml", result), context.channel(), request);
return true;
}
use of io.netty.buffer.ByteBufInputStream in project CorfuDB by CorfuDB.
the class JSONSerializer method deserialize.
/**
* Deserialize an object from a given byte buffer.
*
* @param b The bytebuf to deserialize.
* @return The deserialized object.
*/
@Override
public Object deserialize(ByteBuf b, CorfuRuntime rt) {
int classNameLength = b.readShort();
byte[] classNameBytes = new byte[classNameLength];
b.readBytes(classNameBytes, 0, classNameLength);
String className = new String(classNameBytes);
if (className.equals("null")) {
return null;
} else if (className.equals("CorfuObject")) {
int SMRClassNameLength = b.readShort();
byte[] SMRClassNameBytes = new byte[SMRClassNameLength];
b.readBytes(SMRClassNameBytes, 0, SMRClassNameLength);
String SMRClassName = new String(SMRClassNameBytes);
try {
return rt.getObjectsView().build().setStreamID(new UUID(b.readLong(), b.readLong())).setType(Class.forName(SMRClassName)).open();
} catch (ClassNotFoundException cnfe) {
log.error("Exception during deserialization!", cnfe);
throw new RuntimeException(cnfe);
}
} else {
try (ByteBufInputStream bbis = new ByteBufInputStream(b)) {
try (InputStreamReader r = new InputStreamReader(bbis)) {
return gson.fromJson(r, Class.forName(className));
}
} catch (IOException | ClassNotFoundException ie) {
log.error("Exception during deserialization!", ie);
throw new RuntimeException(ie);
}
}
}
use of io.netty.buffer.ByteBufInputStream in project flink by apache.
the class KvStateRequestSerializer method deserializeKvStateRequestFailure.
/**
* Deserializes the KvState request failure.
*
* @param buf Buffer to deserialize (expected to be positioned after header)
* @return Deserialized KvStateRequestFailure
*/
public static KvStateRequestFailure deserializeKvStateRequestFailure(ByteBuf buf) throws IOException, ClassNotFoundException {
long requestId = buf.readLong();
Throwable cause;
try (ByteBufInputStream bbis = new ByteBufInputStream(buf);
ObjectInputStream in = new ObjectInputStream(bbis)) {
cause = (Throwable) in.readObject();
}
return new KvStateRequestFailure(requestId, cause);
}
use of io.netty.buffer.ByteBufInputStream in project SpongeCommon by SpongePowered.
the class SpongeFavicon method decode.
private static BufferedImage decode(String encoded) throws IOException {
checkArgument(encoded.startsWith(FAVICON_PREFIX), "Unknown favicon format");
ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), Charsets.UTF_8);
try {
ByteBuf buf = Base64.decode(base64);
try {
BufferedImage result = ImageIO.read(new ByteBufInputStream(buf));
checkState(result.getWidth() == 64, "favicon must be 64 pixels wide");
checkState(result.getHeight() == 64, "favicon must be 64 pixels high");
return result;
} finally {
buf.release();
}
} finally {
base64.release();
}
}
Aggregations