Java Code Examples for org.apache.mina.core.file.FileRegion#getRemainingBytes()

The following examples show how to use org.apache.mina.core.file.FileRegion#getRemainingBytes() . 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: FileRegionWriteFilter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IoBuffer getNextBuffer(FileRegion fileRegion) throws IOException {
    // If there are no more bytes to read, return null
    if (fileRegion.getRemainingBytes() <= 0) {
        return null;
    }

    // Allocate the buffer for reading from the file
    final int bufferSize = (int) Math.min(getWriteBufferSize(), fileRegion.getRemainingBytes());
    IoBuffer buffer = IoBuffer.allocate(bufferSize);

    // Read from the file
    int bytesRead = fileRegion.getFileChannel().read(buffer.buf(), fileRegion.getPosition());
    fileRegion.update(bytesRead);

    // return the buffer
    buffer.flip();
    return buffer;
}
 
Example 2
Source File: AbstractPollingIoProcessor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private int writeFile(S session, WriteRequest req, boolean hasFragmentation, int maxLength, long currentTime)
        throws Exception {
    int localWrittenBytes;
    FileRegion region = (FileRegion) req.getMessage();

    if (region.getRemainingBytes() > 0) {
        int length;

        if (hasFragmentation) {
            length = (int) Math.min(region.getRemainingBytes(), maxLength);
        } else {
            length = (int) Math.min(Integer.MAX_VALUE, region.getRemainingBytes());
        }

        localWrittenBytes = transferFile(session, region, length);
        region.update(localWrittenBytes);
    } else {
        localWrittenBytes = 0;
    }

    session.increaseWrittenBytes(localWrittenBytes, currentTime);

    if ((region.getRemainingBytes() <= 0) || (!hasFragmentation && (localWrittenBytes != 0))) {
        fireMessageSent(session, req);
    }

    return localWrittenBytes;
}
 
Example 3
Source File: NioProcessor.java    From jane with GNU Lesser General Public License v3.0 4 votes vote down vote up
void flushNow(NioSession session) {
	if (session.isClosing())
		return;
	try {
		WriteRequestQueue writeQueue = session.getWriteRequestQueue();
		for (WriteRequest req; (req = writeQueue.peek()) != null; writeQueue.poll()) {
			Object message = req.writeRequestMessage();
			if (message instanceof IoBuffer) {
				IoBuffer buf = (IoBuffer)message;
				if (buf.hasRemaining()) {
					session.getChannel().write(buf.buf());
					if (buf.hasRemaining()) {
						session.setInterestedInWrite(true);
						return;
					}
				}
				req.writeRequestFuture().setWritten();
				buf.free();
			} else if (message instanceof FileRegion) {
				FileRegion region = (FileRegion)message;
				long len = region.getRemainingBytes();
				if (len > 0) {
					region.update(region.getFileChannel().transferTo(region.getPosition(), len, session.getChannel()));
  						if (region.getRemainingBytes() > 0) {
  							session.setInterestedInWrite(true);
  							return;
  						}
				}
				req.writeRequestFuture().setWritten();
			} else if (req == NioSession.CLOSE_REQUEST) {
				session.closeNow();
				break;
			} else if (req == NioSession.SHUTDOWN_REQUEST) {
				session.getChannel().shutdownOutput();
				break;
			} else
				throw new IllegalStateException("unknown message type for writting: " + message.getClass().getName() + ": " + message);
		}
		session.setInterestedInWrite(false);
	} catch (Exception e) {
		session.closeNow();
		session.removeNow(e);
	}
}