/** * 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); } }
/** * 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); }
/** * 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)); } }
/** * 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); }
InputStream input = new FileInputStream(modelFile); try { input = CompressionMode.autodetect(modelFile).wrapInput(input); engine = loader.load(input); } finally {
/** * 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); }
/** * 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)); } }
/** * 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); }