|
1 | 1 | /* |
2 | | - * Copyright (c) 2018 by Tran Le Duy |
| 2 | + * Copyright (C) 2018 Tran Le Duy |
3 | 3 | * |
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. |
7 | 8 | * |
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. |
9 | 13 | * |
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/>. |
15 | 16 | */ |
16 | 17 |
|
17 | 18 | package com.duy.common.io; |
18 | 19 |
|
| 20 | + |
19 | 21 | import java.io.File; |
20 | | -import java.io.FileFilter; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileOutputStream; |
21 | 24 | import java.io.IOException; |
22 | 25 | import java.io.InputStream; |
23 | 26 | import java.io.InputStreamReader; |
| 27 | +import java.io.OutputStream; |
24 | 28 | 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; |
29 | 31 |
|
30 | 32 | 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 | + //----------------------------------------------------------------------- |
31 | 52 |
|
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) { |
42 | 64 | } |
43 | | - return out.toString(); |
44 | 65 | } |
45 | 66 |
|
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(); |
49 | 120 | } |
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 > 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; |
59 | 211 | } |
| 212 | + return count; |
60 | 213 | } |
61 | 214 |
|
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 > 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; |
70 | 289 | } |
| 290 | + return (int) count; |
71 | 291 | } |
72 | 292 | } |
0 commit comments