8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
Reviewed-by: alanb
This commit is contained in:
parent
91d9f3fbe0
commit
df509b1b16
src/java.base/share/classes/java/nio
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -301,8 +301,13 @@ public abstract class AsynchronousFileChannel
|
|||||||
public static AsynchronousFileChannel open(Path file, OpenOption... options)
|
public static AsynchronousFileChannel open(Path file, OpenOption... options)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Set<OpenOption> set = new HashSet<>(options.length);
|
Set<OpenOption> set;
|
||||||
Collections.addAll(set, options);
|
if (options.length == 0) {
|
||||||
|
set = Collections.emptySet();
|
||||||
|
} else {
|
||||||
|
set = new HashSet<>();
|
||||||
|
Collections.addAll(set, options);
|
||||||
|
}
|
||||||
return open(file, set, null, NO_ATTRIBUTES);
|
return open(file, set, null, NO_ATTRIBUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -335,8 +335,13 @@ public abstract class FileChannel
|
|||||||
public static FileChannel open(Path path, OpenOption... options)
|
public static FileChannel open(Path path, OpenOption... options)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Set<OpenOption> set = new HashSet<>(options.length);
|
Set<OpenOption> set;
|
||||||
Collections.addAll(set, options);
|
if (options.length == 0) {
|
||||||
|
set = Collections.emptySet();
|
||||||
|
} else {
|
||||||
|
set = new HashSet<>();
|
||||||
|
Collections.addAll(set, options);
|
||||||
|
}
|
||||||
return open(path, set, NO_ATTRIBUTES);
|
return open(path, set, NO_ATTRIBUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,8 +410,13 @@ public final class Files {
|
|||||||
public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
|
public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Set<OpenOption> set = new HashSet<>(options.length);
|
Set<OpenOption> set;
|
||||||
Collections.addAll(set, options);
|
if (options.length == 0) {
|
||||||
|
set = Collections.emptySet();
|
||||||
|
} else {
|
||||||
|
set = new HashSet<>();
|
||||||
|
Collections.addAll(set, options);
|
||||||
|
}
|
||||||
return newByteChannel(path, set);
|
return newByteChannel(path, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -599,6 +604,9 @@ public final class Files {
|
|||||||
|
|
||||||
// -- Creation and deletion --
|
// -- Creation and deletion --
|
||||||
|
|
||||||
|
private static final Set<OpenOption> DEFAULT_CREATE_OPTIONS =
|
||||||
|
Set.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new and empty file, failing if the file already exists. The
|
* Creates a new and empty file, failing if the file already exists. The
|
||||||
* check for the existence of the file and the creation of the new file if
|
* check for the existence of the file and the creation of the new file if
|
||||||
@ -635,9 +643,7 @@ public final class Files {
|
|||||||
public static Path createFile(Path path, FileAttribute<?>... attrs)
|
public static Path createFile(Path path, FileAttribute<?>... attrs)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
EnumSet<StandardOpenOption> options =
|
newByteChannel(path, DEFAULT_CREATE_OPTIONS, attrs).close();
|
||||||
EnumSet.<StandardOpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
|
|
||||||
newByteChannel(path, options, attrs).close();
|
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -384,6 +384,10 @@ public abstract class FileSystemProvider {
|
|||||||
return Channels.newInputStream(Files.newByteChannel(path, options));
|
return Channels.newInputStream(Files.newByteChannel(path, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Set<OpenOption> DEFAULT_OPEN_OPTIONS =
|
||||||
|
Set.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING,
|
||||||
|
StandardOpenOption.WRITE);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens or creates a file, returning an output stream that may be used to
|
* Opens or creates a file, returning an output stream that may be used to
|
||||||
* write bytes to the file. This method works in exactly the manner
|
* write bytes to the file. This method works in exactly the manner
|
||||||
@ -419,18 +423,18 @@ public abstract class FileSystemProvider {
|
|||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
int len = options.length;
|
int len = options.length;
|
||||||
Set<OpenOption> opts = new HashSet<>(len + 3);
|
Set<OpenOption> opts ;
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
opts.add(StandardOpenOption.CREATE);
|
opts = DEFAULT_OPEN_OPTIONS;
|
||||||
opts.add(StandardOpenOption.TRUNCATE_EXISTING);
|
|
||||||
} else {
|
} else {
|
||||||
|
opts = new HashSet<>();
|
||||||
for (OpenOption opt: options) {
|
for (OpenOption opt: options) {
|
||||||
if (opt == StandardOpenOption.READ)
|
if (opt == StandardOpenOption.READ)
|
||||||
throw new IllegalArgumentException("READ not allowed");
|
throw new IllegalArgumentException("READ not allowed");
|
||||||
opts.add(opt);
|
opts.add(opt);
|
||||||
}
|
}
|
||||||
|
opts.add(StandardOpenOption.WRITE);
|
||||||
}
|
}
|
||||||
opts.add(StandardOpenOption.WRITE);
|
|
||||||
return Channels.newOutputStream(newByteChannel(path, opts));
|
return Channels.newOutputStream(newByteChannel(path, opts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user