本文整理汇总了Java中org.xnio.IoUtils类的典型用法代码示例。如果您正苦于以下问题:Java IoUtils类的具体用法?Java IoUtils怎么用?Java IoUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IoUtils类属于org.xnio包,在下文中一共展示了IoUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: read
import org.xnio.IoUtils; //导入依赖的package包/类
@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
try {
long total = 0;
for (int i = offset; i < length; ++i) {
while (dsts[i].hasRemaining()) {
int r = read(dsts[i]);
if (r <= 0 && total > 0) {
return total;
} else if (r <= 0) {
return r;
} else {
total += r;
}
}
}
return total;
} catch (IOException | RuntimeException e) {
IoUtils.safeClose(exchange.getConnection());
throw e;
}
}
示例2: run
import org.xnio.IoUtils; //导入依赖的package包/类
@Override
public void run() {
if(!connection.isOpen()) {
return;
}
handle = null;
if (expireTime > 0) { //timeout is not active
long now = System.currentTimeMillis();
if(expireTime > now) {
handle = connection.getIoThread().executeAfter(this, (expireTime - now) + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
} else {
UndertowLogger.REQUEST_LOGGER.parseRequestTimedOut(connection.getPeerAddress());
IoUtils.safeClose(connection);
}
}
}
示例3: transferFrom
import org.xnio.IoUtils; //导入依赖的package包/类
public long transferFrom(final FileChannel src, final long position, final long count) throws IOException {
try {
if (state != 0) {
final Pooled<ByteBuffer> pooled = exchange.getConnection().getBufferPool().allocate();
ByteBuffer buffer = pooled.getResource();
try {
int res = src.read(buffer);
if (res <= 0) {
return res;
}
buffer.flip();
return write(buffer);
} finally {
pooled.free();
}
} else {
return next.transferFrom(src, position, count);
}
} catch (IOException | RuntimeException e) {
IoUtils.safeClose(exchange.getConnection());
throw e;
}
}
示例4: sendBadRequestAndClose
import org.xnio.IoUtils; //导入依赖的package包/类
private void sendBadRequestAndClose(final StreamConnection connection, final Exception exception) {
UndertowLogger.REQUEST_IO_LOGGER.failedToParseRequest(exception);
connection.getSourceChannel().suspendReads();
new StringWriteChannelListener(BAD_REQUEST) {
@Override
protected void writeDone(final StreamSinkChannel c) {
super.writeDone(c);
c.suspendWrites();
IoUtils.safeClose(connection);
}
@Override
protected void handleError(StreamSinkChannel channel, IOException e) {
IoUtils.safeClose(connection);
}
}.setup(connection.getSinkChannel());
}
示例5: cancel
import org.xnio.IoUtils; //导入依赖的package包/类
void cancel(final HttpServerExchange exchange) {
final ProxyConnection connectionAttachment = exchange.getAttachment(CONNECTION);
if (connectionAttachment != null) {
ClientConnection clientConnection = connectionAttachment.getConnection();
UndertowLogger.REQUEST_LOGGER.timingOutRequest(clientConnection.getPeerAddress() + "" + exchange.getRequestURI());
IoUtils.safeClose(clientConnection);
} else {
UndertowLogger.REQUEST_LOGGER.timingOutRequest(exchange.getRequestURI());
}
if (exchange.isResponseStarted()) {
IoUtils.safeClose(exchange.getConnection());
} else {
exchange.setResponseCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.endExchange();
}
}
示例6: handleException
import org.xnio.IoUtils; //导入依赖的package包/类
@Override
public void handleException(Channel channel, IOException exception) {
IoUtils.safeClose(channel);
if (exchange.isResponseStarted()) {
IoUtils.safeClose(clientConnection);
UndertowLogger.REQUEST_IO_LOGGER.debug("Exception reading from target server", exception);
if (!exchange.isResponseStarted()) {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
} else {
IoUtils.safeClose(exchange.getConnection());
}
} else {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
}
}
示例7: run
import org.xnio.IoUtils; //导入依赖的package包/类
@Override
public void run() {
handle = null;
if (expireTime == -1) {
return;
}
long current = System.currentTimeMillis();
if (current < expireTime) {
//timeout has been bumped, re-schedule
handle = connection.getIoThread().executeAfter(timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
return;
}
UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity");
IoUtils.safeClose(connection);
if (connection.getSourceChannel().isReadResumed()) {
ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getReadListener());
}
if (connection.getSinkChannel().isWriteResumed()) {
ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener());
}
}
示例8: handleWriteTimeout
import org.xnio.IoUtils; //导入依赖的package包/类
private void handleWriteTimeout(final long ret) throws IOException {
if (!connection.isOpen()) {
return;
}
if (ret == 0 && handle != null) {
return;
}
Integer timeout = getTimeout();
if (timeout == null || timeout <= 0) {
return;
}
long currentTime = System.currentTimeMillis();
long expireTimeVar = expireTime;
if (expireTimeVar != -1 && currentTime > expireTimeVar) {
IoUtils.safeClose(connection);
throw new ClosedChannelException();
}
expireTime = currentTime + timeout;
XnioExecutor.Key key = handle;
if (key == null) {
handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS);
}
}
示例9: transferTo
import org.xnio.IoUtils; //导入依赖的package包/类
public long transferTo(final long position, final long count, final FileChannel target) throws IOException {
long val = state;
checkMaxSize(val);
if (anyAreSet(val, FLAG_CLOSED | FLAG_FINISHED) || allAreClear(val, MASK_COUNT)) {
if (allAreClear(val, FLAG_FINISHED)) {
invokeFinishListener();
}
return -1L;
}
long res = 0L;
try {
return res = next.transferTo(position, min(count, val & MASK_COUNT), target);
} catch (IOException | RuntimeException e) {
IoUtils.safeClose(exchange.getConnection());
throw e;
} finally {
exitRead(res);
}
}
示例10: sendBlockingInternal
import org.xnio.IoUtils; //导入依赖的package包/类
private static void sendBlockingInternal(final ByteBuffer[] data, WebSocketFrameType type, final WebSocketChannel wsChannel) throws IOException {
long totalData = Buffers.remaining(data);
StreamSinkFrameChannel channel = wsChannel.send(type, totalData);
for (ByteBuffer buf : data) {
while (buf.hasRemaining()) {
int res = channel.write(buf);
if (res == 0) {
channel.awaitWritable();
}
}
}
channel.shutdownWrites();
while (!channel.flush()) {
channel.awaitWritable();
}
if (type == WebSocketFrameType.CLOSE && wsChannel.isCloseFrameReceived()) {
IoUtils.safeClose(wsChannel);
}
}
示例11: handshakeInternal
import org.xnio.IoUtils; //导入依赖的package包/类
@Override
protected void handshakeInternal(final WebSocketHttpExchange exchange) {
String origin = exchange.getRequestHeader(Headers.ORIGIN_STRING);
if (origin != null) {
exchange.setResponseHeader(Headers.ORIGIN_STRING, origin);
}
selectSubprotocol(exchange);
selectExtensions(exchange);
exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_LOCATION_STRING, getWebSocketLocation(exchange));
final String key = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_KEY_STRING);
try {
final String solution = solve(key);
exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_ACCEPT_STRING, solution);
performUpgrade(exchange);
} catch (NoSuchAlgorithmException e) {
IoUtils.safeClose(exchange);
exchange.endExchange();
return;
}
}
示例12: handshakeInternal
import org.xnio.IoUtils; //导入依赖的package包/类
protected void handshakeInternal(final WebSocketHttpExchange exchange) {
String origin = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_ORIGIN_STRING);
if (origin != null) {
exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_ORIGIN_STRING, origin);
}
selectSubprotocol(exchange);
selectExtensions(exchange);
exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_LOCATION_STRING, getWebSocketLocation(exchange));
final String key = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_KEY_STRING);
try {
final String solution = solve(key);
exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_ACCEPT_STRING, solution);
performUpgrade(exchange);
} catch (NoSuchAlgorithmException e) {
IoUtils.safeClose(exchange);
exchange.endExchange();
return;
}
}
示例13: sendClose
import org.xnio.IoUtils; //导入依赖的package包/类
/**
* Send a Close frame without a payload
*/
public void sendClose() throws IOException {
closeReason = "";
closeCode = CloseMessage.NORMAL_CLOSURE;
StreamSinkFrameChannel closeChannel = send(WebSocketFrameType.CLOSE, 0);
closeChannel.shutdownWrites();
if (!closeChannel.flush()) {
closeChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener(
null, new ChannelExceptionHandler<StreamSinkChannel>() {
@Override
public void handleException(final StreamSinkChannel channel, final IOException exception) {
IoUtils.safeClose(WebSocketChannel.this);
}
}
));
closeChannel.resumeWrites();
}
}
示例14: writeDone
import org.xnio.IoUtils; //导入依赖的package包/类
protected void writeDone(final StreamSinkChannel channel) {
try {
channel.shutdownWrites();
if (!channel.flush()) {
channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
@Override
public void handleEvent(StreamSinkChannel o) {
IoUtils.safeClose(channel);
}
}, ChannelListeners.closingChannelExceptionHandler()));
channel.resumeWrites();
}
} catch (IOException e) {
handleError(channel, e);
}
}
示例15: setup
import org.xnio.IoUtils; //导入依赖的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();
}
}