本文整理汇总了Java中org.xnio.channels.StreamSourceChannel.read方法的典型用法代码示例。如果您正苦于以下问题:Java StreamSourceChannel.read方法的具体用法?Java StreamSourceChannel.read怎么用?Java StreamSourceChannel.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xnio.channels.StreamSourceChannel
的用法示例。
在下文中一共展示了StreamSourceChannel.read方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setup
import org.xnio.channels.StreamSourceChannel; //导入方法依赖的package包/类
public void setup(final StreamSourceChannel channel) {
Pooled<ByteBuffer> resource = bufferPool.allocate();
ByteBuffer buffer = resource.getResource();
try {
int r = 0;
do {
r = channel.read(buffer);
if (r == 0) {
channel.getReadSetter().set(this);
channel.resumeReads();
} else if (r == -1) {
stringDone(string.extract());
IoUtils.safeClose(channel);
} else {
buffer.flip();
string.write(buffer);
}
} while (r > 0);
} catch (IOException e) {
error(e);
} finally {
resource.free();
}
}
示例2: handleEvent
import org.xnio.channels.StreamSourceChannel; //导入方法依赖的package包/类
@Override
public void handleEvent(final StreamSourceChannel channel) {
Pooled<ByteBuffer> resource = bufferPool.allocate();
ByteBuffer buffer = resource.getResource();
try {
int r = 0;
do {
r = channel.read(buffer);
if (r == 0) {
return;
} else if (r == -1) {
stringDone(string.extract());
IoUtils.safeClose(channel);
} else {
buffer.flip();
string.write(buffer);
}
} while (r > 0);
} catch (IOException e) {
error(e);
} finally {
resource.free();
}
}
示例3: read
import org.xnio.channels.StreamSourceChannel; //导入方法依赖的package包/类
public void read(StreamSourceChannel channel) {
try (PooledByteBuffer poolItem = exchange.getConnection().getByteBufferPool().allocate()) {
ByteBuffer buffer = poolItem.getBuffer();
int bytesRead;
while (true) {
buffer.clear();
bytesRead = channel.read(buffer);
if (bytesRead <= 0) break;
buffer.flip();
ensureCapacity(bytesRead);
buffer.get(body, position, bytesRead);
position += bytesRead;
}
if (bytesRead == -1) {
if (contentLength >= 0 && position < body.length) {
throw Exceptions.error("body ends prematurely, expected={}, actual={}", contentLength, position);
} else if (body == null) {
body = new byte[0]; // without content length and has no body
}
complete = true;
exchange.putAttachment(REQUEST_BODY, new RequestBody(body, null));
}
} catch (Throwable e) { // catch all errors during IO, to pass error to action log
IoUtils.safeClose(channel);
complete = true;
exchange.putAttachment(REQUEST_BODY, new RequestBody(null, e));
}
}
示例4: handleEvent
import org.xnio.channels.StreamSourceChannel; //导入方法依赖的package包/类
@Override
public void handleEvent(StreamSourceChannel channel) {
if (this.session.isDisconnected()) {
if (logger.isDebugEnabled()) {
logger.debug("SockJS sockJsSession closed, closing response.");
}
IoUtils.safeClose(this.connection);
throw new SockJsException("Session closed.", this.session.getId(), null);
}
Object pooled = undertowBufferSupport.allocatePooledResource();
try {
int r;
do {
ByteBuffer buffer = undertowBufferSupport.getByteBuffer(pooled);
buffer.clear();
r = channel.read(buffer);
buffer.flip();
if (r == 0) {
return;
}
else if (r == -1) {
onSuccess();
}
else {
while (buffer.hasRemaining()) {
int b = buffer.get();
if (b == '\n') {
handleFrame();
}
else {
this.outputStream.write(b);
}
}
}
}
while (r > 0);
}
catch (IOException exc) {
onFailure(exc);
}
finally {
undertowBufferSupport.closePooledResource(pooled);
}
}