You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrivateIOUtils.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Sonar Standalone Runner
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.bootstrapper.utils;
  21. import java.io.Closeable;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import java.io.Reader;
  27. import java.io.StringWriter;
  28. import java.io.Writer;
  29. /**
  30. * Internal class used only by the Runner as we don't want it to depend on third-party libs.
  31. * This class should not be used by Sonar Runner consumers.
  32. */
  33. public final class PrivateIOUtils {
  34. private PrivateIOUtils() {
  35. // only static methods
  36. }
  37. /**
  38. * The default buffer size to use.
  39. */
  40. private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
  41. /**
  42. * Unconditionally close a <code>Closeable</code>.
  43. */
  44. public static void closeQuietly(Closeable closeable) {
  45. try {
  46. if (closeable != null) {
  47. closeable.close();
  48. }
  49. } catch (IOException ioe) {
  50. }
  51. }
  52. /**
  53. * Get the contents of a <code>Reader</code> as a String.
  54. */
  55. public static String toString(Reader input) throws IOException {
  56. StringWriter sw = new StringWriter();
  57. copyLarge(input, sw);
  58. return sw.toString();
  59. }
  60. /**
  61. * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
  62. */
  63. public static long copyLarge(InputStream input, OutputStream output) throws IOException {
  64. byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
  65. long count = 0;
  66. int n = 0;
  67. while (-1 != (n = input.read(buffer))) {
  68. output.write(buffer, 0, n);
  69. count += n;
  70. }
  71. return count;
  72. }
  73. /**
  74. * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
  75. */
  76. public static long copyLarge(Reader input, Writer output) throws IOException {
  77. char[] buffer = new char[DEFAULT_BUFFER_SIZE];
  78. long count = 0;
  79. int n = 0;
  80. while (-1 != (n = input.read(buffer))) {
  81. output.write(buffer, 0, n);
  82. count += n;
  83. }
  84. return count;
  85. }
  86. /**
  87. * Deletes a file (not a directory).
  88. */
  89. public static boolean deleteFileQuietly(File file) {
  90. if (file == null) {
  91. return false;
  92. }
  93. try {
  94. return file.delete();
  95. } catch (Exception e) {
  96. return false;
  97. }
  98. }
  99. }