/** * Open a file for input with the default charset. * * @param file The file to open. * @param compression The compression mode. * @return A reader opened on the file. * @throws IOException if there was an error opening the file. * @see #openInput(java.io.File, Charset, CompressionMode) */ public static Reader openInput(File file, CompressionMode compression) throws IOException { return openInput(file, Charset.defaultCharset(), compression); }
/** * Open a reader with automatic compression inference. * * @param file The file to open. * @return A reader opened on the input file. * @throws IOException if there is an error opening the file. */ @SuppressWarnings("unused") public static Writer openOutput(File file) throws IOException { return openOutput(file, Charset.defaultCharset(), CompressionMode.AUTO); }
/** * Create a file byte source, automatically decompressing based on file name. * @param file The file byte source. * @return The byte source, possibly decompressing. */ public static ByteSource byteSource(File file) { return byteSource(file, CompressionMode.AUTO); }
@Test public void testUncompressedFile() throws IOException { File file = tmpdir.newFile("uncompressed.txt"); Writer out = LKFileUtils.openOutput(file); try { out.write("hello\n"); } finally { out.close(); } Reader in = LKFileUtils.openInput(file, CompressionMode.NONE); try { char[] buffer = new char[6]; in.read(buffer); assertThat(new String(buffer), equalTo("hello\n")); } finally { out.close(); } }
/** * Load a recommender engine from an input stream. * <p> * <strong>Note:</strong> this method is only capable of auto-detecting gzip-compressed data. * If the {@linkplain #setCompressionMode(CompressionMode) compression mode} is {@link CompressionMode#AUTO}, * only gzip-compressed streams are supported. Set the compression mode manually if you are * using XZ compression. * </p> * * @param stream The input stream. * @return The deserialized recommender. * @throws IOException if there is an error reading the input data. * @throws RecommenderConfigurationException * if there is a configuration error with the deserialized recommender or * the configurations applied to it. */ public LenskitRecommenderEngine load(@WillClose InputStream stream) throws IOException, RecommenderConfigurationException { InputStream decomp; if (compressionMode == CompressionMode.AUTO) { decomp = LKFileUtils.transparentlyDecompress(stream); } else { decomp = compressionMode.wrapInput(stream); } return loadInternal(decomp); }
@Test public void testXZFile() throws IOException { File file = tmpdir.newFile("uncompressed.txt.xz"); Writer out = LKFileUtils.openOutput(file); try { out.write("hello\n"); } finally { out.close(); } Reader in = LKFileUtils.openInput(file, CompressionMode.XZ); try { char[] buffer = new char[6]; in.read(buffer); assertThat(new String(buffer), equalTo("hello\n")); } finally { out.close(); } }
LKFileUtils.transparentlyDecompress(stream), classLoader); try { ClassLoaderContext ctx = null;
@Test public void testGzipFile() throws IOException { File file = tmpdir.newFile("uncompressed.txt.gz"); Writer out = LKFileUtils.openOutput(file); try { out.write("hello\n"); } finally { out.close(); } Reader in = LKFileUtils.openInput(file, CompressionMode.GZIP); try { char[] buffer = new char[6]; in.read(buffer); assertThat(new String(buffer), equalTo("hello\n")); } finally { out.close(); } }
/** * Open a file for output with the default charset. * * @param file The file to open. * @param compression The compression mode. * @return A writer opened on the file. * @throws IOException if there was an error opening the file. * @see #openInput(java.io.File, Charset, CompressionMode) */ @SuppressWarnings("unused") public static Writer openOutput(File file, CompressionMode compression) throws IOException { return openOutput(file, Charset.defaultCharset(), compression); }
/** * Open a reader with automatic compression and the default character set. * * @param file The file to open. * @return A reader opened on the input file. * @throws IOException if there is an error opening the file. * @see #openInput(File, Charset, CompressionMode) * @see CompressionMode#AUTO * @see Charset#defaultCharset() */ @SuppressWarnings("unused") public static Reader openInput(File file) throws IOException { return openInput(file, Charset.defaultCharset(), CompressionMode.AUTO); }
/** * Set the URL of the input data. * @param url The URL of the input data. */ public void setURL(URL url) { sourceURL = url; source = LKFileUtils.byteSource(url, CompressionMode.AUTO) .asCharSource(Charsets.UTF_8); }
/** * Load a recommender engine from an input stream. * <p> * <strong>Note:</strong> this method is only capable of auto-detecting gzip-compressed data. * If the {@linkplain #setCompressionMode(CompressionMode) compression mode} is {@link CompressionMode#AUTO}, * only gzip-compressed streams are supported. Set the compression mode manually if you are * using XZ compression. * </p> * * @param stream The input stream. * @return The deserialized recommender. * @throws IOException if there is an error reading the input data. * @throws RecommenderConfigurationException * if there is a configuration error with the deserialized recommender or * the configurations applied to it. */ public LenskitRecommenderEngine load(@WillClose InputStream stream) throws IOException, RecommenderConfigurationException { InputStream decomp; if (compressionMode == CompressionMode.AUTO) { decomp = LKFileUtils.transparentlyDecompress(stream); } else { decomp = compressionMode.wrapInput(stream); } return loadInternal(decomp); }
/** * Open a CSV writer to write to a file. * * @param file The file to write to. * @param layout The layout of the table. * @param compression What compression, if any, to use. * @return A CSV writer outputting to {@code file}. * @throws IOException if there is an error opening the file or writing the column header. */ public static CSVWriter open(File file, TableLayout layout, CompressionMode compression) throws IOException { Files.createParentDirs(file); Writer writer = LKFileUtils.openOutput(file, Charset.defaultCharset(), compression); try { return new CSVWriter(writer, layout); } catch (Throwable th) { try { writer.close(); } catch (Throwable th2) { th.addSuppressed(th2); } Throwables.propagateIfInstanceOf(th, IOException.class); throw Throwables.propagate(th); } }
/** * Open a file for input with the default charset. * * @param file The file to open. * @param compression The compression mode. * @return A reader opened on the file. * @throws IOException if there was an error opening the file. * @see #openInput(java.io.File, Charset, CompressionMode) */ public static Reader openInput(File file, CompressionMode compression) throws IOException { return openInput(file, Charset.defaultCharset(), compression); }
/** * Set the source file for this reader. * @param file The source file. */ public void setFile(Path file) { source = LKFileUtils.byteSource(file.toFile(), CompressionMode.AUTO) .asCharSource(Charsets.UTF_8); try { sourceURL = file.toUri().toURL(); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid URL " + file, e); } }
LKFileUtils.transparentlyDecompress(stream), classLoader); try { ClassLoaderContext ctx = null;