it.tidalwave.mobile.io.IoUtils Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Core - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://kenai.com/hg/bluebill~core-src
*
**********************************************************************************************************************/
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 it.tidalwave.util.logging.Logger;
import java.io.Closeable;
import java.util.zip.ZipFile;
/***********************************************************************************************************************
*
* @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 Closeable is)
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void safeClose (final @Nullable ZipFile zipFile)
{
if (zipFile != null)
{
try
{
zipFile.close();
}
catch (IOException e)
{
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void copy (final @Nonnull InputStream is, final @Nonnull OutputStream os)
throws IOException
{
final byte[] buffer = new byte[64 * 1024];
for (;;)
{
final int n = is.read(buffer);
if (n <= 0)
{
break;
}
os.write(buffer, 0, n);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
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);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy