List of usage examples for org.apache.commons.net.io Util closeQuietly
public static void closeQuietly(Socket socket)
From source file:net.longfalcon.newsj.nntp.client.ReplyIterator.java
/** * * @param _reader the reader to wrap//from w w w. j a v a 2 s. co m * @param addDotReader whether to additionally wrap the reader in a DotTerminatedMessageReader * @throws IOException */ ReplyIterator(BufferedReader _reader, boolean addDotReader) throws IOException { reader = addDotReader ? new DotTerminatedMessageReader(_reader) : _reader; line = reader.readLine(); // prime the iterator if (line == null) { Util.closeQuietly(reader); } }
From source file:net.longfalcon.newsj.nntp.client.ReplyIterator.java
public String next() throws NoSuchElementException { if (savedException != null) { throw new NoSuchElementException(savedException.toString()); }//w w w . j av a 2 s . co m String prev = line; if (prev == null) { throw new NoSuchElementException(); } try { line = reader.readLine(); // save next line if (line == null) { Util.closeQuietly(reader); } } catch (IOException ex) { savedException = ex; // if it fails, save the exception, as it does not apply to this call Util.closeQuietly(reader); } return prev; }
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * @since 3.1/*from w ww. j ava 2 s. c o m*/ */ protected boolean _storeFile(String command, String remote, InputStream local) throws IOException { Socket socket = _openDataConnection_(command, remote); if (socket == null) { return false; } OutputStream output = getBufferedOutputStream(socket.getOutputStream()); if (__fileType == ASCII_FILE_TYPE) { output = new ToNetASCIIOutputStream(output); } CSL csl = null; if (__controlKeepAliveTimeout > 0) { csl = new CSL(this, __controlKeepAliveTimeout, __controlKeepAliveReplyTimeout); } // Treat everything else as binary for now try { Util.copyStream(local, output, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, __mergeListeners(csl), false); } catch (IOException e) { Util.closeQuietly(socket); // ignore close errors here if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } throw e; } output.close(); // ensure the file is fully written socket.close(); // done writing the file if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } // Get the transfer response boolean ok = completePendingCommand(); return ok; }
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * @since 3.1//from ww w. j av a2s. c o m */ protected boolean _retrieveFile(String command, String remote, OutputStream local) throws IOException { Socket socket = _openDataConnection_(command, remote); if (socket == null) { return false; } InputStream input = getBufferedInputStream(socket.getInputStream()); if (__fileType == ASCII_FILE_TYPE) { input = new FromNetASCIIInputStream(input); } CSL csl = null; if (__controlKeepAliveTimeout > 0) { csl = new CSL(this, __controlKeepAliveTimeout, __controlKeepAliveReplyTimeout); } // Treat everything else as binary for now try { Util.copyStream(input, local, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, __mergeListeners(csl), false); } finally { Util.closeQuietly(input); Util.closeQuietly(socket); if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } } // Get the transfer response boolean ok = completePendingCommand(); return ok; }
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * private method through which all listFiles() and * initiateListParsing methods pass once a parser is determined. * * @exception FTPConnectionClosedException * If the FTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send FTP reply code 421. This exception may be caught either * as an IOException or independently as itself. * @exception IOException// w w w. j av a 2s . c o m * If an I/O error occurs while either sending a * command to the server or receiving a reply from the server. * @see FTPListParseEngine */ private FTPListParseEngine initiateListParsing(FTPFileEntryParser parser, String pathname) throws IOException { Socket socket = _openDataConnection_(FTPCmd.LIST, getListArguments(pathname)); FTPListParseEngine engine = new FTPListParseEngine(parser); if (socket == null) { return engine; } try { engine.readServerList(socket.getInputStream(), getControlEncoding()); } finally { Util.closeQuietly(socket); } completePendingCommand(); return engine; }
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * Initiate list parsing for MLSD listings. * * @param pathname/*from ww w. j ava 2 s .com*/ * @return the engine * @throws IOException */ private FTPListParseEngine initiateMListParsing(String pathname) throws IOException { Socket socket = _openDataConnection_(FTPCmd.MLSD, pathname); FTPListParseEngine engine = new FTPListParseEngine(MLSxEntryParser.getInstance()); if (socket == null) { return engine; } try { engine.readServerList(socket.getInputStream(), getControlEncoding()); } finally { Util.closeQuietly(socket); completePendingCommand(); } return engine; }