List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:jp.sourceforge.reflex.core.ResourceMapper.java
protected String getBody(Reader reader) throws IOException { try {/* w w w.j a v a2s . c o m*/ BufferedReader b = new BufferedReader(reader); StringBuffer sb = new StringBuffer(); String str; while ((str = b.readLine()) != null) { sb.append(str); } return sb.toString(); } finally { reader.close(); } }
From source file:lcn.module.oltp.web.common.base.handler.AltibaseClobStringTypeHandler.java
protected Object getResultInternal(ResultSet rs, int index, LobHandler lobHandler) throws SQLException { StringBuffer read_data = new StringBuffer(""); int read_length; char[] buf = new char[1024]; Reader rd = lobHandler.getClobAsCharacterStream(rs, index); try {//from w w w. j ava 2 s .c o m while ((read_length = rd.read(buf)) != -1) { read_data.append(buf, 0, read_length); } } catch (IOException ie) { SQLException sqle = new SQLException(ie.getMessage()); throw sqle; // 2011.10.10 ? ? } finally { if (rd != null) { try { rd.close(); } catch (Exception ignore) { LOG.debug("IGNORE: " + ignore.getMessage()); } } } return read_data.toString(); //return lobHandler.getClobAsString(rs, index); }
From source file:org.callimachusproject.auth.DigestPasswordAccessor.java
private String readString(FileObject file) { try {/* w w w . java2s .com*/ Reader reader = file.openReader(true); if (reader == null) return null; try { return new Scanner(reader).next(); } finally { reader.close(); } } catch (IOException | NoSuchElementException e) { logger.error(file.toUri().toASCIIString(), e); return null; } }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
/** * Processes the body (text) part of an email. * * @param content the text content of the email. * @param contentType the content type for this text. * @param message the message corresponding to this part * @throws IOException/*from w ww .ja va 2s .c o m*/ * @throws MessagingException */ public void processBody(String content, String contentType, Message message) throws IOException, MessagingException { if (message.getContentType() != null && message.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) { // this is the text-part of an HTMLmultipart message return; } message.setContentType(contentType); if (contentType == null) { message.setContentType(MimeTypes.PLAIN_TEXT_MIME_TYPE); } if (message.getContentType().indexOf(MimeTypes.PLAIN_TEXT_MIME_TYPE) >= 0) { message.setBody(content); if (message.getBody().length() > SUMMARY_SIZE) { message.setSummary(message.getBody().substring(0, SUMMARY_SIZE)); } else { message.setSummary(message.getBody()); } } else if (message.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) { message.setBody(content); Reader reader = null; try { reader = new StringReader(content); cleaner.parse(reader); message.setSummary(cleaner.getSummary()); } finally { if (reader != null) { reader.close(); } } } else { // Managing as text/plain message.setContentType(MimeTypes.PLAIN_TEXT_MIME_TYPE); message.setBody(content); if (message.getBody().length() > SUMMARY_SIZE) { message.setSummary(message.getBody().substring(0, SUMMARY_SIZE)); } else { message.setSummary(message.getBody()); } } }
From source file:co.cask.cdap.gateway.handlers.metrics.MetricsQueryTest.java
@Test public void testingInvalidSystemMetrics() throws Exception { //appfabrics service does not exist String methodRequest = "/system/services/appfabrics/handlers/AppFabricHttpHandler/methods/getAllApps/" + "request.received?aggregate=true"; HttpResponse response = doGet("/v2/metrics" + methodRequest); Reader reader = new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8); try {//from ww w . j a va 2 s . c om Assert.assertEquals("GET " + methodRequest + " did not return 404 NOT-FOUND status.", HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode()); } finally { reader.close(); } }
From source file:org.lanark.jsr303js.taglib.JSR303JSCodebaseTag.java
/** * Copies the chars from in to out and then closes in but * leaves out open./*www.ja v a 2 s .co m*/ * * @param in the input reader * @param out the output writer * @throws IOException if there is an io exception */ private void copy(Reader in, Writer out) throws IOException { Assert.notNull(in, "No Reader specified"); Assert.notNull(out, "No Writer specified"); try { char[] buffer = new char[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } finally { try { in.close(); } catch (IOException ex) { logger.warn("Could not close Reader", ex); } } }
From source file:org.springmodules.validation.valang.javascript.taglib.ValangCodebaseTag.java
/** * Copies the chars from in to out and then closes in but leaves out open. *///from w w w . jav a2 s .co m private void copy(Reader in, Writer out) throws IOException { Assert.notNull(in, "No Reader specified"); Assert.notNull(out, "No Writer specified"); try { char[] buffer = new char[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } finally { try { in.close(); } catch (IOException ex) { logger.warn("Could not close Reader", ex); } } }
From source file:kr.co.skysoft.framework.taglib.JSR303JSCodebaseTag.java
/** * Copies the chars from in to out and then closes in but * leaves out open./*from w ww. j a v a 2s . c o m*/ * * @param in the input reader * @param out the output writer * @throws IOException if there is an io exception */ private void copy(Reader in, Writer out) throws IOException { Assert.notNull(in, "No Reader specified"); Assert.notNull(out, "No Writer specified"); try { char[] buffer = new char[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } finally { try { in.close(); } catch (IOException ex) { logger.warn("Could not close Reader", ex); } } }
From source file:com.urbancode.ud.client.UDRestClient.java
/** * @param response The response created by an HTTPClient call. * * @return String//from w ww .j a v a 2 s. c om */ protected String getBody(HttpResponse response) throws IOException { String result = null; StringBuilder builder = new StringBuilder(); if (response.getStatusLine().getStatusCode() != 204) { InputStream body = response.getEntity().getContent(); if (body != null) { Reader reader = IO.reader(body, IO.utf8()); try { IO.copy(reader, builder); } finally { reader.close(); } } result = builder.toString(); } // Else if the response was "No Content" we should pass back null return result; }
From source file:com.github.jknack.handlebars.io.URLTemplateSource.java
@Override public String content() throws IOException { Reader reader = null; final int bufferSize = 1024; try {/*from w w w. j a v a 2 s .c o m*/ reader = reader(); char[] cbuf = new char[bufferSize]; StringBuilder sb = new StringBuilder(bufferSize); int len; while ((len = reader.read(cbuf, 0, bufferSize)) != -1) { sb.append(cbuf, 0, len); } return sb.toString(); } finally { if (reader != null) { reader.close(); } } }