org.apache.mina.core.file.FileRegion Java Examples

The following examples show how to use org.apache.mina.core.file.FileRegion. 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: NioProcessor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected int transferFile(NioSession session, FileRegion region, int length) throws Exception {
    try {
        return (int) region.getFileChannel().transferTo(region.getPosition(), length, session.getChannel());
    } catch (IOException e) {
        // Check to see if the IOException is being thrown due to
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103988
        String message = e.getMessage();
        if ((message != null) && message.contains("temporarily unavailable")) {
            return 0;
        }

        throw e;
    }
}
 
Example #3
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 #4
Source File: FileRegionWriteFilter.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected Class<FileRegion> getMessageClass() {
    return FileRegion.class;
}
 
Example #5
Source File: ProtocolCodecFilter.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    Object message = writeRequest.getMessage();

    // Bypass the encoding if the message is contained in a IoBuffer,
    // as it has already been encoded before
    if ((message instanceof IoBuffer) || (message instanceof FileRegion)) {
        nextFilter.filterWrite(session, writeRequest);
        return;
    }

    // Get the encoder in the session
    ProtocolEncoder encoder = factory.getEncoder(session);

    ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest);

    if (encoder == null) {
        throw new ProtocolEncoderException("The encoder is null for the session " + session);
    }

    if (encoderOut == null) {
        throw new ProtocolEncoderException("The encoderOut is null for the session " + session);
    }

    try {
        // Now we can try to encode the response
        encoder.encode(session, message, encoderOut);

        // Send it directly
        Queue<Object> bufferQueue = ((AbstractProtocolEncoderOutput) encoderOut).getMessageQueue();

        // Write all the encoded messages now
        while (!bufferQueue.isEmpty()) {
            Object encodedMessage = bufferQueue.poll();

            if (encodedMessage == null) {
                break;
            }

            // Flush only when the buffer has remaining.
            if (!(encodedMessage instanceof IoBuffer) || ((IoBuffer) encodedMessage).hasRemaining()) {
                SocketAddress destination = writeRequest.getDestination();
                WriteRequest encodedWriteRequest = new EncodedWriteRequest(encodedMessage, null, destination);

                nextFilter.filterWrite(session, encodedWriteRequest);
            }
        }

        // Call the next filter
        nextFilter.filterWrite(session, new MessageWriteRequest(writeRequest));
    } catch (Throwable t) {
        ProtocolEncoderException pee;

        // Generate the correct exception
        if (t instanceof ProtocolEncoderException) {
            pee = (ProtocolEncoderException) t;
        } else {
            pee = new ProtocolEncoderException(t);
        }

        throw pee;
    }
}
 
Example #6
Source File: AbstractPollingIoProcessor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private boolean flushNow(S session, long currentTime) {
    if (!session.isConnected()) {
        scheduleRemove(session);
        return false;
    }

    final boolean hasFragmentation = session.getTransportMetadata().hasFragmentation();

    final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();

    // Set limitation for the number of written bytes for read-write
    // fairness. I used maxReadBufferSize * 3 / 2, which yields best
    // performance in my experience while not breaking fairness much.
    final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize()
            + (session.getConfig().getMaxReadBufferSize() >>> 1);
    int writtenBytes = 0;
    WriteRequest req = null;

    try {
        // Clear OP_WRITE
        setInterestedInWrite(session, false);

        do {
            // Check for pending writes.
            req = session.getCurrentWriteRequest();

            if (req == null) {
                req = writeRequestQueue.poll(session);

                if (req == null) {
                    break;
                }

                session.setCurrentWriteRequest(req);
            }

            int localWrittenBytes = 0;
            Object message = req.getMessage();

            if (message instanceof IoBuffer) {
                localWrittenBytes = writeBuffer(session, req, hasFragmentation, maxWrittenBytes - writtenBytes,
                        currentTime);

                if ((localWrittenBytes > 0) && ((IoBuffer) message).hasRemaining()) {
                    // the buffer isn't empty, we re-interest it in writing
                    writtenBytes += localWrittenBytes;
                    setInterestedInWrite(session, true);
                    return false;
                }
            } else if (message instanceof FileRegion) {
                localWrittenBytes = writeFile(session, req, hasFragmentation, maxWrittenBytes - writtenBytes,
                        currentTime);

                // Fix for Java bug on Linux
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103988
                // If there's still data to be written in the FileRegion,
                // return 0 indicating that we need
                // to pause until writing may resume.
                if ((localWrittenBytes > 0) && (((FileRegion) message).getRemainingBytes() > 0)) {
                    writtenBytes += localWrittenBytes;
                    setInterestedInWrite(session, true);
                    return false;
                }
            } else {
                throw new IllegalStateException("Don't know how to handle message of type '"
                        + message.getClass().getName() + "'.  Are you missing a protocol encoder?");
            }

            if (localWrittenBytes == 0) {
                // Kernel buffer is full.
                setInterestedInWrite(session, true);
                return false;
            }

            writtenBytes += localWrittenBytes;

            if (writtenBytes >= maxWrittenBytes) {
                // Wrote too much
                scheduleFlush(session);
                return false;
            }
        } while (writtenBytes < maxWrittenBytes);
    } catch (Exception e) {
        if (req != null) {
            req.getFuture().setException(e);
        }

        IoFilterChain filterChain = session.getFilterChain();
        filterChain.fireExceptionCaught(e);
        return false;
    }

    return true;
}
 
Example #7
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);
	}
}
 
Example #8
Source File: AbstractPollingIoProcessor.java    From neoscada with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Write a part of a file to a {@link IoSession}, if the underlying API
 * isn't supporting system calls like sendfile(), you can throw a
 * {@link UnsupportedOperationException} so the file will be send using
 * usual {@link #write(AbstractIoSession, IoBuffer, int)} call.
 *
 * @param session
 *            the session to write
 * @param region
 *            the file region to write
 * @param length
 *            the length of the portion to send
 * @return the number of written bytes
 * @throws Exception
 *             any exception thrown by the underlying system calls
 */
protected abstract int transferFile(S session, FileRegion region, int length) throws Exception;