PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/util/IOUtilities.java

#
Java | 153 lines | 71 code | 9 blank | 73 comment | 12 complexity | a2fed724d099c1b21f2394a5da8b70f8 MD5 | raw file
  1/*
  2 * IOTools.java - IO related functions
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2006 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.util;
 24
 25import java.io.OutputStream;
 26import java.io.IOException;
 27import java.io.Reader;
 28import java.io.InputStream;
 29
 30/**
 31 * IO tools that depends on JDK only.
 32 *
 33 * @author Matthieu Casanova
 34 * @version $Id: IOUtilities.java 5487 2006-06-23 22:58:12Z kpouer $
 35 * @since 4.3pre5
 36 */
 37public class IOUtilities
 38{
 39		//{{{ copyStream() method
 40	/**
 41	 * Copy an input stream to an output stream.
 42	 *
 43	 * @param bufferSize the size of the buffer
 44	 * @param progress the progress observer it could be null
 45	 * @param in the input stream
 46	 * @param out the output stream
 47	 * @param canStop if true, the copy can be stopped by interrupting the thread
 48	 * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
 49	 * @throws IOException  IOException If an I/O error occurs
 50	 */
 51	public static boolean copyStream(int bufferSize, ProgressObserver progress,
 52					InputStream in, OutputStream out, boolean canStop)
 53		throws IOException
 54	{
 55		byte[] buffer = new byte[bufferSize];
 56		int n;
 57		long copied = 0L;
 58		while (-1 != (n = in.read(buffer)))
 59		{
 60			out.write(buffer, 0, n);
 61			copied += n;
 62			if(progress != null)
 63				progress.setValue(copied);
 64			if(canStop && Thread.interrupted()) return false;
 65		}
 66		return true;
 67	} //}}}
 68
 69	//{{{ copyStream() method
 70	/**
 71	 * Copy an input stream to an output stream with a buffer of 4096 bytes.
 72	 *
 73	 * @param progress the progress observer it could be null
 74	 * @param in the input stream
 75	 * @param out the output stream
 76	 * @param canStop if true, the copy can be stopped by interrupting the thread
 77	 * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
 78	 * @throws IOException  IOException If an I/O error occurs
 79	 */
 80	public static boolean copyStream(ProgressObserver progress,
 81					 InputStream in, OutputStream out, boolean canStop)
 82		throws IOException
 83	{
 84		return copyStream(4096,progress, in, out, canStop);
 85	} //}}}
 86
 87	//{{{ closeQuietly() method
 88	/**
 89	 * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
 90	 *
 91	 * @param in the InputStream to close.
 92	 */
 93	public static void closeQuietly(InputStream in)
 94	{
 95		if(in != null)
 96		{
 97			try
 98			{
 99				in.close();
100			}
101			catch (IOException e)
102			{
103				//ignore
104			}
105		}
106	} //}}}
107
108	//{{{ copyStream() method
109	/**
110	 * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
111	 *
112	 * @param out the OutputStream to close.
113	 */
114	public static void closeQuietly(OutputStream out)
115	{
116		if(out != null)
117		{
118			try
119			{
120				out.close();
121			}
122			catch (IOException e)
123			{
124				//ignore
125			}
126		}
127	} //}}}
128
129	//{{{ copyStream() method
130	/**
131	 * Method that will close an {@link Reader} ignoring it if it is null and ignoring exceptions.
132	 *
133	 * @param r the Reader to close.
134	 * @since jEdit 4.3pre5
135	 */
136	public static void closeQuietly(Reader r)
137	{
138		if(r != null)
139		{
140			try
141			{
142				r.close();
143			}
144			catch (IOException e)
145			{
146				//ignore
147			}
148		}
149	} //}}}
150
151
152	private IOUtilities(){}
153}