|
3 | 3 | * or more contributor license agreements. Licensed under the "Elastic License
|
4 | 4 | * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
5 | 5 | * Public License v 1"; you may not use this file except in compliance with, at
|
6 |
| - * your election, the "Elastic License 2.0", the "GNU Affero General Public |
7 |
| - * License v3.0 only", or the "Server Side Public License, v 1". |
| 6 | + * your election, the "Server Side Public License v3.0 only", or the "Server Side Public License, v 1". |
8 | 7 | */
|
9 | 8 |
|
10 | 9 | package org.elasticsearch.gradle;
|
|
18 | 17 | * An outputstream to a File that is lazily opened on the first write.
|
19 | 18 | */
|
20 | 19 | class LazyFileOutputStream extends OutputStream {
|
21 |
| - private OutputStream delegate; |
| 20 | + private final File file; |
| 21 | + private volatile OutputStream delegate; |
| 22 | + private volatile boolean initialized = false; |
| 23 | + private final Object lock = new Object(); |
22 | 24 |
|
23 | 25 | LazyFileOutputStream(File file) {
|
24 |
| - // use an initial dummy delegate to avoid doing a conditional on every write |
25 |
| - this.delegate = new OutputStream() { |
26 |
| - private void bootstrap() throws IOException { |
27 |
| - file.getParentFile().mkdirs(); |
28 |
| - delegate = new FileOutputStream(file); |
29 |
| - } |
30 |
| - |
31 |
| - @Override |
32 |
| - public void write(int b) throws IOException { |
33 |
| - bootstrap(); |
34 |
| - delegate.write(b); |
35 |
| - } |
36 |
| - |
37 |
| - @Override |
38 |
| - public void write(byte b[], int off, int len) throws IOException { |
39 |
| - bootstrap(); |
40 |
| - delegate.write(b, off, len); |
41 |
| - } |
| 26 | + this.file = file; |
| 27 | + } |
42 | 28 |
|
43 |
| - @Override |
44 |
| - public void write(byte b[]) throws IOException { |
45 |
| - bootstrap(); |
46 |
| - delegate.write(b); |
| 29 | + private void ensureInitialized() throws IOException { |
| 30 | + if (initialized == false) { |
| 31 | + synchronized (lock) { |
| 32 | + if (initialized == false) { |
| 33 | + file.getParentFile().mkdirs(); |
| 34 | + delegate = new FileOutputStream(file); |
| 35 | + initialized = true; |
| 36 | + } |
47 | 37 | }
|
48 |
| - }; |
| 38 | + } |
49 | 39 | }
|
50 | 40 |
|
51 | 41 | @Override
|
52 | 42 | public void write(int b) throws IOException {
|
| 43 | + ensureInitialized(); |
53 | 44 | delegate.write(b);
|
54 | 45 | }
|
55 | 46 |
|
56 | 47 | @Override
|
57 | 48 | public void write(byte b[], int off, int len) throws IOException {
|
| 49 | + ensureInitialized(); |
58 | 50 | delegate.write(b, off, len);
|
59 | 51 | }
|
60 | 52 |
|
| 53 | + @Override |
| 54 | + public void write(byte b[]) throws IOException { |
| 55 | + ensureInitialized(); |
| 56 | + delegate.write(b); |
| 57 | + } |
| 58 | + |
61 | 59 | @Override
|
62 | 60 | public void close() throws IOException {
|
63 |
| - delegate.close(); |
| 61 | + synchronized (lock) { |
| 62 | + if (initialized && delegate != null) { |
| 63 | + delegate.close(); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void flush() throws IOException { |
| 70 | + if (initialized && delegate != null) { |
| 71 | + delegate.flush(); |
| 72 | + } |
64 | 73 | }
|
65 | 74 | }
|
0 commit comments