Skip to content

Commit 25d80c3

Browse files
committed
remove common IO
1 parent 95fa902 commit 25d80c3

File tree

20 files changed

+478
-370
lines changed

20 files changed

+478
-370
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/duy/ccppcompiler/compiler/manager/CompileManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
import com.duy.ccppcompiler.compiler.model.OutputScope;
2626
import com.duy.ccppcompiler.compiler.shell.CommandResult;
2727
import com.duy.ccppcompiler.console.TermActivity;
28-
import com.duy.ccppcompiler.pkgmanager.Environment;
2928
import com.duy.ccppcompiler.ide.editor.CppIdeActivity;
29+
import com.duy.ccppcompiler.pkgmanager.Environment;
30+
import com.duy.common.io.IOUtils;
3031
import com.jecelyin.common.utils.UIUtils;
3132
import com.pdaxrom.cctools.BuildConstants;
3233
import com.pdaxrom.utils.Utils;
3334

34-
import org.apache.commons.io.IOUtils;
35-
3635
import java.io.File;
3736
import java.io.FileInputStream;
3837
import java.io.FileOutputStream;
Lines changed: 266 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,292 @@
11
/*
2-
* Copyright (c) 2018 by Tran Le Duy
2+
* Copyright (C) 2018 Tran Le Duy
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
78
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
913
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1516
*/
1617

1718
package com.duy.common.io;
1819

20+
1921
import java.io.File;
20-
import java.io.FileFilter;
22+
import java.io.FileInputStream;
23+
import java.io.FileOutputStream;
2124
import java.io.IOException;
2225
import java.io.InputStream;
2326
import java.io.InputStreamReader;
27+
import java.io.OutputStream;
2428
import java.io.Reader;
25-
26-
/**
27-
* Created by Duy on 12-Apr-18.
28-
*/
29+
import java.io.Writer;
30+
import java.nio.charset.Charset;
2931

