001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch.bootstrapper; 021 022 import java.io.*; 023 024 public final class BootstrapperIOUtils { 025 026 private BootstrapperIOUtils() { 027 // only static methods 028 } 029 030 /** 031 * The default buffer size to use. 032 */ 033 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 034 035 /** 036 * Unconditionally close a <code>Closeable</code>. 037 */ 038 public static void closeQuietly(Closeable closeable) { 039 try { 040 if (closeable != null) { 041 closeable.close(); 042 } 043 } catch (IOException ioe) { // NOSONAR 044 } 045 } 046 047 /** 048 * Get the contents of a <code>Reader</code> as a String. 049 */ 050 public static String toString(Reader input) throws IOException { 051 StringWriter sw = new StringWriter(); 052 copyLarge(input, sw); 053 return sw.toString(); 054 } 055 056 /** 057 * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>. 058 */ 059 public static long copyLarge(InputStream input, OutputStream output) throws IOException { 060 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 061 long count = 0; 062 int n = 0; 063 while (-1 != (n = input.read(buffer))) { 064 output.write(buffer, 0, n); 065 count += n; 066 } 067 return count; 068 } 069 070 /** 071 * Copy chars from a <code>Reader</code> to a <code>Writer</code>. 072 */ 073 public static long copyLarge(Reader input, Writer output) throws IOException { 074 char[] buffer = new char[DEFAULT_BUFFER_SIZE]; 075 long count = 0; 076 int n = 0; 077 while (-1 != (n = input.read(buffer))) { 078 output.write(buffer, 0, n); 079 count += n; 080 } 081 return count; 082 } 083 084 /** 085 * Deletes a file (not a directory). 086 */ 087 public static boolean deleteFileQuietly(File file) { 088 if (file == null) { 089 return false; 090 } 091 try { 092 return file.delete(); 093 } catch (Exception e) { 094 return false; 095 } 096 } 097 098 }