/** * Auto-detect a compression mode from a file name. * @param name The file name. * @return The compression mode. Will never be {@link #AUTO}. */ public static CompressionMode autodetect(String name) { return AUTO.getEffectiveCompressionMode(name); }
/** * Auto-detect a compression mode from a file's name. * @param file The file. * @return The compression mode. Will never be {@link #AUTO}. */ public static CompressionMode autodetect(File file) { return AUTO.getEffectiveCompressionMode(file.getName() ); } }
/** * Open a file for input, optionally compressed. * * @param file The file to open. * @param charset The character set to use. * @param compression Whether to compress the file. * @return A reader opened on the file. * @throws IOException if there is an error opening the file. */ public static Reader openInput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); InputStream istream = new FileInputStream(file); try { InputStream wrapped = effComp.wrapInput(istream); return new InputStreamReader(wrapped, charset); } catch (Exception ex) { Closeables.close(istream, true); Throwables.propagateIfPossible(ex, IOException.class); throw Throwables.propagate(ex); } }
/** * Load a recommender from a file. * * @param file The recommender model file to load. * @return The recommender engine. * @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(File file) throws IOException, RecommenderConfigurationException { logger.info("Loading recommender engine from {}", file); try (FileInputStream input = new FileInputStream(file)) { CompressionMode effComp = compressionMode.getEffectiveCompressionMode(file.getName()); logger.info("using {} compression", effComp); return loadInternal(effComp.wrapInput(input)); } }
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file, CompressionMode comp) throws IOException { FileInputStream fin = new FileInputStream(file); InputStream rawin = comp.getEffectiveCompressionMode(file.getName()).wrapInput(fin); // REVIEW do we want to use the default charset? Reader reader = new InputStreamReader(rawin, Charset.defaultCharset()); BufferedReader buffer = new BufferedReader(reader); return new LineStream(buffer); }
/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @param compressed Whether to compress the output file. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file, CompressionMode compressed) throws IOException { try (OutputStream out = new FileOutputStream(file); OutputStream zout = compressed.getEffectiveCompressionMode(file.getName()).wrapOutput(out)) { write(zout); } }
/** * Open a file for input, optionally compressed. * * @param file The file to open. * @param charset The character set to use. * @param compression Whether to compress the file. * @return A reader opened on the file. * @throws IOException if there is an error opening the file. */ public static Writer openOutput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); OutputStream ostream = new FileOutputStream(file); try { OutputStream wrapped = effComp.wrapOutput(ostream); return new OutputStreamWriter(wrapped, charset); } catch (IOException ex) { try { ostream.close(); } catch (IOException ex2) { ex.addSuppressed(ex2); } throw ex; } }
/** * Create a URL-backed byte source. * @param url The URL of the byte source. * @param compression The compression mode. * @return The byte source, possibly decompressing. */ public static ByteSource byteSource(URL url, CompressionMode compression) { CompressionMode effectiveMode = compression.getEffectiveCompressionMode(url.getPath()); ByteSource source = Resources.asByteSource(url); if (!effectiveMode.equals(CompressionMode.NONE)) { source = new CompressedByteSource(source, effectiveMode.getCompressorName()); } return source; }
/** * Create a file byte source. * @param file The file containing data. * @param compression The compression mode. * @return The byte source, possibly decompressing. */ public static ByteSource byteSource(File file, CompressionMode compression) { CompressionMode effectiveMode = compression.getEffectiveCompressionMode(file.getName()); ByteSource source = Files.asByteSource(file); if (!effectiveMode.equals(CompressionMode.NONE)) { source = new CompressedByteSource(source, effectiveMode.getCompressorName()); } return source; }
@Test public void testNoneNone() { assertThat(CompressionMode.NONE.getEffectiveCompressionMode("foo.txt"), equalTo(CompressionMode.NONE)); assertThat(CompressionMode.NONE.getEffectiveCompressionMode("foo.gz"), equalTo(CompressionMode.NONE)); }
@Test public void testAutoXZ() { assertThat(CompressionMode.AUTO.getEffectiveCompressionMode("foo.xz"), equalTo(CompressionMode.XZ)); }
@Test public void testAutoGZ() { assertThat(CompressionMode.AUTO.getEffectiveCompressionMode("foo.gz"), equalTo(CompressionMode.GZIP)); }
@Test public void testAutoNone() { assertThat(CompressionMode.AUTO.getEffectiveCompressionMode("foo.txt"), equalTo(CompressionMode.NONE)); } }
/** * Auto-detect a compression mode from a file name. * @param name The file name. * @return The compression mode. Will never be {@link #AUTO}. */ public static CompressionMode autodetect(String name) { return AUTO.getEffectiveCompressionMode(name); }
/** * Auto-detect a compression mode from a file's name. * @param file The file. * @return The compression mode. Will never be {@link #AUTO}. */ public static CompressionMode autodetect(File file) { return AUTO.getEffectiveCompressionMode(file.getName() ); } }
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file, CompressionMode comp) throws IOException { FileInputStream fin = new FileInputStream(file); InputStream rawin = comp.getEffectiveCompressionMode(file.getName()).wrapInput(fin); // REVIEW do we want to use the default charset? Reader reader = new InputStreamReader(rawin, Charset.defaultCharset()); BufferedReader buffer = new BufferedReader(reader); return new LineStream(buffer); }
/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @param compressed Whether to compress the output file. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file, CompressionMode compressed) throws IOException { try (OutputStream out = new FileOutputStream(file); OutputStream zout = compressed.getEffectiveCompressionMode(file.getName()).wrapOutput(out)) { write(zout); } }
/** * Load a recommender from a file. * * @param file The recommender model file to load. * @return The recommender engine. * @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(File file) throws IOException, RecommenderConfigurationException { logger.info("Loading recommender engine from {}", file); try (FileInputStream input = new FileInputStream(file)) { CompressionMode effComp = compressionMode.getEffectiveCompressionMode(file.getName()); logger.info("using {} compression", effComp); return loadInternal(effComp.wrapInput(input)); } }
/** * Create a file byte source. * @param file The file containing data. * @param compression The compression mode. * @return The byte source, possibly decompressing. */ public static ByteSource byteSource(File file, CompressionMode compression) { CompressionMode effectiveMode = compression.getEffectiveCompressionMode(file.getName()); ByteSource source = Files.asByteSource(file); if (!effectiveMode.equals(CompressionMode.NONE)) { source = new CompressedByteSource(source, effectiveMode.getCompressorName()); } return source; }
/** * Create a URL-backed byte source. * @param url The URL of the byte source. * @param compression The compression mode. * @return The byte source, possibly decompressing. */ public static ByteSource byteSource(URL url, CompressionMode compression) { CompressionMode effectiveMode = compression.getEffectiveCompressionMode(url.getPath()); ByteSource source = Resources.asByteSource(url); if (!effectiveMode.equals(CompressionMode.NONE)) { source = new CompressedByteSource(source, effectiveMode.getCompressorName()); } return source; }