PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.util;
  23. import java.io.OutputStream;
  24. import java.io.IOException;
  25. import java.io.Reader;
  26. import java.io.InputStream;
  27. /**
  28. * IO tools that depends on JDK only.
  29. *
  30. * @author Matthieu Casanova
  31. * @version $Id: IOUtilities.java 5487 2006-06-23 22:58:12Z kpouer $
  32. * @since 4.3pre5
  33. */
  34. public class IOUtilities
  35. {
  36. //{{{ copyStream() method
  37. /**
  38. * Copy an input stream to an output stream.
  39. *
  40. * @param bufferSize the size of the buffer
  41. * @param progress the progress observer it could be null
  42. * @param in the input stream
  43. * @param out the output stream
  44. * @param canStop if true, the copy can be stopped by interrupting the thread
  45. * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
  46. * @throws IOException IOException If an I/O error occurs
  47. */
  48. public static boolean copyStream(int bufferSize, ProgressObserver progress,
  49. InputStream in, OutputStream out, boolean canStop)
  50. throws IOException
  51. {
  52. byte[] buffer = new byte[bufferSize];
  53. int n;
  54. long copied = 0L;
  55. while (-1 != (n = in.read(buffer)))
  56. {
  57. out.write(buffer, 0, n);
  58. copied += n;
  59. if(progress != null)
  60. progress.setValue(copied);
  61. if(canStop && Thread.interrupted()) return false;
  62. }
  63. return true;
  64. } //}}}
  65. //{{{ copyStream() method
  66. /**
  67. * Copy an input stream to an output stream with a buffer of 4096 bytes.
  68. *
  69. * @param progress the progress observer it could be null
  70. * @param in the input stream
  71. * @param out the output stream
  72. * @param canStop if true, the copy can be stopped by interrupting the thread
  73. * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
  74. * @throws IOException IOException If an I/O error occurs
  75. */
  76. public static boolean copyStream(ProgressObserver progress,
  77. InputStream in, OutputStream out, boolean canStop)
  78. throws IOException
  79. {
  80. return copyStream(4096,progress, in, out, canStop);
  81. } //}}}
  82. //{{{ closeQuietly() method
  83. /**
  84. * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
  85. *
  86. * @param in the InputStream to close.
  87. */
  88. public static void closeQuietly(InputStream in)
  89. {
  90. if(in != null)
  91. {
  92. try
  93. {
  94. in.close();
  95. }
  96. catch (IOException e)
  97. {
  98. //ignore
  99. }
  100. }
  101. } //}}}
  102. //{{{ copyStream() method
  103. /**
  104. * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
  105. *
  106. * @param out the OutputStream to close.
  107. */
  108. public static void closeQuietly(OutputStream out)
  109. {
  110. if(out != null)
  111. {
  112. try
  113. {
  114. out.close();
  115. }
  116. catch (IOException e)
  117. {
  118. //ignore
  119. }
  120. }
  121. } //}}}
  122. //{{{ copyStream() method
  123. /**
  124. * Method that will close an {@link Reader} ignoring it if it is null and ignoring exceptions.
  125. *
  126. * @param r the Reader to close.
  127. * @since jEdit 4.3pre5
  128. */
  129. public static void closeQuietly(Reader r)
  130. {
  131. if(r != null)
  132. {
  133. try
  134. {
  135. r.close();
  136. }
  137. catch (IOException e)
  138. {
  139. //ignore
  140. }
  141. }
  142. } //}}}
  143. private IOUtilities(){}
  144. }