|
| 1 | +package org.duckdb.io; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.Reader; |
| 7 | +import java.sql.SQLException; |
| 8 | + |
| 9 | +public class IOUtils { |
| 10 | + |
| 11 | + public static byte[] readAllBytes(InputStream x) throws SQLException { |
| 12 | + try { |
| 13 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 14 | + byte[] buffer = new byte[8192]; |
| 15 | + int length; |
| 16 | + while ((length = x.read(buffer)) != -1) { |
| 17 | + out.write(buffer, 0, length); |
| 18 | + } |
| 19 | + return out.toByteArray(); |
| 20 | + } catch (IOException e) { |
| 21 | + throw new SQLException(e); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public static String readToString(Reader reader) throws SQLException { |
| 26 | + try { |
| 27 | + StringBuilder sb = new StringBuilder(); |
| 28 | + char[] buffer = new char[4096]; |
| 29 | + int length; |
| 30 | + while ((length = reader.read(buffer)) != -1) { |
| 31 | + sb.append(buffer, 0, length); |
| 32 | + } |
| 33 | + return sb.toString(); |
| 34 | + } catch (IOException e) { |
| 35 | + throw new SQLException(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static InputStream wrapStreamWithMaxBytes(InputStream is, long maxBytes) { |
| 40 | + if (maxBytes < 0) { |
| 41 | + return is; |
| 42 | + } |
| 43 | + return new LimitedInputStream(is, maxBytes); |
| 44 | + } |
| 45 | + |
| 46 | + public static Reader wrapReaderWithMaxChars(Reader reader, long maxChars) { |
| 47 | + if (maxChars < 0) { |
| 48 | + return reader; |
| 49 | + } |
| 50 | + return new LimitedReader(reader, maxChars); |
| 51 | + } |
| 52 | +} |
0 commit comments