it.tidalwave.mobile.io.IoUtils Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* $Id: IoUtils.java,v d491492f0834 2010/07/04 22:58:45 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.io;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import it.tidalwave.util.logging.Logger;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class IoUtils
{
private static final String CLASS = IoUtils.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void safeClose (final @Nullable InputStream is)
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void safeClose (final @Nullable OutputStream os)
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void safeClose (final @Nullable Reader r)
{
if (r != null)
{
try
{
r.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void safeClose (final @Nullable Writer w)
{
if (w != null)
{
try
{
w.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void copy (final @Nonnull InputStream is, final @Nonnull OutputStream os)
throws IOException
{
for (;;)
{
final int r = is.read();
if (r < 0)
{
break;
}
os.write(r);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void copy (final @Nonnull File srcFile, final @Nonnull File destFile)
throws IOException
{
logger.fine("copy(%s -> %s)", srcFile.getAbsolutePath(), destFile.getAbsolutePath());
InputStream is = null;
OutputStream os = null;
final File parentFile = destFile.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs())
{
throw new IOException("Cannot create path " + parentFile.getAbsolutePath());
}
try
{
is = new FileInputStream(srcFile);
os = new FileOutputStream(destFile);
copy(is, os);
}
finally
{
safeClose(is);
safeClose(os);
}
}
}