| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.io.BufferedReader; |
| 9 |
import java.io.BufferedWriter; |
| 10 |
import java.io.ByteArrayInputStream; |
| 11 |
import java.io.ByteArrayOutputStream; |
| 12 |
import java.io.EOFException; |
| 13 |
import java.io.File; |
| 14 |
import java.io.IOException; |
| 15 |
import java.io.InputStream; |
| 16 |
import java.io.InputStreamReader; |
| 17 |
import java.io.OutputStream; |
| 18 |
import java.io.OutputStreamWriter; |
| 19 |
import java.io.Reader; |
| 20 |
import java.io.StringWriter; |
| 21 |
import java.io.Writer; |
| 22 |
import java.nio.ByteBuffer; |
| 23 |
import java.nio.channels.FileChannel; |
| 24 |
import java.nio.charset.StandardCharsets; |
| 25 |
|
| 26 |
import org.h2.engine.Constants; |
| 27 |
import org.h2.engine.SysProperties; |
| 28 |
import org.h2.mvstore.DataUtils; |
| 29 |
import org.h2.store.fs.FileUtils; |
| 30 |
|
| 31 |
/** |
| 32 |
* This utility class contains input/output functions. |
| 33 |
*/ |
| 34 |
public class IOUtils { |
| 35 |
|
| 36 |
private IOUtils() { |
| 37 |
// utility class |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Close an AutoCloseable without throwing an exception. |
| 42 |
* |
| 43 |
* @param out the AutoCloseable or null |
| 44 |
*/ |
| 45 |
public static void closeSilently(AutoCloseable out) { |
| 46 |
if (out != null) { |
| 47 |
try { |
| 48 |
trace("closeSilently", null, out); |
| 49 |
out.close(); |
| 50 |
} catch (Exception e) { |
| 51 |
// ignore |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Skip a number of bytes in an input stream. |
| 58 |
* |
| 59 |
* @param in the input stream |
| 60 |
* @param skip the number of bytes to skip |
| 61 |
* @throws EOFException if the end of file has been reached before all bytes |
| 62 |
* could be skipped |
| 63 |
* @throws IOException if an IO exception occurred while skipping |
| 64 |
*/ |
| 65 |
public static void skipFully(InputStream in, long skip) throws IOException { |
| 66 |
try { |
| 67 |
while (skip > 0) { |
| 68 |
long skipped = in.skip(skip); |
| 69 |
if (skipped <= 0) { |
| 70 |
throw new EOFException(); |
| 71 |
} |
| 72 |
skip -= skipped; |
| 73 |
} |
| 74 |
} catch (Exception e) { |
| 75 |
throw DataUtils.convertToIOException(e); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Skip a number of characters in a reader. |
| 81 |
* |
| 82 |
* @param reader the reader |
| 83 |
* @param skip the number of characters to skip |
| 84 |
* @throws EOFException if the end of file has been reached before all |
| 85 |
* characters could be skipped |
| 86 |
* @throws IOException if an IO exception occurred while skipping |
| 87 |
*/ |
| 88 |
public static void skipFully(Reader reader, long skip) throws IOException { |
| 89 |
try { |
| 90 |
while (skip > 0) { |
| 91 |
long skipped = reader.skip(skip); |
| 92 |
if (skipped <= 0) { |
| 93 |
throw new EOFException(); |
| 94 |
} |
| 95 |
skip -= skipped; |
| 96 |
} |
| 97 |
} catch (Exception e) { |
| 98 |
throw DataUtils.convertToIOException(e); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Copy all data from the input stream to the output stream and close both |
| 104 |
* streams. Exceptions while closing are ignored. |
| 105 |
* |
| 106 |
* @param in the input stream |
| 107 |
* @param out the output stream |
| 108 |
* @return the number of bytes copied |
| 109 |
* @throws IOException on failure |
| 110 |
*/ |
| 111 |
public static long copyAndClose(InputStream in, OutputStream out) |
| 112 |
throws IOException { |
| 113 |
try { |
| 114 |
long len = copyAndCloseInput(in, out); |
| 115 |
out.close(); |
| 116 |
return len; |
| 117 |
} catch (Exception e) { |
| 118 |
throw DataUtils.convertToIOException(e); |
| 119 |
} finally { |
| 120 |
closeSilently(out); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Copy all data from the input stream to the output stream and close the |
| 126 |
* input stream. Exceptions while closing are ignored. |
| 127 |
* |
| 128 |
* @param in the input stream |
| 129 |
* @param out the output stream (null if writing is not required) |
| 130 |
* @return the number of bytes copied |
| 131 |
* @throws IOException on failure |
| 132 |
*/ |
| 133 |
public static long copyAndCloseInput(InputStream in, OutputStream out) |
| 134 |
throws IOException { |
| 135 |
try { |
| 136 |
return copy(in, out); |
| 137 |
} catch (Exception e) { |
| 138 |
throw DataUtils.convertToIOException(e); |
| 139 |
} finally { |
| 140 |
closeSilently(in); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Copy all data from the input stream to the output stream. Both streams |
| 146 |
* are kept open. |
| 147 |
* |
| 148 |
* @param in the input stream |
| 149 |
* @param out the output stream (null if writing is not required) |
| 150 |
* @return the number of bytes copied |
| 151 |
* @throws IOException on failure |
| 152 |
*/ |
| 153 |
public static long copy(InputStream in, OutputStream out) |
| 154 |
throws IOException { |
| 155 |
return copy(in, out, Long.MAX_VALUE); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Copy all data from the input stream to the output stream. Both streams |
| 160 |
* are kept open. |
| 161 |
* |
| 162 |
* @param in the input stream |
| 163 |
* @param out the output stream (null if writing is not required) |
| 164 |
* @param length the maximum number of bytes to copy |
| 165 |
* @return the number of bytes copied |
| 166 |
* @throws IOException on failure |
| 167 |
*/ |
| 168 |
public static long copy(InputStream in, OutputStream out, long length) |
| 169 |
throws IOException { |
| 170 |
try { |
| 171 |
long copied = 0; |
| 172 |
int len = (int) Math.min(length, Constants.IO_BUFFER_SIZE); |
| 173 |
byte[] buffer = new byte[len]; |
| 174 |
while (length > 0) { |
| 175 |
len = in.read(buffer, 0, len); |
| 176 |
if (len < 0) { |
| 177 |
break; |
| 178 |
} |
| 179 |
if (out != null) { |
| 180 |
out.write(buffer, 0, len); |
| 181 |
} |
| 182 |
copied += len; |
| 183 |
length -= len; |
| 184 |
len = (int) Math.min(length, Constants.IO_BUFFER_SIZE); |
| 185 |
} |
| 186 |
return copied; |
| 187 |
} catch (Exception e) { |
| 188 |
throw DataUtils.convertToIOException(e); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Copy all data from the input FileChannel to the output stream. Both source and destination |
| 194 |
* are kept open. |
| 195 |
* |
| 196 |
* @param in the input FileChannel |
| 197 |
* @param out the output stream (null if writing is not required) |
| 198 |
* @return the number of bytes copied |
| 199 |
* @throws IOException on failure |
| 200 |
*/ |
| 201 |
public static long copy(FileChannel in, OutputStream out) |
| 202 |
throws IOException { |
| 203 |
return copy(in, out, Long.MAX_VALUE); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Copy all data from the input FileChannel to the output stream. Both source and destination |
| 208 |
* are kept open. |
| 209 |
* |
| 210 |
* @param in the input FileChannel |
| 211 |
* @param out the output stream (null if writing is not required) |
| 212 |
* @param length the maximum number of bytes to copy |
| 213 |
* @return the number of bytes copied |
| 214 |
* @throws IOException on failure |
| 215 |
*/ |
| 216 |
public static long copy(FileChannel in, OutputStream out, long length) |
| 217 |
throws IOException { |
| 218 |
try { |
| 219 |
long copied = 0; |
| 220 |
byte[] buffer = new byte[(int) Math.min(length, Constants.IO_BUFFER_SIZE)]; |
| 221 |
ByteBuffer wrap = ByteBuffer.wrap(buffer); |
| 222 |
while (length > 0) { |
| 223 |
int len = in.read(wrap, copied); |
| 224 |
if (len < 0) { |
| 225 |
break; |
| 226 |
} |
| 227 |
if (out != null) { |
| 228 |
out.write(buffer, 0, len); |
| 229 |
} |
| 230 |
copied += len; |
| 231 |
length -= len; |
| 232 |
wrap.rewind(); |
| 233 |
if (length < wrap.limit()) { |
| 234 |
wrap.limit((int)length); |
| 235 |
} |
| 236 |
} |
| 237 |
return copied; |
| 238 |
} catch (Exception e) { |
| 239 |
throw DataUtils.convertToIOException(e); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Copy all data from the reader to the writer and close the reader. |
| 245 |
* Exceptions while closing are ignored. |
| 246 |
* |
| 247 |
* @param in the reader |
| 248 |
* @param out the writer (null if writing is not required) |
| 249 |
* @param length the maximum number of bytes to copy |
| 250 |
* @return the number of characters copied |
| 251 |
* @throws IOException on failure |
| 252 |
*/ |
| 253 |
public static long copyAndCloseInput(Reader in, Writer out, long length) |
| 254 |
throws IOException { |
| 255 |
try { |
| 256 |
long copied = 0; |
| 257 |
int len = (int) Math.min(length, Constants.IO_BUFFER_SIZE); |
| 258 |
char[] buffer = new char[len]; |
| 259 |
while (length > 0) { |
| 260 |
len = in.read(buffer, 0, len); |
| 261 |
if (len < 0) { |
| 262 |
break; |
| 263 |
} |
| 264 |
if (out != null) { |
| 265 |
out.write(buffer, 0, len); |
| 266 |
} |
| 267 |
copied += len; |
| 268 |
length -= len; |
| 269 |
len = (int) Math.min(length, Constants.IO_BUFFER_SIZE); |
| 270 |
} |
| 271 |
return copied; |
| 272 |
} catch (Exception e) { |
| 273 |
throw DataUtils.convertToIOException(e); |
| 274 |
} finally { |
| 275 |
in.close(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Read a number of bytes from an input stream and close the stream. |
| 281 |
* |
| 282 |
* @param in the input stream |
| 283 |
* @param length the maximum number of bytes to read, or -1 to read until |
| 284 |
* the end of file |
| 285 |
* @return the bytes read |
| 286 |
* @throws IOException on failure |
| 287 |
*/ |
| 288 |
public static byte[] readBytesAndClose(InputStream in, int length) |
| 289 |
throws IOException { |
| 290 |
try { |
| 291 |
if (length <= 0) { |
| 292 |
length = Integer.MAX_VALUE; |
| 293 |
} |
| 294 |
int block = Math.min(Constants.IO_BUFFER_SIZE, length); |
| 295 |
ByteArrayOutputStream out = new ByteArrayOutputStream(block); |
| 296 |
copy(in, out, length); |
| 297 |
return out.toByteArray(); |
| 298 |
} catch (Exception e) { |
| 299 |
throw DataUtils.convertToIOException(e); |
| 300 |
} finally { |
| 301 |
in.close(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Read a number of characters from a reader and close it. |
| 307 |
* |
| 308 |
* @param in the reader |
| 309 |
* @param length the maximum number of characters to read, or -1 to read |
| 310 |
* until the end of file |
| 311 |
* @return the string read |
| 312 |
* @throws IOException on failure |
| 313 |
*/ |
| 314 |
public static String readStringAndClose(Reader in, int length) |
| 315 |
throws IOException { |
| 316 |
try { |
| 317 |
if (length <= 0) { |
| 318 |
length = Integer.MAX_VALUE; |
| 319 |
} |
| 320 |
int block = Math.min(Constants.IO_BUFFER_SIZE, length); |
| 321 |
StringWriter out = new StringWriter(block); |
| 322 |
copyAndCloseInput(in, out, length); |
| 323 |
return out.toString(); |
| 324 |
} finally { |
| 325 |
in.close(); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Try to read the given number of bytes to the buffer. This method reads |
| 331 |
* until the maximum number of bytes have been read or until the end of |
| 332 |
* file. |
| 333 |
* |
| 334 |
* @param in the input stream |
| 335 |
* @param buffer the output buffer |
| 336 |
* @param max the number of bytes to read at most |
| 337 |
* @return the number of bytes read, 0 meaning EOF |
| 338 |
* @throws IOException on failure |
| 339 |
*/ |
| 340 |
public static int readFully(InputStream in, byte[] buffer, int max) |
| 341 |
throws IOException { |
| 342 |
try { |
| 343 |
int result = 0, len = Math.min(max, buffer.length); |
| 344 |
while (len > 0) { |
| 345 |
int l = in.read(buffer, result, len); |
| 346 |
if (l < 0) { |
| 347 |
break; |
| 348 |
} |
| 349 |
result += l; |
| 350 |
len -= l; |
| 351 |
} |
| 352 |
return result; |
| 353 |
} catch (Exception e) { |
| 354 |
throw DataUtils.convertToIOException(e); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Try to read the given number of characters to the buffer. This method |
| 360 |
* reads until the maximum number of characters have been read or until the |
| 361 |
* end of file. |
| 362 |
* |
| 363 |
* @param in the reader |
| 364 |
* @param buffer the output buffer |
| 365 |
* @param max the number of characters to read at most |
| 366 |
* @return the number of characters read, 0 meaning EOF |
| 367 |
* @throws IOException on failure |
| 368 |
*/ |
| 369 |
public static int readFully(Reader in, char[] buffer, int max) |
| 370 |
throws IOException { |
| 371 |
try { |
| 372 |
int result = 0, len = Math.min(max, buffer.length); |
| 373 |
while (len > 0) { |
| 374 |
int l = in.read(buffer, result, len); |
| 375 |
if (l < 0) { |
| 376 |
break; |
| 377 |
} |
| 378 |
result += l; |
| 379 |
len -= l; |
| 380 |
} |
| 381 |
return result; |
| 382 |
} catch (Exception e) { |
| 383 |
throw DataUtils.convertToIOException(e); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Create a reader to read from an input stream using the UTF-8 format. If |
| 389 |
* the input stream is null, this method returns null. The InputStreamReader |
| 390 |
* that is used here is not exact, that means it may read some additional |
| 391 |
* bytes when buffering. |
| 392 |
* |
| 393 |
* @param in the input stream or null |
| 394 |
* @return the reader |
| 395 |
*/ |
| 396 |
public static Reader getReader(InputStream in) { |
| 397 |
// InputStreamReader may read some more bytes |
| 398 |
return in == null ? null : new BufferedReader( |
| 399 |
new InputStreamReader(in, StandardCharsets.UTF_8)); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Create a buffered writer to write to an output stream using the UTF-8 |
| 404 |
* format. If the output stream is null, this method returns null. |
| 405 |
* |
| 406 |
* @param out the output stream or null |
| 407 |
* @return the writer |
| 408 |
*/ |
| 409 |
public static Writer getBufferedWriter(OutputStream out) { |
| 410 |
return out == null ? null : new BufferedWriter( |
| 411 |
new OutputStreamWriter(out, StandardCharsets.UTF_8)); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Wrap an input stream in a reader. The bytes are converted to characters |
| 416 |
* using the US-ASCII character set. |
| 417 |
* |
| 418 |
* @param in the input stream |
| 419 |
* @return the reader |
| 420 |
*/ |
| 421 |
public static Reader getAsciiReader(InputStream in) { |
| 422 |
return in == null ? null : new InputStreamReader(in, StandardCharsets.US_ASCII); |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Trace input or output operations if enabled. |
| 427 |
* |
| 428 |
* @param method the method from where this method was called |
| 429 |
* @param fileName the file name |
| 430 |
* @param o the object to append to the message |
| 431 |
*/ |
| 432 |
public static void trace(String method, String fileName, Object o) { |
| 433 |
if (SysProperties.TRACE_IO) { |
| 434 |
System.out.println("IOUtils." + method + ' ' + fileName + ' ' + o); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Create an input stream to read from a string. The string is converted to |
| 440 |
* a byte array using UTF-8 encoding. |
| 441 |
* If the string is null, this method returns null. |
| 442 |
* |
| 443 |
* @param s the string |
| 444 |
* @return the input stream |
| 445 |
*/ |
| 446 |
public static InputStream getInputStreamFromString(String s) { |
| 447 |
if (s == null) { |
| 448 |
return null; |
| 449 |
} |
| 450 |
return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Copy a file from one directory to another, or to another file. |
| 455 |
* |
| 456 |
* @param original the original file name |
| 457 |
* @param copy the file name of the copy |
| 458 |
* @throws IOException on failure |
| 459 |
*/ |
| 460 |
public static void copyFiles(String original, String copy) throws IOException { |
| 461 |
InputStream in = FileUtils.newInputStream(original); |
| 462 |
OutputStream out = FileUtils.newOutputStream(copy, false); |
| 463 |
copyAndClose(in, out); |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Converts / and \ name separators in path to native separators. |
| 468 |
* |
| 469 |
* @param path path to convert |
| 470 |
* @return path with converted separators |
| 471 |
*/ |
| 472 |
public static String nameSeparatorsToNative(String path) { |
| 473 |
return File.separatorChar == '/' ? path.replace('\\', '/') : path.replace('/', '\\'); |
| 474 |
} |
| 475 |
|
| 476 |
} |