本文整理汇总了Java中org.glassfish.grizzly.Buffer类的典型用法代码示例。如果您正苦于以下问题:Java Buffer类的具体用法?Java Buffer怎么用?Java Buffer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Buffer类属于org.glassfish.grizzly包,在下文中一共展示了Buffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleWrite
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
Connection<?> connection = context.getConnection();
GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
try {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024); // 不需要关闭
Object msg = context.getMessage();
codec.encode(channel, channelBuffer, msg);
GrizzlyChannel.removeChannelIfDisconnectd(connection);
Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
buffer.put(channelBuffer.toByteBuffer());
buffer.flip();
buffer.allowBufferDispose(true);
context.setMessage(buffer);
} finally {
GrizzlyChannel.removeChannelIfDisconnectd(connection);
}
return context.getInvokeAction();
}
示例2: handleWrite
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
Connection<?> connection = context.getConnection();
GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
try {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024); // 不需要关闭
Object msg = context.getMessage();
codec.encode(channel, channelBuffer, msg);
GrizzlyChannel.removeChannelIfDisconnectd(connection);
Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
buffer.put(channelBuffer.toByteBuffer());
buffer.flip();
buffer.allowBufferDispose(true);
context.setMessage(buffer);
} finally {
GrizzlyChannel.removeChannelIfDisconnectd(connection);
}
return context.getInvokeAction();
}
示例3: transformImpl
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
protected final TransformationResult<MessageLite, Buffer> transformImpl(
final AttributeStorage storage, final @NonNull MessageLite input)
throws TransformationException {
final MemoryManager memoryManager = obtainMemoryManager(storage);
final BufferOutputStream outputStream = new BufferOutputStream(memoryManager);
final byte[] encodedMessage = input.toByteArray();
try {
writeHeader(outputStream, encodedMessage.length);
outputStream.write(encodedMessage);
outputStream.close();
} catch (final IOException e) {
final String msg = "Error writing protobuf message to output stream.";
log.warn(msg, e);
return createErrorResult(IO_WRITE_ERROR, msg);
}
return createCompletedResult(outputStream.getBuffer().flip(), null);
}
示例4: transformImpl
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
protected TransformationResult<Request, Buffer> transformImpl(AttributeStorage storage, Request request) throws TransformationException {
byte ver = request.getHeader().getSerializeVersion() ;
int pLen = request.getPayload() == null ? 0 : request.getPayload().length;
byte[] header = request.getHeader().toByte();
// encoding length , and check the max value of integer, should be 2GB
int totalLen = OFFSET + header.length + pLen + R_SIZE + H_SIZE;
if ( totalLen > Integer.MAX_VALUE ) {
throw new TransformationException( totalLen +" exceeding Integer.MAX_VALUE");
}
//logger.info("total "+totalLen+" hd "+header.length+" payload "+pLen);
// Retrieve the memory manager
//final MemoryManager memoryManager = ctx.getConnection().getTransport().getMemoryManager();
// allocate the buffer of required size
final Buffer bout = obtainMemoryManager(storage).allocate(totalLen);
// Instruct the FilterChain to call the next filter
bout.put(SIGNATURE);
bout.put(ver);
bout.putInt(totalLen);
bout.putShort((short) header.length);
bout.put(header);
// request type
bout.put((byte) request.getType().ordinal());
bout.put( request.getPayload());
bout.flip();
bout.allowBufferDispose(true);
return TransformationResult.createCompletedResult(bout, null);
}
示例5: transformImpl
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
protected TransformationResult<Buffer, Request> transformImpl(AttributeStorage storage, Buffer input) throws TransformationException {
//Integer packSize = lengthAttribute.get(storage);
VersionLength versionLength = lengthAttribute.get(storage);
if (versionLength == null) {
if (input.remaining() < OFFSET) {
return TransformationResult.createIncompletedResult(input);
}
byte signature = input.get();
byte ver = input.get();
int packSize = input.getInt();
if (signature != SIGNATURE ) {
//channel.close();
throw new TransformationException("expect "+SIGNATURE+" but get "+signature+" "+" "+input.toString());
}
if ( packSize < 0 || packSize > MAX_LEN ) {
//channel.close();
throw new TransformationException(packSize+" Exceed Integer.MAX_VALUE or MAX_LEN");
}
//use versionlength
versionLength = new VersionLength(packSize, ver);
//lengthAttribute.set(storage, packSize);
lengthAttribute.set(storage, versionLength);
}
if (input.remaining() < (versionLength.getLength() -OFFSET) ) {
//logger.info("packSize "+versionLength.getLength() + " remaining "+input.remaining() );
return TransformationResult.createIncompletedResult(input);
}
int tmpLimit = input.limit();
input.limit(input.position() + (versionLength.getLength() -OFFSET) );
Request request= doDecode(input, versionLength.getLength(), versionLength.getVersion() );
input.position(input.limit());
input.limit(tmpLimit);
return TransformationResult.createCompletedResult(
request, input);
}
示例6: doDecode
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
protected Request doDecode(Buffer buffer, int len, byte ver) {
//read all byte from buffer first, in case exception during de serialize
short headLen = buffer.getShort();
byte[] bytes = new byte[ (int) headLen];
buffer.get( bytes);
//read request type before payload
byte type = buffer.get();
byte[] payload = new byte[ len - headLen - OFFSET - R_SIZE - H_SIZE ];
buffer.get( payload);
Header header = Header.toHeader( bytes);
// assign serializeVersion
header.setSerializeVersion( ver);
return new Request( header, payload, getRequestType( type));
}
示例7: handleWrite
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
/**
* Method is called, when we write a data to the Connection.
*
* We override this method to perform GIOPMessage -> Buffer transformation.
*
* @param ctx Context of {@link FilterChainContext} processing
* @return the next action
* @throws java.io.IOException
*/
@Override
public NextAction handleWrite(final FilterChainContext ctx) throws IOException {
final Request request = ctx.getMessage();
byte ver = version;
int pLen = request.getPayload() == null ? 0 : request.getPayload().length;
byte[] header = request.getHeader().toByte();
// encoding length , and check the max value of integer, should be 2GB
int totalLen = TOTAL + header.length + pLen + R_SIZE;
if ( totalLen > Integer.MAX_VALUE ) {
throw new IOException( totalLen +" exceeding Integer.MAX_VALUE");
}
logger.info("total "+totalLen+" hd "+header.length+" payload "+pLen);
// Retrieve the memory manager
final MemoryManager memoryManager =
ctx.getConnection().getTransport().getMemoryManager();
// allocate the buffer of required size
final Buffer bout = memoryManager.allocate(totalLen);
// Instruct the FilterChain to call the next filter
bout.put(SIGNATURE);
bout.put(ver);
bout.putInt(totalLen);
bout.putShort((short) header.length);
bout.put(header);
// request type
bout.put((byte) request.getType().ordinal());
bout.put( request.getPayload());
// Set the Buffer as a context message
ctx.setMessage( bout.flip() );
return ctx.getInvokeAction();
}
示例8: doDecode
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
protected Request doDecode(Buffer buffer, int len, byte ver) {
//read all byte from buffer first, in case exception during de serialize
short headLen = buffer.getShort();
byte[] bytes = new byte[ (int) headLen];
buffer.get( bytes);
//read request type before payload
byte type = buffer.get();
byte[] payload = new byte[ len - headLen - TOTAL - R_SIZE ];
buffer.get( payload);
Header header = Header.toHeader( bytes);
// assign serializeVersion
header.setSerializeVersion( ver);
return new Request( header, payload, getRequestType( type));
}
示例9: handleWrite
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
public NextAction handleWrite(FilterChainContext ctx) throws IOException {
GrizzlyByteBufferWrapper wrapper = new GrizzlyByteBufferWrapper(ctx);
try {
ProtocolUtils.encode(ctx.getMessage(), wrapper);
final Buffer buffer = wrapper.getBuffer();
buffer.trim();
ctx.setMessage(buffer);
return ctx.getInvokeAction();
} catch (Exception e) {
throw new IOException("encode message to byte error", e);
}
}
示例10: handleWrite
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
public NextAction handleWrite(FilterChainContext ctx) throws IOException {
// final Object message = ctx.getMessage();
//
// HeapBuffer buffer = (HeapBuffer) message;
// byte[] bytes = new byte[buffer.remaining()];
// buffer.get(bytes);
// byte[] encoded = binaryEncryptor.encrypt(bytes);
// Buffer buf = new HeapMemoryManager().wrap(encoded);
// ctx.setMessage(buf);
System.out.println(System.nanoTime() + ":write:"
+ ((Buffer) ctx.getMessage()).remaining());
return super.handleWrite(ctx);
}
示例11: addToBuffer
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
public void addToBuffer(NIOInputStream in, ByteBuffer postBytes){
Buffer buffer = in.readBuffer();
ByteBuffer bb = buffer.toByteBuffer();
try {
postBytes.put(bb);
} finally {
buffer.tryDispose();
}
}
示例12: readHeader
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public int readHeader(final Buffer input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("'input' cannot be null.");
}
final byte[] messageLengthArr = new byte[headerLength];
input.get(messageLengthArr);
log.debug("inputRemaining={}", input.remaining());
return ByteBuffer.wrap(messageLengthArr).getInt();
}
示例13: readHeader
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public int readHeader(final Buffer input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("'input' cannot be null.");
}
final BufferInputStream inputStream = new BufferInputStream(input);
return CodedInputStream.readRawVarint32(input.get(), inputStream);
}
示例14: handleRead
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
public NextAction handleRead(FilterChainContext context) throws IOException {
Object message = context.getMessage();
Connection<?> connection = context.getConnection();
Channel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
try {
if (message instanceof Buffer) { // 收到新的数据包
Buffer grizzlyBuffer = (Buffer) message; // 缓存
ChannelBuffer frame;
if (previousData.readable()) {
if (previousData instanceof DynamicChannelBuffer) {
previousData.writeBytes(grizzlyBuffer.toByteBuffer());
frame = previousData;
} else {
int size = previousData.readableBytes() + grizzlyBuffer.remaining();
frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize);
frame.writeBytes(previousData, previousData.readableBytes());
frame.writeBytes(grizzlyBuffer.toByteBuffer());
}
} else {
frame = ChannelBuffers.wrappedBuffer(grizzlyBuffer.toByteBuffer());
}
Object msg;
int savedReadIndex;
do {
savedReadIndex = frame.readerIndex();
try {
msg = codec.decode(channel, frame);
} catch (Exception e) {
previousData = ChannelBuffers.EMPTY_BUFFER;
throw new IOException(e.getMessage(), e);
}
if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) {
frame.readerIndex(savedReadIndex);
return context.getStopAction();
} else {
if (savedReadIndex == frame.readerIndex()) {
previousData = ChannelBuffers.EMPTY_BUFFER;
throw new IOException("Decode without read data.");
}
if (msg != null) {
context.setMessage(msg);
return context.getInvokeAction();
} else {
return context.getInvokeAction();
}
}
} while (frame.readable());
} else { // 其它事件直接往下传
return context.getInvokeAction();
}
} finally {
GrizzlyChannel.removeChannelIfDisconnectd(connection);
}
}
示例15: service
import org.glassfish.grizzly.Buffer; //导入依赖的package包/类
@Override
public void service(Request request, Response response) throws Exception {
request.setCharacterEncoding("UTF-8");
Buffer postBody = request.getPostBody(1024);
String text = postBody.toStringContent();
if (request.getParameter("text") != null) {
text = request.getParameter("text");
}
String outputFormat = request.getParameter("format");
InputStream inputStream = new ByteArrayInputStream(text.getBytes());
OutputStream outputStream = new ByteArrayOutputStream();
TintRunner.OutputFormat format = TintRunner.getOutputFormat(outputFormat, TintRunner.OutputFormat.JSON);
pipeline.run(inputStream, outputStream, format);
LOGGER.debug("Text: {}", text);
String output = outputStream.toString();
writeOutput(response, contentTypes.get(format), output);
}