/** * @see #buildGZIPWriter(OutputStream) */ public static Writer buildGZIPWriter(File file) throws IOException { return buildGZIPWriter(new FileOutputStream(file, false)); }
@Override public void contextDestroyed(ServletContextEvent event) { log.info("Uninitializing Myrrix in servlet context..."); ClientThread theClientThread = clientThread; if (theClientThread != null) { try { theClientThread.close(); } catch (IOException e) { log.warn("Error while closing client thread", e); } } ServletContext context = event.getServletContext(); Closeable recommender = (Closeable) context.getAttribute(AbstractMyrrixServlet.RECOMMENDER_KEY); if (recommender != null) { try { recommender.close(); } catch (IOException e) { log.warn("Unexpected error while closing", e); } } IOUtils.deleteRecursively(tempDirToDelete); log.info("Myrrix is uninitialized"); }
private static void consumeIDs(URLConnection connection, FastIDSet result) throws IOException { BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { String line; while ((line = reader.readLine()) != null) { result.add(Long.parseLong(line)); } } finally { reader.close(); } }
Closer closer = Closer.create(); try { BufferedReader buffered = closer.register(IOUtils.buffer(reader)); Writer out = closer.register(IOUtils.buildGZIPWriter(tempFile)); CharSequence line; while ((line = buffered.readLine()) != null) {
@Override public void ingest(File file) throws TasteException { Reader reader = null; try { reader = IOUtils.openReaderMaybeDecompressing(file); ingest(reader); } catch (IOException ioe) { throw new TasteException(ioe); } finally { try { reader.close(); } catch (IOException e) { // Can't happen, continue } } }
@Override public void ingest(Reader reader) throws TasteException { BufferedReader buffered = IOUtils.buffer(reader); try {
/** * @see #buildGZIPOutputStream(OutputStream) */ public static GZIPOutputStream buildGZIPOutputStream(File file) throws IOException { return buildGZIPOutputStream(new FileOutputStream(file)); }
/** * @param url URL whose contents are to be read and copied * @param file file to write contents to * @throws IOException if the URL can't be read or the file can't be written */ public static void copyURLToFile(URL url, File file) throws IOException { InputStream in = url.openStream(); try { copyStreamToFile(in, file); } finally { in.close(); } }
Map<Integer,Pair<Writer,HttpURLConnection>> writersAndConnections = Maps.newHashMapWithExpectedSize(partitions.size()); BufferedReader buffered = IOUtils.buffer(reader); try { try { if (writerAndConnection == null) { HttpURLConnection connection = buildConnectionToAReplica(partition); Writer writer = IOUtils.buildGZIPWriter(connection.getOutputStream()); writerAndConnection = new Pair<Writer,HttpURLConnection>(writer, connection); writersAndConnections.put(partition, writerAndConnection);
@Override public void ingest(File file) throws TasteException { Reader reader = null; try { reader = IOUtils.openReaderMaybeDecompressing(file); ingest(reader); } catch (IOException ioe) { throw new TasteException(ioe); } finally { try { reader.close(); } catch (IOException e) { // Can't happen, continue } } }
/** * @param delegate {@link OutputStream} to wrap * @return the result of {@link #buildGZIPOutputStream(OutputStream)} as a {@link Writer} that encodes * using UTF-8 encoding */ public static Writer buildGZIPWriter(OutputStream delegate) throws IOException { return new OutputStreamWriter(buildGZIPOutputStream(delegate), Charsets.UTF_8); }
@Override public synchronized void close() { if (!closed) { closed = true; if (tomcat != null) { try { tomcat.stop(); tomcat.destroy(); } catch (LifecycleException le) { log.warn("Unexpected error while stopping", le); } if (!IOUtils.deleteRecursively(noSuchBaseDir)) { log.info("Could not delete {}", noSuchBaseDir); } } } }
Writer trainingOut = IOUtils.buildGZIPWriter(trainingFile); try {
@Override public void ingest(File file) throws TasteException { Reader reader = null; try { reader = IOUtils.openReaderMaybeDecompressing(file); ingest(reader); } catch (IOException ioe) { throw new TasteException(ioe); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // Can't happen, continue } } }
private static List<RecommendedItem> consumeItems(URLConnection connection) throws IOException { List<RecommendedItem> result = Lists.newArrayList(); BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { CharSequence line; while ((line = reader.readLine()) != null) { Iterator<String> tokens = COMMA.split(line).iterator(); long itemID = Long.parseLong(tokens.next()); float value = LangUtils.parseFloat(tokens.next()); result.add(new GenericRecommendedItem(itemID, value)); } } finally { reader.close(); } return result; }
/** * Serializes an object, with gzip compression, to a given file. */ public static <T extends Serializable> void writeObjectToFile(File f, T t) throws IOException { Preconditions.checkArgument(f.getName().endsWith(".gz"), "File should end in .gz: %s", f); ObjectOutputStream out = new ObjectOutputStream(buildGZIPOutputStream(f)); try { out.writeObject(t); } finally { out.close(); } }
@After public void tearDown() throws Exception { IOUtils.deleteRecursively(testTempDir); }
closeAppender(); appender = IOUtils.buildGZIPWriter(appendFile);
switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { return Integer.parseInt(reader.readLine());
recommender.close(); IOUtils.deleteRecursively(trainingDataDir);