Java Code Examples for org.xnio.channels.StreamSourceChannel#read()
The following examples show how to use
org.xnio.channels.StreamSourceChannel#read() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestBodyParser.java From jweb-cms with GNU Affero General Public License v3.0 | 6 votes |
public void read(StreamSourceChannel channel) { WritableByteChannel out = Channels.newChannel(body); 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(); out.write(buffer); } if (bytesRead == -1) { complete = true; exchange.putAttachment(REQUEST_BODY, new RequestBody(body.toByteArray(), null)); } } catch (IOException e) { IoUtils.safeClose(channel); complete = true; exchange.putAttachment(REQUEST_BODY, new RequestBody(null, e)); } }
Example 2
Source File: StringReadChannelListener.java From lams with GNU General Public License v2.0 | 6 votes |
public void setup(final StreamSourceChannel channel) { PooledByteBuffer resource = bufferPool.allocate(); ByteBuffer buffer = resource.getBuffer(); 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.close(); } }
Example 3
Source File: StringReadChannelListener.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public void handleEvent(final StreamSourceChannel channel) { PooledByteBuffer resource = bufferPool.allocate(); ByteBuffer buffer = resource.getBuffer(); 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.close(); } }
Example 4
Source File: ByteBufferReadChannelListener.java From light-4j with Apache License 2.0 | 5 votes |
public void setup(StreamSourceChannel channel) { PooledByteBuffer resource = this.bufferPool.allocate(); ByteBuffer buffer = resource.getBuffer(); try { int r; do { r = channel.read(buffer); if (r == 0) { channel.getReadSetter().set(this); channel.resumeReads(); } else if (r == -1) { this.bufferDone(this.result); IoUtils.safeClose(channel); } else { buffer.flip(); ByteBuffer[] buffs = new ByteBuffer[]{buffer}; for(int i = 0; i < buffs.length; ++i) { ByteBuffer buf = buffs[i]; while(buf.hasRemaining()) { result.add(buf.get()); } } } } while(r > 0); } catch (IOException var8) { this.error(var8); } finally { resource.close(); } }
Example 5
Source File: ByteBufferReadChannelListener.java From light-4j with Apache License 2.0 | 5 votes |
public void handleEvent(StreamSourceChannel channel) { PooledByteBuffer resource = this.bufferPool.allocate(); ByteBuffer buffer = resource.getBuffer(); try { int r; do { r = channel.read(buffer); if (r == 0) { return; } if (r == -1) { this.bufferDone(this.result); IoUtils.safeClose(channel); } else { buffer.flip(); ByteBuffer[] buffs = new ByteBuffer[]{buffer};; for(int i = 0; i < buffs.length; ++i) { ByteBuffer buf = buffs[i]; while(buf.hasRemaining()) { result.add(buf.get()); } } } } while(r > 0); } catch (IOException var8) { this.error(var8); } finally { resource.close(); } }
Example 6
Source File: RequestBodyReader.java From core-ng-project with Apache License 2.0 | 5 votes |
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 new Error(format("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)); } }
Example 7
Source File: ChunkyByteBuffer.java From divolte-collector with Apache License 2.0 | 5 votes |
private void waitForEndOfStream(final StreamSourceChannel channel) { // So we have to supply a buffer to detect EOF. // This is a corner case, and some alternatives are inappropriate: // - A singleton 1-byte buffer will lead to contention if multiple clients are in this state. // - A zero-byte buffer can't be used because the semantics for reading from it aren't properly // defined. final ByteBuffer tinyBuffer = ByteBuffer.allocate(1); try { final int numRead = channel.read(tinyBuffer); switch (numRead) { case -1: // End-of-stream. Phew. endOfFile(channel); break; case 0: // Not yet sure; keep waiting. channel.resumeReads(); break; default: // Overflow. Doh. channel.getReadSetter().set(null); completionHandler.overflow(); } } catch (final IOException e) { onFailure(channel, e); } }
Example 8
Source File: UndertowXhrTransport.java From spring-analysis-note with MIT License | 4 votes |
@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); } PooledByteBuffer pooled = bufferPool.allocate(); try { int r; do { ByteBuffer buffer = pooled.getBuffer(); 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 { pooled.close(); } }
Example 9
Source File: UndertowXhrTransport.java From java-technology-stack with MIT License | 4 votes |
@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); } PooledByteBuffer pooled = bufferPool.allocate(); try { int r; do { ByteBuffer buffer = pooled.getBuffer(); 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 { pooled.close(); } }
Example 10
Source File: FormEncodedDataDefinition.java From lams with GNU General Public License v2.0 | 4 votes |
private void doParse(final StreamSourceChannel channel) throws IOException { int c = 0; final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate(); try { final ByteBuffer buffer = pooled.getBuffer(); do { buffer.clear(); c = channel.read(buffer); if (c > 0) { buffer.flip(); while (buffer.hasRemaining()) { byte n = buffer.get(); switch (state) { case 0: { if (n == '=') { name = builder.toString(); builder.setLength(0); state = 2; } else if (n == '&') { data.add(builder.toString(), ""); builder.setLength(0); state = 0; } else if (n == '%' || n == '+') { state = 1; builder.append((char) n); } else { builder.append((char) n); } break; } case 1: { if (n == '=') { name = URLUtils.decode(builder.toString(), charset, true, new StringBuilder()); builder.setLength(0); state = 2; } else if (n == '&') { data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), ""); builder.setLength(0); state = 0; } else { builder.append((char) n); } break; } case 2: { if (n == '&') { data.add(name, builder.toString()); builder.setLength(0); state = 0; } else if (n == '%' || n == '+') { state = 3; builder.append((char) n); } else { builder.append((char) n); } break; } case 3: { if (n == '&') { data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder())); builder.setLength(0); state = 0; } else { builder.append((char) n); } break; } } } } } while (c > 0); if (c == -1) { if (state == 2) { data.add(name, builder.toString()); } else if (state == 3) { data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder())); } else if(builder.length() > 0) { if(state == 1) { data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), ""); } else { data.add(builder.toString(), ""); } } state = 4; exchange.putAttachment(FORM_DATA, data); } } finally { pooled.close(); } }
Example 11
Source File: UndertowXhrTransport.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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); } }
Example 12
Source File: ChunkyByteBuffer.java From divolte-collector with Apache License 2.0 | 4 votes |
private void fillBuffers(final StreamSourceChannel channel, @Nullable final Consumer<StreamSourceChannel> willBlockHandler) { // Note: a few early-returns are sprinkled around. for (;;) { final ByteBuffer currentChunk = chunks[currentChunkIndex]; final int readResult; try { readResult = channel.read(currentChunk); } catch (final IOException e) { onFailure(channel, e); return; } switch (readResult) { case -1: // End-of-file. endOfFile(channel); return; case 0: // Nothing pending right now. if (null != willBlockHandler) { willBlockHandler.accept(channel); } return; default: // Some data was read: // - Might have filled current chunk; move on to next, if possible. // - Might be no more data; will check on next loop. if (!currentChunk.hasRemaining()) { if (currentChunkIndex + 1 < chunks.length) { // We allocate chunks on demand, as we advance. chunks[++currentChunkIndex] = ByteBuffer.allocate(CHUNK_SIZE); } else { // Buffers are full; detect if we're at EOF, waiting if necessary. channel.getReadSetter().set(this::waitForEndOfStream); waitForEndOfStream(channel); return; } } } } }