Skip to content

Commit

Permalink
Improve file handling on Windows (#3764)
Browse files Browse the repository at this point in the history
Changes:
- replace `new FileInputStream` with `Files.newInputStream`
- replace `new FileOutputStream` with `Files.newOutputStream`

Motivation: 
#1939

https://bugs.openjdk.org/browse/JDK-6357433 (in a comment) says:
> For jdk7, the workaround is to **use new file system API**. So instead
of new FileInputStream(f) and new FileOutputStream(f), use
f.toPath().newInputStream() and f.toPath().newOutputStream()

So seems like new NIO API does use, FILE_SHARE_DELETE, which is needed
to delete files in unix-style.
  • Loading branch information
sake92 authored Oct 17, 2024
1 parent 2a5a13d commit 59e1bda
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import mill.api.Logger
import mill.scalalib.publish.Artifact
import mill.scalalib.publish.SonatypeHelpers.getArtifactMappings

import java.io.FileOutputStream
import java.nio.file.Files
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry

Expand Down Expand Up @@ -110,15 +110,15 @@ class SonatypeCentralPublisher(
wd: os.Path
)(func: JarOutputStream => Unit): java.io.File = {
val zipFile =
(wd / s"$fileNameWithoutExtension.zip").toIO
val fileOutputStream = new FileOutputStream(zipFile)
(wd / s"$fileNameWithoutExtension.zip")
val fileOutputStream = Files.newOutputStream(zipFile.toNIO)
val jarOutputStream = new JarOutputStream(fileOutputStream)
try {
func(jarOutputStream)
} finally {
jarOutputStream.close()
}
zipFile
zipFile.toIO
}

private def zipFilesToJar(
Expand Down
4 changes: 3 additions & 1 deletion main/api/src/mill/api/IO.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mill.api

import java.nio.file.Files

/**
* Misc IO utilities, eventually probably should be pushed upstream into
* ammonite-ops
Expand Down Expand Up @@ -29,7 +31,7 @@ object IO extends StreamSupport {
if (!entry.isDirectory) {
val entryDest = ctx.dest / dest / os.SubPath(entry.getName)
os.makeDir.all(entryDest / os.up)
val fileOut = new java.io.FileOutputStream(entryDest.toString)
val fileOut = Files.newOutputStream(entryDest.toNIO)
IO.stream(zipStream, fileOut)
fileOut.close()
}
Expand Down
8 changes: 4 additions & 4 deletions main/api/src/mill/api/JarOps.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package mill.api

import mill.api.Loose.Agg

import java.io.{BufferedOutputStream, FileOutputStream}
import java.io.BufferedOutputStream
import java.nio.file.Files
import java.util.jar.{JarEntry, JarOutputStream, Manifest}
import scala.collection.mutable
import mill.api.Loose.Agg

@experimental
trait JarOps {
Expand Down Expand Up @@ -76,7 +76,7 @@ trait JarOps {
val _ = seen.add(os.sub / "META-INF/MANIFEST.MF")

val jarStream = new JarOutputStream(
new BufferedOutputStream(new FileOutputStream(jar.toIO)),
new BufferedOutputStream(Files.newOutputStream(jar.toNIO)),
manifest
)

Expand Down
4 changes: 2 additions & 2 deletions main/client/src/mill/main/client/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -126,7 +125,8 @@ int run(Path serverDir, boolean setJnaNoSys, Locks locks) throws Exception {
) {
stdoutTailer.start();
stderrTailer.start();
try (FileOutputStream f = new FileOutputStream(serverDir + "/" + ServerFiles.runArgs)) {
String serverPath = serverDir + "/" + ServerFiles.runArgs;
try (OutputStream f = Files.newOutputStream(Paths.get(serverPath))) {
f.write(System.console() != null ? 1 : 0);
Util.writeString(f, BuildInfo.millVersion);
Util.writeArgs(args, f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import java.nio.file.Files;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -49,7 +48,7 @@ public void handleNoExistingFileThatAppearsLater() throws Exception {
assertEquals(bas.toString(), "");

try (
PrintStream out = new PrintStream(new FileOutputStream(file));
PrintStream out = new PrintStream(Files.newOutputStream(file.toPath()));
) {
out.println("log line");
assertTrue(file.exists());
Expand All @@ -76,7 +75,7 @@ public void handleExistingInitiallyEmptyFile() throws Exception{
assertEquals(bas.toString(), "");

try (
PrintStream out = new PrintStream(new FileOutputStream(file));
PrintStream out = new PrintStream(Files.newOutputStream(file.toPath()));
) {
out.println("log line");
assertTrue(file.exists());
Expand All @@ -95,7 +94,7 @@ public void handleExistingFileWithOldContent() throws Exception{
assertTrue(file.exists());

try (
PrintStream out = new PrintStream(new FileOutputStream(file));
PrintStream out = new PrintStream(Files.newOutputStream(file.toPath()));
) {
out.println("old line 1");
out.println("old line 2");
Expand Down Expand Up @@ -130,7 +129,7 @@ public void handleExistingEmptyFileWhichDisappearsAndComesBack() throws Exceptio
assertEquals(bas.toString(), "");

try (
PrintStream out = new PrintStream(new FileOutputStream(file));
PrintStream out = new PrintStream(Files.newOutputStream(file.toPath()));
) {
out.println("log line 1");
out.println("log line 2");
Expand All @@ -146,7 +145,7 @@ public void handleExistingEmptyFileWhichDisappearsAndComesBack() throws Exceptio
Thread.sleep(100);

try (
PrintStream out = new PrintStream(new FileOutputStream(file));
PrintStream out = new PrintStream(Files.newOutputStream(file.toPath()));
) {
out.println("new line");
assertTrue(file.exists());
Expand Down
18 changes: 7 additions & 11 deletions main/util/src/mill/util/Util.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mill.util

import java.nio.file.Files
import java.nio.file.Paths
import coursier.Repository
import mill.api.Loose.Agg
import mill.api.{BuildInfo, Ctx, IO, PathRef, Result}
Expand All @@ -19,7 +21,7 @@ object Util {
{
val millOptionsPath = sys.props("MILL_OPTIONS_PATH")
if (millOptionsPath != null)
LongMillProps.load(new java.io.FileInputStream(millOptionsPath))
LongMillProps.load(Files.newInputStream(Paths.get(millOptionsPath)))
}

def cleanupScaladoc(v: String): Array[String] = {
Expand All @@ -45,19 +47,13 @@ object Util {
ctx: Ctx.Dest
): PathRef = {
val out = ctx.dest / dest

val website = new java.net.URI(url).toURL
val rbc = java.nio.channels.Channels.newChannel(website.openStream)
val websiteInputStream = website.openStream
try {
val fos = new java.io.FileOutputStream(out.toIO)
try {
fos.getChannel.transferFrom(rbc, 0, java.lang.Long.MAX_VALUE)
PathRef(out)
} finally {
fos.close()
}
Files.copy(websiteInputStream, out.toNIO)
PathRef(out)
} finally {
rbc.close()
websiteInputStream.close()
}
}

Expand Down
4 changes: 2 additions & 2 deletions runner/client/src/mill/runner/client/MillProcessLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static mill.main.client.OutFiles.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -145,7 +145,7 @@ static String[] millClasspath() throws Exception {

// read MILL_CLASSPATH from file MILL_OPTIONS_PATH
Properties millProps = new Properties();
try (FileInputStream is = new FileInputStream(millOptionsPath)) {
try (InputStream is = Files.newInputStream(Paths.get(millOptionsPath))) {
millProps.load(is);
} catch (IOException e) {
throw new RuntimeException("Could not load '" + millOptionsPath + "'", e);
Expand Down
7 changes: 5 additions & 2 deletions runner/src/mill/runner/MillMain.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mill.runner

import java.io.{FileOutputStream, PipedInputStream, PrintStream}
import java.io.{PipedInputStream, PrintStream}
import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.Locale
import scala.jdk.CollectionConverters._
import scala.util.Properties
Expand Down Expand Up @@ -45,7 +47,8 @@ object MillMain {
// and all Mill output (stdout and stderr) goes to a dedicated file
val stderrFile = WorkspaceRoot.workspaceRoot / ".bsp/mill-bsp.stderr"
os.makeDir.all(stderrFile / os.up)
val errFile = new PrintStream(new FileOutputStream(stderrFile.toIO, true))
val errFile =
new PrintStream(Files.newOutputStream(stderrFile.toNIO, StandardOpenOption.APPEND))
val errTee = new TeePrintStream(initialSystemStreams.err, errFile)
val msg = s"Mill in BSP mode, version ${BuildInfo.millVersion}, ${new java.util.Date()}"
errTee.println(msg)
Expand Down
4 changes: 2 additions & 2 deletions testrunner/src/mill/testrunner/TestRunnerUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mill.api.{Ctx, Loose, TestReporter, internal}
import os.Path
import sbt.testing._

import java.io.FileInputStream
import java.nio.file.Files
import java.lang.annotation.Annotation
import java.lang.reflect.Modifier
import java.util.concurrent.ConcurrentLinkedQueue
Expand All @@ -19,7 +19,7 @@ import scala.jdk.CollectionConverters.IteratorHasAsScala
if (os.isDir(base)) {
os.walk.stream(base).filter(_.ext == "class").map(_.relativeTo(base).toString)
} else {
val zip = new ZipInputStream(new FileInputStream(base.toIO))
val zip = new ZipInputStream(Files.newInputStream(base.toNIO))
geny.Generator.selfClosing(
(
Iterator.continually(zip.getNextEntry)
Expand Down

0 comments on commit 59e1bda

Please sign in to comment.