equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
382 } |
382 } |
383 } |
383 } |
384 return Channels.newInputStream(Files.newByteChannel(path, options)); |
384 return Channels.newInputStream(Files.newByteChannel(path, options)); |
385 } |
385 } |
386 |
386 |
|
387 private static final Set<OpenOption> DEFAULT_OPEN_OPTIONS = |
|
388 Set.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, |
|
389 StandardOpenOption.WRITE); |
|
390 |
387 /** |
391 /** |
388 * Opens or creates a file, returning an output stream that may be used to |
392 * Opens or creates a file, returning an output stream that may be used to |
389 * write bytes to the file. This method works in exactly the manner |
393 * write bytes to the file. This method works in exactly the manner |
390 * specified by the {@link Files#newOutputStream} method. |
394 * specified by the {@link Files#newOutputStream} method. |
391 * |
395 * |
417 */ |
421 */ |
418 public OutputStream newOutputStream(Path path, OpenOption... options) |
422 public OutputStream newOutputStream(Path path, OpenOption... options) |
419 throws IOException |
423 throws IOException |
420 { |
424 { |
421 int len = options.length; |
425 int len = options.length; |
422 Set<OpenOption> opts = new HashSet<>(len + 3); |
426 Set<OpenOption> opts ; |
423 if (len == 0) { |
427 if (len == 0) { |
424 opts.add(StandardOpenOption.CREATE); |
428 opts = DEFAULT_OPEN_OPTIONS; |
425 opts.add(StandardOpenOption.TRUNCATE_EXISTING); |
|
426 } else { |
429 } else { |
|
430 opts = new HashSet<>(); |
427 for (OpenOption opt: options) { |
431 for (OpenOption opt: options) { |
428 if (opt == StandardOpenOption.READ) |
432 if (opt == StandardOpenOption.READ) |
429 throw new IllegalArgumentException("READ not allowed"); |
433 throw new IllegalArgumentException("READ not allowed"); |
430 opts.add(opt); |
434 opts.add(opt); |
431 } |
435 } |
|
436 opts.add(StandardOpenOption.WRITE); |
432 } |
437 } |
433 opts.add(StandardOpenOption.WRITE); |
|
434 return Channels.newOutputStream(newByteChannel(path, opts)); |
438 return Channels.newOutputStream(newByteChannel(path, opts)); |
435 } |
439 } |
436 |
440 |
437 /** |
441 /** |
438 * Opens or creates a file for reading and/or writing, returning a file |
442 * Opens or creates a file for reading and/or writing, returning a file |