|
| 1 | +package com.clickhouse.client; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.nio.file.Path; |
| 8 | + |
| 9 | +import com.clickhouse.client.config.ClickHouseClientOption; |
| 10 | + |
| 11 | +/** |
| 12 | + * Wrapper of {@link java.io.File} with additional information like compression |
| 13 | + * and format. |
| 14 | + */ |
| 15 | +public class ClickHouseFile { |
| 16 | + /** |
| 17 | + * Null file which has no compression and format. |
| 18 | + */ |
| 19 | + public static final ClickHouseFile NULL = new ClickHouseFile(null, ClickHouseCompression.NONE, 0, null); |
| 20 | + |
| 21 | + public static ClickHouseFile of(File file) { |
| 22 | + return of(file, null, 0, null); |
| 23 | + } |
| 24 | + |
| 25 | + public static ClickHouseFile of(Path path) { |
| 26 | + return of(ClickHouseChecker.nonNull(path, "Path").toFile(), null, 0, null); |
| 27 | + } |
| 28 | + |
| 29 | + public static ClickHouseFile of(String file) { |
| 30 | + return of(new File(ClickHouseChecker.nonEmpty(file, "File")), null, 0, null); |
| 31 | + } |
| 32 | + |
| 33 | + public static ClickHouseFile of(String file, ClickHouseCompression compression, int compressionLevel, |
| 34 | + ClickHouseFormat format) { |
| 35 | + return of(new File(ClickHouseChecker.nonEmpty(file, "File")), compression, compressionLevel, format); |
| 36 | + } |
| 37 | + |
| 38 | + public static ClickHouseFile of(File file, ClickHouseCompression compression, int compressionLevel, |
| 39 | + ClickHouseFormat format) { |
| 40 | + return new ClickHouseFile(ClickHouseChecker.nonNull(file, "File"), |
| 41 | + compression != null ? compression : ClickHouseCompression.fromFileName(file.getName()), |
| 42 | + compressionLevel < 1 ? 0 : compressionLevel, |
| 43 | + format != null ? format : ClickHouseFormat.fromFileName(file.getName())); |
| 44 | + } |
| 45 | + |
| 46 | + private final File file; |
| 47 | + private final ClickHouseCompression compress; |
| 48 | + private final int compressLevel; |
| 49 | + private final ClickHouseFormat format; |
| 50 | + |
| 51 | + protected ClickHouseFile(File file, ClickHouseCompression compress, int compressLevel, ClickHouseFormat format) { |
| 52 | + this.file = file; |
| 53 | + this.compress = compress; |
| 54 | + this.compressLevel = compressLevel; |
| 55 | + this.format = format; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an input stream for reading the file. |
| 60 | + * |
| 61 | + * @return non-null input stream for reading the file |
| 62 | + */ |
| 63 | + public ClickHouseInputStream asInputStream() { |
| 64 | + if (!isAvailable()) { |
| 65 | + return ClickHouseInputStream.empty(); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + return ClickHouseInputStream.wrap(this, new FileInputStream(getFile()), |
| 70 | + (int) ClickHouseClientOption.READ_BUFFER_SIZE.getDefaultValue(), null, |
| 71 | + getCompressionAlgorithm(), getCompressionLevel()); |
| 72 | + } catch (FileNotFoundException e) { |
| 73 | + throw new IllegalArgumentException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates an output stream for writing data into the file. |
| 79 | + * |
| 80 | + * @return non-null input stream for writing data into the file |
| 81 | + */ |
| 82 | + public ClickHouseOutputStream asOutputStream() { |
| 83 | + if (!isAvailable()) { |
| 84 | + return ClickHouseOutputStream.empty(); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + return ClickHouseOutputStream.wrap(this, new FileOutputStream(getFile()), |
| 89 | + (int) ClickHouseClientOption.WRITE_BUFFER_SIZE.getDefaultValue(), null, |
| 90 | + getCompressionAlgorithm(), getCompressionLevel()); |
| 91 | + } catch (FileNotFoundException e) { |
| 92 | + throw new IllegalArgumentException(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets file, which only works when {@link #isAvailable()} returns {@code true}. |
| 98 | + * |
| 99 | + * @return non-null file, except {@code null} for {@link #NULL} |
| 100 | + */ |
| 101 | + public File getFile() { |
| 102 | + return file; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Gets file format, which could be null. Use {@link #hasFormat()} to check |
| 107 | + * first. |
| 108 | + * |
| 109 | + * @return file format, could be null |
| 110 | + */ |
| 111 | + public ClickHouseFormat getFormat() { |
| 112 | + return format; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets compression algorithm. |
| 117 | + * |
| 118 | + * @return non-null compression algorithm |
| 119 | + */ |
| 120 | + public ClickHouseCompression getCompressionAlgorithm() { |
| 121 | + return compress; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Gets compression level. |
| 126 | + * |
| 127 | + * @return compression level, which is always greater than or equal to zero |
| 128 | + */ |
| 129 | + public int getCompressionLevel() { |
| 130 | + return compressLevel; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Checks if the file format is defined or not. |
| 135 | + * |
| 136 | + * @return true if the file format is defined; false otherwise |
| 137 | + */ |
| 138 | + public boolean hasFormat() { |
| 139 | + return format != null; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Checks if the file is available or not. |
| 144 | + * |
| 145 | + * @return true if the file is available; false otherwise |
| 146 | + */ |
| 147 | + public boolean isAvailable() { |
| 148 | + return file != null && file.exists(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Checks if the file is compressed or not. |
| 153 | + * |
| 154 | + * @return true if the file is compressed; false otherwise |
| 155 | + */ |
| 156 | + public boolean isCompressed() { |
| 157 | + return compress != ClickHouseCompression.NONE; |
| 158 | + } |
| 159 | +} |
0 commit comments