Debian Codegraph @

buildSrc/src/main/java/org/apache/lucene/gradle/SpillWriter.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.lucene.gradle;
18
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.nio.charset.StandardCharsets;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.function.Supplier;
27
28 public class SpillWriter extends Writer {
29 private final static int MAX_BUFFERED = 2 * 1024;
30 private final StringWriter buffer = new StringWriter(MAX_BUFFERED);
31
32 private final Supplier<Path> spillPathSupplier;
33 private Writer spill;
34 private Path spillPath;
35
36 public SpillWriter(Supplier<Path> spillPathSupplier) {
37 this.spillPathSupplier = spillPathSupplier;
38 }
39
40 @Override
41 public void write(char[] cbuf, int off, int len) throws IOException {
42 getSink(len).write(cbuf, off, len);
43 }
44
45 @Override
46 public void write(int c) throws IOException {
47 getSink(1).write(c);
48 }
49
50 @Override
51 public void write(char[] cbuf) throws IOException {
52 getSink(cbuf.length).write(cbuf);
53 }
54
55 @Override
56 public void write(String str) throws IOException {
57 getSink(str.length()).write(str);
58 }
59
60 @Override
61 public void write(String str, int off, int len) throws IOException {
62 getSink(len).write(str, off, len);
63 }
64
65 @Override
66 public Writer append(CharSequence csq) throws IOException {
67 getSink(csq.length()).append(csq);
68 return this;
69 }
70
71 @Override
72 public Writer append(CharSequence csq, int start, int end) throws IOException {
73 getSink(Math.max(0, end - start)).append(csq, start, end);
74 return this;
75 }
76
77 @Override
78 public Writer append(char c) throws IOException {
79 getSink(1).append(c);
80 return this;
81 }
82
83 private Writer getSink(int expectedWriteChars) throws IOException {
84 if (spill == null) {
85 if (buffer.getBuffer().length() + expectedWriteChars <= MAX_BUFFERED) {
86 return buffer;
87 }
88
89 spillPath = spillPathSupplier.get();
90 spill = Files.newBufferedWriter(spillPath, StandardCharsets.UTF_8);
91 spill.append(buffer.getBuffer());
92 buffer.getBuffer().setLength(0);
93 }
94
95 return spill;
96 }
97
98 @Override
99 public void flush() throws IOException {
100 getSink(0).flush();
101 }
102
103 @Override
104 public void close() throws IOException {
105 buffer.close();
106 if (spill != null) {
107 spill.close();
108 Files.delete(spillPath);
109 }
110 }
111
112 public void copyTo(Writer writer) throws IOException {
113 if (spill != null) {
114 flush();
115 try (Reader reader = Files.newBufferedReader(spillPath, StandardCharsets.UTF_8)) {
116 reader.transferTo(writer);
117 }
118 } else {
119 writer.append(buffer.getBuffer());
120 }
121 }
122
123 public long length() throws IOException {
124 flush();
125 if (spill != null) {
126 return Files.size(spillPath);
127 } else {
128 return buffer.getBuffer().length();
129 }
130 }
131 }