本文整理了Java中io.micronaut.core.io.buffer.ByteBuffer
类的一些代码示例,展示了ByteBuffer
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer
类的具体详情如下:
包路径:io.micronaut.core.io.buffer.ByteBuffer
类名称:ByteBuffer
[英]Interface to allow interfacing with different byte buffer implementations, primarily as an abstraction over Netty.
[中]接口,允许与不同的字节缓冲区实现接口,主要作为Netty上的抽象。
代码示例来源:origin: micronaut-projects/micronaut-core
private ByteBuf charSequenceToByteBuf(CharSequence bodyValue, MediaType requestContentType) {
CharSequence charSequence = bodyValue;
return byteBufferFactory.copiedBuffer(
charSequence.toString().getBytes(
requestContentType.getCharset().orElse(defaultCharset)
)
).asNativeBuffer();
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public <T> byte[] encode(T object) throws CodecException {
ByteBuffer buffer = encode(object, byteBufferFactory);
return buffer.toByteArray();
}
代码示例来源:origin: micronaut-projects/micronaut-core
} else if (body instanceof ByteBuffer) {
ByteBuffer byteBuffer = (ByteBuffer) body;
Object nativeBuffer = byteBuffer.asNativeBuffer();
if (nativeBuffer instanceof ByteBuf) {
byteBuf = (ByteBuf) nativeBuffer;
} else {
byteBuf = Unpooled.wrappedBuffer(byteBuffer.asNioBuffer());
LOG.debug("Encoding emitted response object [{}] using codec: {}", body, codec);
byteBuf = (ByteBuf) codec.encode(body, new NettyByteBufferFactory(context.alloc())).asNativeBuffer();
代码示例来源:origin: micronaut-projects/micronaut-core
body = jsonCodec.encode(data, allocator);
ByteBuffer eventData = allocator.buffer(body.readableBytes() + 10);
writeAttribute(eventData, COMMENT_PREFIX, event.getComment());
writeAttribute(eventData, ID_PREFIX, event.getId());
eventData.write(DATA_PREFIX)
.write(body)
.write(NEWLINE) // Write new lines for event separation
.write(NEWLINE);
return eventData;
代码示例来源:origin: io.micronaut/runtime
@Override
public <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException {
try {
if (CharSequence.class.isAssignableFrom(type.getType())) {
return (T) buffer.toString(applicationConfiguration.getDefaultCharset());
} else if (type.hasTypeVariables()) {
JavaType javaType = constructJavaType(type);
return objectMapper.readValue(buffer.toByteArray(), javaType);
} else {
return objectMapper.readValue(buffer.toByteArray(), type.getType());
}
} catch (IOException e) {
throw new CodecException("Error decoding JSON stream for type [" + type.getType() + "]: " + e.getMessage());
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @param eventData The byte buffer
* @param attribute The attribute
* @param value The value
*/
protected void writeAttribute(ByteBuffer eventData, byte[] attribute, String value) {
if (value != null) {
eventData.write(attribute)
.write(value, defaultCharset)
.write(NEWLINE);
}
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public ByteBuffer write(ByteBuffer... buffers) {
if (ArrayUtils.isNotEmpty(buffers)) {
ByteBuf[] byteBufs = Arrays.stream(buffers)
.map(buffer -> {
if (buffer instanceof NettyByteBuffer) {
return ((NettyByteBuffer) buffer).asNativeBuffer();
} else {
return Unpooled.wrappedBuffer(buffer.asNioBuffer());
}
}).toArray(ByteBuf[]::new);
return write(byteBufs);
}
return this;
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances
* should override this method.
*
* @param type The type
* @param buffer the buffer
* @param <T> The decoded type
* @return The decoded result
* @throws CodecException When the result cannot be decoded
*/
default <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException {
return decode(type, buffer.toInputStream());
}
代码示例来源:origin: io.micronaut/http-server
body = jsonCodec.encode(data, allocator);
ByteBuffer eventData = allocator.buffer(body.readableBytes() + 10);
writeAttribute(eventData, COMMENT_PREFIX, event.getComment());
writeAttribute(eventData, ID_PREFIX, event.getId());
eventData.write(DATA_PREFIX)
.write(body)
.write(NEWLINE) // Write new lines for event separation
.write(NEWLINE);
return eventData;
代码示例来源:origin: io.micronaut/http-server
/**
* @param eventData The byte buffer
* @param attribute The attribute
* @param value The value
*/
protected void writeAttribute(ByteBuffer eventData, byte[] attribute, String value) {
if (value != null) {
eventData.write(attribute)
.write(value, serverConfiguration.getDefaultCharset())
.write(NEWLINE);
}
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
private void encodeInput(I input, InvokeRequest invokeRequest) {
if (input != null) {
ByteBuffer byteBuffer = jsonMediaTypeCodec.encode(input, byteBufferFactory).asNioBuffer();
invokeRequest.setPayload(byteBuffer);
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances
* should override this method.
*
* @param type The type
* @param buffer the buffer
* @param <T> The decoded type
* @return The decoded result
* @throws CodecException When the result cannot be decoded
*/
default <T> T decode(Class<T> type, ByteBuffer<?> buffer) throws CodecException {
return decode(type, buffer.toInputStream());
}
代码示例来源:origin: micronaut-projects/micronaut-core
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getBody(Argument<T> type) {
return convertedBodies.computeIfAbsent(type.getType(), aClass -> getBody().flatMap(b -> {
ArgumentConversionContext<T> context = ConversionContext.of(type);
if (b instanceof ByteBuffer) {
return conversionService.convert(((ByteBuffer) b).asNativeBuffer(), context);
}
return conversionService.convert(b, context);
}));
}
代码示例来源:origin: micronaut-projects/micronaut-core
} else if (body instanceof ByteBuffer) {
ByteBuffer byteBuffer = (ByteBuffer) body;
Object nativeBuffer = byteBuffer.asNativeBuffer();
if (nativeBuffer instanceof ByteBuf) {
byteBuf = (ByteBuf) nativeBuffer;
} else {
byteBuf = Unpooled.wrappedBuffer(byteBuffer.asNioBuffer());
LOG.debug("Encoding emitted response object [{}] using codec: {}", body, codec);
byteBuf = (ByteBuf) codec.encode(body, new NettyByteBufferFactory(context.alloc())).asNativeBuffer();
代码示例来源:origin: micronaut-projects/micronaut-core
byte[] result = object.toByteArray();
((ReferenceCounted) object).release();
return Optional.of(result);
代码示例来源:origin: io.micronaut/runtime
@Override
public <T> ByteBuffer encode(T object, ByteBufferFactory allocator) throws CodecException {
byte[] bytes = encode(object);
int len = bytes.length;
return allocator.buffer(len, len).write(bytes);
}
}
代码示例来源:origin: io.micronaut/micronaut-http
/**
* Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances
* should override this method.
*
* @param type The type
* @param buffer the buffer
* @param <T> The decoded type
* @return The decoded result
* @throws CodecException When the result cannot be decoded
*/
default <T> T decode(Class<T> type, ByteBuffer<?> buffer) throws CodecException {
return decode(type, buffer.toInputStream());
}
代码示例来源:origin: micronaut-projects/micronaut-core
ByteBuf bytebuf = (ByteBuf) ((ByteBuffer) b).asNativeBuffer();
return convertByteBuf(bytebuf, finalArgument);
} else {
代码示例来源:origin: io.micronaut/http-server
@Override
public <T> byte[] encode(T object) throws CodecException {
ByteBuffer buffer = encode(object, byteBufferFactory);
return buffer.toByteArray();
}
代码示例来源:origin: io.micronaut/micronaut-http
/**
* Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances
* should override this method.
*
* @param type The type
* @param buffer the buffer
* @param <T> The decoded type
* @return The decoded result
* @throws CodecException When the result cannot be decoded
*/
default <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException {
return decode(type, buffer.toInputStream());
}
内容来源于网络,如有侵权,请联系作者删除!