List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:interactivespaces.service.template.internal.freemarker.FreemarkerTemplater.java
@Override public String instantiateTemplate(String templateName, Map<String, Object> data) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Writer out = new OutputStreamWriter(baos); try {/*from w ww . ja va 2 s .c o m*/ Template template = freemarkerConfig.getTemplate(templateName); template.process(data, out); out.close(); } catch (Exception e) { throw new InteractiveSpacesException(String.format("Could not instantiate template %s", templateName), e); } finally { Closeables.closeQuietly(out); } return new String(baos.toString()); }
From source file:org.apache.drill.exec.rpc.data.DataClientConnection.java
public void shutdownIfClient() { Closeables.closeQuietly(client); }
From source file:net.sourceforge.docfetcher.model.parse.AbiWordParser.java
/** * Returns a {@code Source} for the given AbiWord file. *//*ww w . j ava 2 s .c o m*/ @NotNull private static Source getSource(@NotNull InputStream in, @NotNull String filename) throws ParseException { try { String ext = Util.getExtension(filename); if (ext.equals("zabw") || ext.equals("abw.gz")) //$NON-NLS-1$ //$NON-NLS-2$ in = new GZIPInputStream(in); Source source = new Source(in); source.setLogger(null); source.fullSequentialParse(); return source; } catch (IOException e) { throw new ParseException(e); } finally { Closeables.closeQuietly(in); } }
From source file:org.apache.giraph.utils.FileUtils.java
/** * Write lines to a file./* w w w . j av a 2s.c o m*/ * * @param file File to write lines to * @param lines Strings written to the file * @throws IOException */ public static void writeLines(File file, String[] lines) throws IOException { Writer writer = Files.newWriter(file, Charsets.UTF_8); try { for (String line : lines) { writer.write(line); writer.write('\n'); } } finally { Closeables.closeQuietly(writer); } }
From source file:core.routing.ITSupport.java
/** * Does HTTP GET against given URL./*from ww w .ja v a2 s . c om*/ * * @param url * @return * @throws IOException */ protected HttpResponse executeGet(final String url) throws IOException { InputStream entityStream = null; try { final HttpClient httpClient = new DefaultHttpClient(); final HttpGet get = new HttpGet(url); final HttpResponse httpResponse = httpClient.execute(get); return httpResponse; } catch (IOException e) { Closeables.closeQuietly(entityStream); throw e; } }
From source file:com.metamx.druid.http.FileRequestLogger.java
@LifecycleStart public void start() { try {//from www .j a v a 2 s . co m baseDir.mkdirs(); MutableDateTime mutableDateTime = new DateTime().toMutableDateTime(); mutableDateTime.setMillisOfDay(0); currentDay = mutableDateTime.toDateTime(); fileWriter = new FileWriter(new File(baseDir, currentDay.toString("yyyy-MM-dd'.log'")), true); long nextDay = currentDay.plusDays(1).getMillis(); Duration delay = new Duration(nextDay - new DateTime().getMillis()); ScheduledExecutors.scheduleWithFixedDelay(exec, delay, Duration.standardDays(1), new Callable<ScheduledExecutors.Signal>() { @Override public ScheduledExecutors.Signal call() { currentDay = currentDay.plusDays(1); try { synchronized (lock) { Closeables.closeQuietly(fileWriter); fileWriter = new FileWriter(new File(baseDir, currentDay.toString()), true); } } catch (Exception e) { Throwables.propagate(e); } return ScheduledExecutors.Signal.REPEAT; } }); } catch (IOException e) { Throwables.propagate(e); } }
From source file:org.apache.drill.exec.rpc.data.DataConnectionCreator.java
public void close() { Closeables.closeQuietly(server); }
From source file:org.jclouds.savvis.vpdc.handlers.SymphonyVPDCErrorHandler.java
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams String message = parseMessage(response); Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); try {// w w w . j a v a2s. c o m message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: exception = new IllegalArgumentException(message, exception); break; case 401: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; case 405: exception = new IllegalArgumentException(message, exception); break; case 409: exception = new IllegalStateException(message, exception); break; } } finally { if (response.getPayload() != null) Closeables.closeQuietly(response.getPayload().getInput()); command.setException(exception); } }
From source file:org.jclouds.softlayer.handlers.SoftLayerErrorHandler.java
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams String message = parseMessage(response); Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); try {// w ww . j av a 2s. c o m message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; case 500: if (message != null) { if (message.indexOf("Unable to determine package for") != -1) { exception = new ResourceNotFoundException(message, exception); } else if (message.indexOf("currently an active transaction") != -1) { exception = new IllegalStateException(message, exception); } } } } finally { Closeables.closeQuietly(response.getPayload()); command.setException(exception); } }
From source file:com.android.contacts.util.ContactPhotoUtils.java
/** * Given a uri pointing to a bitmap, reads it into a bitmap and returns it. * @throws FileNotFoundException// www. j a va 2s .com */ public static Bitmap getBitmapFromUri(Context context, Uri uri) throws FileNotFoundException { final InputStream imageStream = context.getContentResolver().openInputStream(uri); try { return BitmapFactory.decodeStream(imageStream); } finally { Closeables.closeQuietly(imageStream); } }