2012-10-24 18:27:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.channel;
|
|
|
|
|
2013-06-13 06:14:21 +02:00
|
|
|
import io.netty.util.AbstractReferenceCounted;
|
2014-10-28 19:42:07 +01:00
|
|
|
import io.netty.util.IllegalReferenceCountException;
|
2013-02-26 23:54:25 +01:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2013-01-10 07:23:58 +01:00
|
|
|
|
2014-10-28 19:42:07 +01:00
|
|
|
import java.io.File;
|
2012-10-24 18:27:26 +02:00
|
|
|
import java.io.IOException;
|
2014-10-28 19:42:07 +01:00
|
|
|
import java.io.RandomAccessFile;
|
2012-10-24 18:27:26 +02:00
|
|
|
import java.nio.channels.FileChannel;
|
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
|
2012-12-19 08:24:32 +01:00
|
|
|
/**
|
2014-10-28 19:42:07 +01:00
|
|
|
* Default {@link FileRegion} implementation which transfer data from a {@link FileChannel} or {@link File}.
|
2013-11-02 15:59:33 +01:00
|
|
|
*
|
|
|
|
* Be aware that the {@link FileChannel} will be automatically closed once {@link #refCnt()} returns
|
|
|
|
* {@code 0}.
|
2012-12-19 08:24:32 +01:00
|
|
|
*/
|
2013-02-10 14:25:53 +01:00
|
|
|
public class DefaultFileRegion extends AbstractReferenceCounted implements FileRegion {
|
2012-10-24 18:27:26 +02:00
|
|
|
|
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultFileRegion.class);
|
2014-10-28 19:42:07 +01:00
|
|
|
private final File f;
|
2012-10-24 18:27:26 +02:00
|
|
|
private final long position;
|
|
|
|
private final long count;
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
private long transfered;
|
2014-10-28 19:42:07 +01:00
|
|
|
private FileChannel file;
|
2012-10-24 18:27:26 +02:00
|
|
|
|
2012-12-19 08:24:32 +01:00
|
|
|
/**
|
|
|
|
* Create a new instance
|
|
|
|
*
|
|
|
|
* @param file the {@link FileChannel} which should be transfered
|
|
|
|
* @param position the position from which the transfer should start
|
|
|
|
* @param count the number of bytes to transfer
|
|
|
|
*/
|
2012-10-24 18:27:26 +02:00
|
|
|
public DefaultFileRegion(FileChannel file, long position, long count) {
|
2012-12-19 08:24:32 +01:00
|
|
|
if (file == null) {
|
|
|
|
throw new NullPointerException("file");
|
|
|
|
}
|
|
|
|
if (position < 0) {
|
|
|
|
throw new IllegalArgumentException("position must be >= 0 but was " + position);
|
|
|
|
}
|
2013-06-19 19:19:57 +02:00
|
|
|
if (count < 0) {
|
2012-12-19 08:24:32 +01:00
|
|
|
throw new IllegalArgumentException("count must be >= 0 but was " + count);
|
|
|
|
}
|
2012-10-24 18:27:26 +02:00
|
|
|
this.file = file;
|
|
|
|
this.position = position;
|
|
|
|
this.count = count;
|
2014-10-28 19:42:07 +01:00
|
|
|
f = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance using the given {@link File}. The {@link File} will be opened lazily or
|
|
|
|
* explicitly via {@link #open()}.
|
|
|
|
*
|
|
|
|
* @param f the {@link File} which should be transfered
|
|
|
|
* @param position the position from which the transfer should start
|
|
|
|
* @param count the number of bytes to transfer
|
|
|
|
*/
|
|
|
|
public DefaultFileRegion(File f, long position, long count) {
|
|
|
|
if (f == null) {
|
|
|
|
throw new NullPointerException("f");
|
|
|
|
}
|
|
|
|
if (position < 0) {
|
|
|
|
throw new IllegalArgumentException("position must be >= 0 but was " + position);
|
|
|
|
}
|
|
|
|
if (count < 0) {
|
|
|
|
throw new IllegalArgumentException("count must be >= 0 but was " + count);
|
|
|
|
}
|
|
|
|
this.position = position;
|
|
|
|
this.count = count;
|
|
|
|
this.f = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if the {@link FileRegion} has a open file-descriptor
|
|
|
|
*/
|
|
|
|
public boolean isOpen() {
|
|
|
|
return file != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly open the underlying file-descriptor if not done yet.
|
|
|
|
*/
|
|
|
|
public void open() throws IOException {
|
|
|
|
if (!isOpen() && refCnt() > 0) {
|
|
|
|
// Only open if this DefaultFileRegion was not released yet.
|
|
|
|
file = new RandomAccessFile(f, "r").getChannel();
|
|
|
|
}
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long position() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long count() {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-03-30 11:27:12 +02:00
|
|
|
@Deprecated
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
@Override
|
|
|
|
public long transfered() {
|
|
|
|
return transfered;
|
|
|
|
}
|
|
|
|
|
2016-03-30 11:27:12 +02:00
|
|
|
@Override
|
|
|
|
public long transferred() {
|
|
|
|
return transfered;
|
|
|
|
}
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
@Override
|
|
|
|
public long transferTo(WritableByteChannel target, long position) throws IOException {
|
|
|
|
long count = this.count - position;
|
|
|
|
if (count < 0 || position < 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"position out of range: " + position +
|
2013-01-10 07:23:58 +01:00
|
|
|
" (expected: 0 - " + (this.count - 1) + ')');
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
|
|
|
if (count == 0) {
|
|
|
|
return 0L;
|
|
|
|
}
|
2014-10-28 19:42:07 +01:00
|
|
|
if (refCnt() == 0) {
|
|
|
|
throw new IllegalReferenceCountException(0);
|
|
|
|
}
|
|
|
|
// Call open to make sure fc is initialized. This is a no-oop if we called it before.
|
|
|
|
open();
|
2012-10-24 18:27:26 +02:00
|
|
|
|
2013-06-10 11:19:58 +02:00
|
|
|
long written = file.transferTo(this.position + position, count, target);
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
if (written > 0) {
|
|
|
|
transfered += written;
|
|
|
|
}
|
|
|
|
return written;
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-10 14:25:53 +01:00
|
|
|
protected void deallocate() {
|
2014-10-28 19:42:07 +01:00
|
|
|
FileChannel file = this.file;
|
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.file = null;
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
try {
|
|
|
|
file.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn("Failed to close a file.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 12:00:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileRegion retain() {
|
|
|
|
super.retain();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileRegion retain(int increment) {
|
|
|
|
super.retain(increment);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileRegion touch() {
|
|
|
|
return this;
|
|
|
|
}
|
2014-01-29 03:44:59 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileRegion touch(Object hint) {
|
|
|
|
return this;
|
|
|
|
}
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|