From 6f616bb3cfcdca5b591ce18a53fbd90e948ffc64 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 17 Aug 2019 09:43:01 +0200 Subject: [PATCH] =?UTF-8?q?Avoid=20creating=20FileInputStream=20and=20File?= =?UTF-8?q?OutputStream=20for=20obtaining=20Fil=E2=80=A6=20(#8110)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation: If all we need is the FileChannel we should better use RandomAccessFile as FileInputStream and FileOutputStream use a finalizer. Modifications: Replace FileInputStream and FileOutputStream with RandomAccessFile when possible. Result: Fixes https://github.com/netty/netty/issues/8078. --- .../ReadOnlyDirectByteBufferBufTest.java | 7 ++- .../http/multipart/AbstractDiskHttpData.java | 45 ++++++++++--------- .../multipart/AbstractMemoryHttpData.java | 15 +++---- .../netty/handler/stream/ChunkedNioFile.java | 6 +-- .../socket/SocketFileRegionTest.java | 8 ++-- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java index b877b25b05..73fb7c8262 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java @@ -20,9 +20,8 @@ import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.ReadOnlyBufferException; import java.nio.channels.FileChannel; @@ -307,12 +306,12 @@ public class ReadOnlyDirectByteBufferBufTest { ByteBuf b2 = null; try { - output = new FileOutputStream(file).getChannel(); + output = new RandomAccessFile(file, "rw").getChannel(); byte[] bytes = new byte[1024]; ThreadLocalRandom.current().nextBytes(bytes); output.write(ByteBuffer.wrap(bytes)); - input = new FileInputStream(file).getChannel(); + input = new RandomAccessFile(file, "r").getChannel(); ByteBuffer m = input.map(FileChannel.MapMode.READ_ONLY, 0, input.size()); b1 = buffer(m); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java index c0b0c87344..0c11ac42f3 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java @@ -22,10 +22,9 @@ import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; @@ -125,8 +124,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { } return; } - try (FileOutputStream outputStream = new FileOutputStream(file)) { - FileChannel localfileChannel = outputStream.getChannel(); + try (RandomAccessFile accessFile = new RandomAccessFile(file, "rw")) { + accessFile.setLength(0); + FileChannel localfileChannel = accessFile.getChannel(); ByteBuffer byteBuffer = buffer.nioBuffer(); int written = 0; while (written < size) { @@ -160,8 +160,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { file = tempFile(); } if (fileChannel == null) { - FileOutputStream outputStream = new FileOutputStream(file); - fileChannel = outputStream.getChannel(); + RandomAccessFile accessFile = new RandomAccessFile(file, "rw"); + fileChannel = accessFile.getChannel(); } while (written < localsize) { written += fileChannel.write(byteBuffer); @@ -179,8 +179,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { file = tempFile(); } if (fileChannel == null) { - FileOutputStream outputStream = new FileOutputStream(file); - fileChannel = outputStream.getChannel(); + RandomAccessFile accessFile = new RandomAccessFile(file, "rw"); + fileChannel = accessFile.getChannel(); } fileChannel.force(false); fileChannel.close(); @@ -211,8 +211,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { } file = tempFile(); int written = 0; - try (FileOutputStream outputStream = new FileOutputStream(file)) { - FileChannel localfileChannel = outputStream.getChannel(); + try (RandomAccessFile accessFile = new RandomAccessFile(file, "rw")) { + accessFile.setLength(0); + FileChannel localfileChannel = accessFile.getChannel(); byte[] bytes = new byte[4096 * 4]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int read = inputStream.read(bytes); @@ -280,8 +281,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { return EMPTY_BUFFER; } if (fileChannel == null) { - FileInputStream inputStream = new FileInputStream(file); - fileChannel = inputStream.getChannel(); + RandomAccessFile accessFile = new RandomAccessFile(file, "r"); + fileChannel = accessFile.getChannel(); } int read = 0; ByteBuffer byteBuffer = ByteBuffer.allocate(length); @@ -337,15 +338,15 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { if (!file.renameTo(dest)) { // must copy IOException exception = null; - FileInputStream inputStream = null; - FileOutputStream outputStream = null; + RandomAccessFile inputAccessFile = null; + RandomAccessFile outputAccessFile = null; long chunkSize = 8196; long position = 0; try { - inputStream = new FileInputStream(file); - outputStream = new FileOutputStream(dest); - FileChannel in = inputStream.getChannel(); - FileChannel out = outputStream.getChannel(); + inputAccessFile = new RandomAccessFile(file, "r"); + outputAccessFile = new RandomAccessFile(dest, "rw"); + FileChannel in = inputAccessFile.getChannel(); + FileChannel out = outputAccessFile.getChannel(); while (position < size) { if (chunkSize < size - position) { chunkSize = size - position; @@ -355,9 +356,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { } catch (IOException e) { exception = e; } finally { - if (inputStream != null) { + if (inputAccessFile != null) { try { - inputStream.close(); + inputAccessFile.close(); } catch (IOException e) { if (exception == null) { // Choose to report the first exception exception = e; @@ -366,9 +367,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { } } } - if (outputStream != null) { + if (outputAccessFile != null) { try { - outputStream.close(); + outputAccessFile.close(); } catch (IOException e) { if (exception == null) { // Choose to report the first exception exception = e; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java index 5481e4ec7e..29a70ea900 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java @@ -20,10 +20,9 @@ import io.netty.buffer.CompositeByteBuf; import io.netty.handler.codec.http.HttpConstants; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; @@ -124,8 +123,8 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { throw new IllegalArgumentException("File too big to be loaded in memory"); } checkSize(newsize); - FileInputStream inputStream = new FileInputStream(file); - FileChannel fileChannel = inputStream.getChannel(); + RandomAccessFile accessFile = new RandomAccessFile(file, "r"); + FileChannel fileChannel = accessFile.getChannel(); byte[] array = new byte[(int) newsize]; ByteBuffer byteBuffer = ByteBuffer.wrap(array); int read = 0; @@ -133,7 +132,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { read += fileChannel.read(byteBuffer); } fileChannel.close(); - inputStream.close(); + accessFile.close(); byteBuffer.flip(); if (byteBuf != null) { byteBuf.release(); @@ -223,8 +222,8 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { return true; } int length = byteBuf.readableBytes(); - FileOutputStream outputStream = new FileOutputStream(dest); - FileChannel fileChannel = outputStream.getChannel(); + RandomAccessFile accessFile = new RandomAccessFile(dest, "rw"); + FileChannel fileChannel = accessFile.getChannel(); int written = 0; if (byteBuf.nioBufferCount() == 1) { ByteBuffer byteBuffer = byteBuf.nioBuffer(); @@ -240,7 +239,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { fileChannel.force(false); fileChannel.close(); - outputStream.close(); + accessFile.close(); return written == length; } diff --git a/handler/src/main/java/io/netty/handler/stream/ChunkedNioFile.java b/handler/src/main/java/io/netty/handler/stream/ChunkedNioFile.java index 3b10f6d843..be0696955d 100644 --- a/handler/src/main/java/io/netty/handler/stream/ChunkedNioFile.java +++ b/handler/src/main/java/io/netty/handler/stream/ChunkedNioFile.java @@ -23,8 +23,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.FileRegion; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.channels.FileChannel; /** @@ -47,7 +47,7 @@ public class ChunkedNioFile implements ChunkedInput { * Creates a new instance that fetches data from the specified file. */ public ChunkedNioFile(File in) throws IOException { - this(new FileInputStream(in).getChannel()); + this(new RandomAccessFile(in, "r").getChannel()); } /** @@ -57,7 +57,7 @@ public class ChunkedNioFile implements ChunkedInput { * {@link #readChunk(ChannelHandlerContext)} call */ public ChunkedNioFile(File in, int chunkSize) throws IOException { - this(new FileInputStream(in).getChannel(), chunkSize); + this(new RandomAccessFile(in, "r").getChannel(), chunkSize); } /** diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java index 72926b1e76..cda796c219 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java @@ -30,9 +30,9 @@ import org.hamcrest.CoreMatchers; import org.junit.Test; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.channels.WritableByteChannel; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -120,7 +120,7 @@ public class SocketFileRegionTest extends AbstractSocketTest { // Request file region which is bigger then the underlying file. FileRegion region = new DefaultFileRegion( - new FileInputStream(file).getChannel(), 0, data.length + 1024); + new RandomAccessFile(file, "r").getChannel(), 0, data.length + 1024); assertThat(cc.writeAndFlush(region).await().cause(), CoreMatchers.instanceOf(IOException.class)); cc.close().sync(); @@ -182,8 +182,8 @@ public class SocketFileRegionTest extends AbstractSocketTest { Channel cc = cb.connect(sc.localAddress()).sync().channel(); FileRegion region = new DefaultFileRegion( - new FileInputStream(file).getChannel(), startOffset, data.length - bufferSize); - FileRegion emptyRegion = new DefaultFileRegion(new FileInputStream(file).getChannel(), 0, 0); + new RandomAccessFile(file, "r").getChannel(), startOffset, data.length - bufferSize); + FileRegion emptyRegion = new DefaultFileRegion(new RandomAccessFile(file, "r").getChannel(), 0, 0); if (!defaultFileRegion) { region = new FileRegionWrapper(region);