3032
public class IOUtils {
33+
/**
34+
* Represents the end-of-file (or stream).
35+
*
36+
* @since 2.5 (made public)
37+
*/
38+
public static final int EOF = -1;
39+
/**
40+
* The default buffer size ({@value}) to use for
41+
* {@link #copyLarge(InputStream, OutputStream)}
42+
* and
43+
* {@link #copyLarge(Reader, Writer)}
44+
*/
45+
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
46+
/**
47+
* The default buffer size to use for the skip() methods.
48+
*/
49+
private static final int SKIP_BUFFER_SIZE = 2048;
50+
// read toString
51+
//-----------------------------------------------------------------------
3152

32-
public static String toString(InputStream inputStream) throws IOException {
33-
final int bufferSize = 1024;
34-
final char[] buffer = new char[bufferSize];
35-
final StringBuilder out = new StringBuilder();
36-
Reader in = new InputStreamReader(inputStream, "UTF-8");
37-
for (; ; ) {
38-
int rsz = in.read(buffer, 0, buffer.length);
39-
if (rsz < 0)
40-
break;
41-
out.append(buffer, 0, rsz);
53+
public static void copyNotIfExistAndClose(InputStream input, File outputFile) throws IOException {
54+
if (outputFile.exists()) {
55+
return;
56+
}
57+
outputFile.getParentFile().mkdirs();
58+
FileOutputStream output = new FileOutputStream(outputFile);
59+
copy(input, output);
60+
output.close();
61+
try {
62+
input.close();
63+
} catch (Exception ignored) {
4264
}
43-
return out.toString();
4465
}
4566

46-
public static void delete(File file) {
47-
if (!file.exists()) {
48-
return;
67+
public static String toString(File file) throws IOException {
68+
FileInputStream input = new FileInputStream(file);
69+
String s = toString(input);
70+
input.close();
71+
return s;
72+
}
73+
74+
public static void writeAndClose(String content, File file) throws IOException {
75+
FileOutputStream output = new FileOutputStream(file);
76+
output.write(content.getBytes());
77+
output.close();
78+
}
79+
80+
/**
81+
* Gets the contents of an <code>InputStream</code> as a String
82+
* using the default character encoding of the platform.
83+
* <p>
84+
* This method buffers the input internally, so there is no need to use a
85+
* <code>BufferedInputStream</code>.
86+
*
87+
* @param input the <code>InputStream</code> to read from
88+
* @return the requested String
89+
* @throws NullPointerException if the input is null
90+
* @throws IOException if an I/O error occurs
91+
* @deprecated 2.5 use {@link #toString(InputStream, Charset)} instead
92+
*/
93+
@Deprecated
94+
public static String toString(final InputStream input) throws IOException {
95+
return toString(input, Charset.defaultCharset());
96+
}
97+
98+
/**
99+
* Gets the contents of an <code>InputStream</code> as a String
100+
* using the specified character encoding.
101+
* <p>
102+
* This method buffers the input internally, so there is no need to use a
103+
* <code>BufferedInputStream</code>.
104+
* </p>
105+
*
106+
* @param input the <code>InputStream</code> to read from
107+
* @param encoding the encoding to use, null means platform default
108+
* @return the requested String
109+
* @throws NullPointerException if the input is null
110+
* @throws IOException if an I/O error occurs
111+
* @since 2.3
112+
*/
113+
public static String toString(final InputStream input, final Charset encoding) throws IOException {
114+
final StringBuilderWriter sw = new StringBuilderWriter();
115+
try {
116+
copy(input, sw, encoding);
117+
return sw.toString();
118+
} finally {
119+
sw.close();
49120
}
50-
if (file.isDirectory()) {
51-
File[] files = file.listFiles();
52-
if (file != null) {
53-
for (File f : files) {
54-
delete(f);
55-
}
56-
}
57-
} else if (file.isFile()) {
58-
file.delete();
121+
}
122+
123+
/**
124+
* Copies bytes from an <code>InputStream</code> to chars on a
125+
* <code>Writer</code> using the specified character encoding.
126+
* <p>
127+
* This method buffers the input internally, so there is no need to use a
128+
* <code>BufferedInputStream</code>.
129+
* <p>
130+
* This method uses {@link InputStreamReader}.
131+
*
132+
* @param input the <code>InputStream</code> to read from
133+
* @param output the <code>Writer</code> to write to
134+
* @param inputEncoding the encoding to use for the input stream, null means platform default
135+
* @throws NullPointerException if the input or output is null
136+
* @throws IOException if an I/O error occurs
137+
* @since 2.3
138+
*/
139+
public static void copy(final InputStream input, final Writer output, final Charset inputEncoding)
140+
throws IOException {
141+
final InputStreamReader in = new InputStreamReader(input, inputEncoding);
142+
copy(in, output);
143+
}
144+
145+
/**
146+
* Copies chars from a <code>Reader</code> to a <code>Writer</code>.
147+
* <p>
148+
* This method buffers the input internally, so there is no need to use a
149+
* <code>BufferedReader</code>.
150+
* <p>
151+
* Large streams (over 2GB) will return a chars copied value of
152+
* <code>-1</code> after the copy has completed since the correct
153+
* number of chars cannot be returned as an int. For large streams
154+
* use the <code>copyLarge(Reader, Writer)</code> method.
155+
*
156+
* @param input the <code>Reader</code> to read from
157+
* @param output the <code>Writer</code> to write to
158+
* @return the number of characters copied, or -1 if &gt; Integer.MAX_VALUE
159+
* @throws NullPointerException if the input or output is null
160+
* @throws IOException if an I/O error occurs
161+
* @since 1.1
162+
*/
163+
public static int copy(final Reader input, final Writer output) throws IOException {
164+
final long count = copyLarge(input, output);
165+
if (count > Integer.MAX_VALUE) {
166+
return -1;
167+
}
168+
return (int) count;
169+
}
170+
171+
/**
172+
* Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
173+
* <p>
174+
* This method buffers the input internally, so there is no need to use a
175+
* <code>BufferedReader</code>.
176+
* <p>
177+
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
178+
*
179+
* @param input the <code>Reader</code> to read from
180+
* @param output the <code>Writer</code> to write to
181+
* @return the number of characters copied
182+
* @throws NullPointerException if the input or output is null
183+
* @throws IOException if an I/O error occurs
184+
* @since 1.3
185+
*/
186+
public static long copyLarge(final Reader input, final Writer output) throws IOException {
187+
return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]);
188+
}
189+
190+
/**
191+
* Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
192+
* <p>
193+
* This method uses the provided buffer, so there is no need to use a
194+
* <code>BufferedReader</code>.
195+
* <p>
196+
*
197+
* @param input the <code>Reader</code> to read from
198+
* @param output the <code>Writer</code> to write to
199+
* @param buffer the buffer to be used for the copy
200+
* @return the number of characters copied
201+
* @throws NullPointerException if the input or output is null
202+
* @throws IOException if an I/O error occurs
203+
* @since 2.2
204+
*/
205+
public static long copyLarge(final Reader input, final Writer output, final char[] buffer) throws IOException {
206+
long count = 0;
207+
int n;
208+
while (EOF != (n = input.read(buffer))) {
209+
output.write(buffer, 0, n);
210+
count += n;
59211
}
212+
return count;
60213
}
61214

62-
public static void deleteChild(File toDelete, FileFilter fileFilter) {
63-
File[] files = toDelete.listFiles();
64-
if (files != null) {
65-
for (File file : files) {
66-
if (fileFilter.accept(file)) {
67-
file.delete();
68-
}
69-
}
215+
/**
216+
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
217+
* <code>OutputStream</code>.
218+
* <p>
219+
* This method buffers the input internally, so there is no need to use a
220+
* <code>BufferedInputStream</code>.
221+
* <p>
222+
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
223+
*
224+
* @param input the <code>InputStream</code> to read from
225+
* @param output the <code>OutputStream</code> to write to
226+
* @return the number of bytes copied
227+
* @throws NullPointerException if the input or output is null
228+
* @throws IOException if an I/O error occurs
229+
* @since 1.3
230+
*/
231+
public static long copyLarge(InputStream input, OutputStream output)
232+
throws IOException {
233+
return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
234+
}
235+
236+
/**
237+
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
238+
* <code>OutputStream</code>.
239+
* <p>
240+
* This method uses the provided buffer, so there is no need to use a
241+
* <code>BufferedInputStream</code>.
242+
* <p>
243+
*
244+
* @param input the <code>InputStream</code> to read from
245+
* @param output the <code>OutputStream</code> to write to
246+
* @param buffer the buffer to use for the copy
247+
* @return the number of bytes copied
248+
* @throws NullPointerException if the input or output is null
249+
* @throws IOException if an I/O error occurs
250+
* @since 2.2
251+
*/
252+
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
253+
throws IOException {
254+
long count = 0;
255+
int n = 0;
256+
while (EOF != (n = input.read(buffer))) {
257+
output.write(buffer, 0, n);
258+
count += n;
259+
}
260+
return count;
261+
}
262+
263+
// copy from InputStream
264+
//-----------------------------------------------------------------------
265+
266+
/**
267+
* Copy bytes from an <code>InputStream</code> to an
268+
* <code>OutputStream</code>.
269+
* <p>
270+
* This method buffers the input internally, so there is no need to use a
271+
* <code>BufferedInputStream</code>.
272+
* <p>
273+
* Large streams (over 2GB) will return a bytes copied value of
274+
* <code>-1</code> after the copy has completed since the correct
275+
* number of bytes cannot be returned as an int. For large streams
276+
* use the <code>copyLarge(InputStream, OutputStream)</code> method.
277+
*
278+
* @param input the <code>InputStream</code> to read from
279+
* @param output the <code>OutputStream</code> to write to
280+
* @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE
281+
* @throws NullPointerException if the input or output is null
282+
* @throws IOException if an I/O error occurs
283+
* @since 1.1
284+
*/
285+
public static int copy(InputStream input, OutputStream output) throws IOException {
286+
long count = copyLarge(input, output);
287+
if (count > Integer.MAX_VALUE) {
288+
return -1;
70289
}
290+
return (int) count;
71291
}
72292
}

0 commit comments

Comments
 (0)