diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index 401b80fabd..c20accfbb1 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -18,6 +18,7 @@ package io.netty.buffer; import io.netty.util.ByteProcessor; import io.netty.util.CharsetUtil; import io.netty.util.IllegalReferenceCountException; +import io.netty.util.internal.PlatformDependent; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -4534,7 +4535,7 @@ public abstract class AbstractByteBufTest { @Test public void testReadBytesAndWriteBytesWithFileChannel() throws IOException { - File file = File.createTempFile("file-channel", ".tmp"); + File file = PlatformDependent.createTempFile("file-channel", ".tmp", null); RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(file, "rw"); @@ -4573,7 +4574,7 @@ public abstract class AbstractByteBufTest { @Test public void testGetBytesAndSetBytesWithFileChannel() throws IOException { - File file = File.createTempFile("file-channel", ".tmp"); + File file = PlatformDependent.createTempFile("file-channel", ".tmp", null); RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(file, "rw"); diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java index 2e1e042222..26f6ed8080 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java @@ -15,6 +15,7 @@ */ package io.netty.buffer; +import io.netty.util.internal.PlatformDependent; import org.junit.Assert; import org.junit.Test; @@ -306,7 +307,7 @@ public class ReadOnlyDirectByteBufferBufTest { @Test public void testWrapMemoryMapped() throws Exception { - File file = File.createTempFile("netty-test", "tmp"); + File file = PlatformDependent.createTempFile("netty-test", "tmp", null); FileChannel output = null; FileChannel input = null; ByteBuf b1 = null; 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 f180a04048..f97317d9ab 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 @@ -18,6 +18,7 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.HttpConstants; import io.netty.util.internal.EmptyArrays; +import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -89,9 +90,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { File tmpFile; if (getBaseDirectory() == null) { // create a temporary file - tmpFile = File.createTempFile(getPrefix(), newpostfix); + tmpFile = PlatformDependent.createTempFile(getPrefix(), newpostfix, null); } else { - tmpFile = File.createTempFile(getPrefix(), newpostfix, new File( + tmpFile = PlatformDependent.createTempFile(getPrefix(), newpostfix, new File( getBaseDirectory())); } if (deleteOnExit()) { diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkedInputTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkedInputTest.java index f3cc36dee2..7a2d1452f2 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkedInputTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkedInputTest.java @@ -25,6 +25,7 @@ import io.netty.handler.stream.ChunkedNioFile; import io.netty.handler.stream.ChunkedNioStream; import io.netty.handler.stream.ChunkedStream; import io.netty.handler.stream.ChunkedWriteHandler; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -46,7 +47,7 @@ public class HttpChunkedInputTest { FileOutputStream out = null; try { - TMP = File.createTempFile("netty-chunk-", ".tmp"); + TMP = PlatformDependent.createTempFile("netty-chunk-", ".tmp", null); TMP.deleteOnExit(); out = new FileOutputStream(TMP); out.write(BYTES); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java index 29109a3f06..d212ae6ee2 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java @@ -17,6 +17,7 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import java.io.File; @@ -39,7 +40,7 @@ public class AbstractDiskHttpDataTest { public void testGetChunk() throws Exception { TestHttpData test = new TestHttpData("test", UTF_8, 0); try { - File tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); + File tmpFile = PlatformDependent.createTempFile(UUID.randomUUID().toString(), ".tmp", null); tmpFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] bytes = new byte[4096]; diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java index cfe6b390bb..1befe5b04f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -43,7 +44,7 @@ public class AbstractMemoryHttpDataTest { public void testSetContentFromFile() throws Exception { TestHttpData test = new TestHttpData("test", UTF_8, 0); try { - File tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); + File tmpFile = PlatformDependent.createTempFile(UUID.randomUUID().toString(), ".tmp", null); tmpFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] bytes = new byte[4096]; @@ -70,7 +71,7 @@ public class AbstractMemoryHttpDataTest { public void testRenameTo() throws Exception { TestHttpData test = new TestHttpData("test", UTF_8, 0); try { - File tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); + File tmpFile = PlatformDependent.createTempFile(UUID.randomUUID().toString(), ".tmp", null); tmpFile.deleteOnExit(); final int totalByteCount = 4096; byte[] bytes = new byte[totalByteCount]; diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java index f2c9ce90ea..0ed936786d 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java @@ -21,6 +21,7 @@ import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import java.io.File; @@ -272,8 +273,9 @@ public class DiskFileUploadTest { assertEquals(maxSize, originalFile.length()); assertEquals(maxSize, f1.length()); byte[] bytes = new byte[8]; + ThreadLocalRandom.current().nextBytes(bytes); - File tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); + File tmpFile = PlatformDependent.createTempFile(UUID.randomUUID().toString(), ".tmp", null); tmpFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tmpFile); try { diff --git a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java index c0ab3d3178..1236f0f339 100644 --- a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java +++ b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java @@ -178,7 +178,7 @@ public final class NativeLibraryLoader { String prefix = libname.substring(0, index); String suffix = libname.substring(index); - tmpFile = File.createTempFile(prefix, suffix, WORKDIR); + tmpFile = PlatformDependent.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index 4cbe06d02d..cb6f3aa283 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -38,6 +38,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.file.Files; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; @@ -1288,6 +1289,24 @@ public final class PlatformDependent { return LINUX_OS_CLASSIFIERS; } + @SuppressJava6Requirement(reason = "Guarded by version check") + public static File createTempFile(String prefix, String suffix, File directory) throws IOException { + if (javaVersion() >= 7) { + if (directory == null) { + return Files.createTempFile(prefix, suffix).toFile(); + } + return Files.createTempFile(directory.toPath(), prefix, suffix).toFile(); + } + if (directory == null) { + return File.createTempFile(prefix, suffix); + } + File file = File.createTempFile(prefix, suffix, directory); + // Try to adjust the perms, if this fails there is not much else we can do... + file.setReadable(false, false); + file.setReadable(true, true); + return file; + } + /** * Adds only those classifier strings to dest which are present in allowed. * diff --git a/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java index d5c8c825c8..e74943dc11 100644 --- a/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java +++ b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.handler.codec.base64.Base64; import io.netty.util.CharsetUtil; +import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -29,6 +30,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; @@ -320,7 +322,7 @@ public final class SelfSignedCertificate { wrappedBuf.release(); } - File keyFile = File.createTempFile("keyutil_" + fqdn + '_', ".key"); + File keyFile = PlatformDependent.createTempFile("keyutil_" + fqdn + '_', ".key", null); keyFile.deleteOnExit(); OutputStream keyOut = new FileOutputStream(keyFile); @@ -351,7 +353,7 @@ public final class SelfSignedCertificate { wrappedBuf.release(); } - File certFile = File.createTempFile("keyutil_" + fqdn + '_', ".crt"); + File certFile = PlatformDependent.createTempFile("keyutil_" + fqdn + '_', ".crt", null); certFile.deleteOnExit(); OutputStream certOut = new FileOutputStream(certFile); diff --git a/handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java b/handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java index 5fd3ec51a5..5dfdeeecc6 100644 --- a/handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java +++ b/handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java @@ -26,6 +26,7 @@ import io.netty.channel.ChannelPromise; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; +import io.netty.util.internal.PlatformDependent; import org.junit.Assert; import org.junit.Test; @@ -55,7 +56,7 @@ public class ChunkedWriteHandlerTest { FileOutputStream out = null; try { - TMP = File.createTempFile("netty-chunk-", ".tmp"); + TMP = PlatformDependent.createTempFile("netty-chunk-", ".tmp", null); TMP.deleteOnExit(); out = new FileOutputStream(TMP); out.write(BYTES); diff --git a/handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java b/handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java index 7a81406191..72d604c069 100644 --- a/handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java +++ b/handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java @@ -33,6 +33,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.util.CharsetUtil; +import io.netty.util.internal.PlatformDependent; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -62,7 +63,7 @@ public class FileRegionThrottleTest { BYTES[i] = (byte) r.nextInt(255); } - tmp = File.createTempFile("netty-traffic", ".tmp"); + tmp = PlatformDependent.createTempFile("netty-traffic", ".tmp", null); tmp.deleteOnExit(); try (FileOutputStream out = new FileOutputStream(tmp)) { out.write(BYTES); 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 b232b922fb..7d93b19a3d 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 @@ -26,6 +26,7 @@ import io.netty.channel.ChannelOption; import io.netty.channel.DefaultFileRegion; import io.netty.channel.FileRegion; import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.internal.PlatformDependent; import org.hamcrest.CoreMatchers; import org.junit.Test; @@ -101,7 +102,7 @@ public class SocketFileRegionTest extends AbstractSocketTest { } public void testFileRegionCountLargerThenFile(ServerBootstrap sb, Bootstrap cb) throws Throwable { - File file = File.createTempFile("netty-", ".tmp"); + File file = PlatformDependent.createTempFile("netty-", ".tmp", null); file.deleteOnExit(); final FileOutputStream out = new FileOutputStream(file); @@ -135,7 +136,7 @@ public class SocketFileRegionTest extends AbstractSocketTest { cb.option(ChannelOption.AUTO_READ, autoRead); final int bufferSize = 1024; - final File file = File.createTempFile("netty-", ".tmp"); + final File file = PlatformDependent.createTempFile("netty-", ".tmp", null); file.deleteOnExit(); final FileOutputStream out = new FileOutputStream(file); diff --git a/transport-native-unix-common-tests/src/main/java/io/netty/channel/unix/tests/UnixTestUtils.java b/transport-native-unix-common-tests/src/main/java/io/netty/channel/unix/tests/UnixTestUtils.java index 346be7b046..7ee43e1e37 100644 --- a/transport-native-unix-common-tests/src/main/java/io/netty/channel/unix/tests/UnixTestUtils.java +++ b/transport-native-unix-common-tests/src/main/java/io/netty/channel/unix/tests/UnixTestUtils.java @@ -17,6 +17,7 @@ package io.netty.channel.unix.tests; import io.netty.channel.unix.DomainSocketAddress; import io.netty.channel.unix.Socket; +import io.netty.util.internal.PlatformDependent; import java.io.File; import java.io.IOException; @@ -26,7 +27,7 @@ public final class UnixTestUtils { try { File file; do { - file = File.createTempFile("NETTY", "UDS"); + file = PlatformDependent.createTempFile("NETTY", "UDS", null); if (!file.delete()) { throw new IOException("failed to delete: " + file); } diff --git a/transport/src/test/java/io/netty/channel/DefaultFileRegionTest.java b/transport/src/test/java/io/netty/channel/DefaultFileRegionTest.java index 47d6f3cc98..5c32445d8a 100644 --- a/transport/src/test/java/io/netty/channel/DefaultFileRegionTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultFileRegionTest.java @@ -15,6 +15,7 @@ */ package io.netty.channel; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import java.io.ByteArrayOutputStream; @@ -39,7 +40,7 @@ public class DefaultFileRegionTest { } private static File newFile() throws IOException { - File file = File.createTempFile("netty-", ".tmp"); + File file = PlatformDependent.createTempFile("netty-", ".tmp", null); file.deleteOnExit(); final FileOutputStream out = new FileOutputStream(file);