it.tidalwave.mobile.android.io.AndroidInternalFileSystem 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: AndroidInternalFileSystem.java,v 79a497f64ec9 2010/08/18 23:41:56 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.android.io;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.openide.util.Lookup;
import it.tidalwave.mobile.io.FileSystem;
import android.content.Context;
import android.os.StatFs;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidInternalFileSystem implements FileSystem
{
@CheckForNull
private StatFs statFs;
@Nonnull
private final Context context = Lookup.getDefault().lookup(Context.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public OutputStream openFileOutput (final @Nonnull String path)
throws IOException
{
return new FileOutputStream(getFile(path));
// return context.openFileOutput(name, Context.MODE_WORLD_READABLE);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public InputStream openFileInput (final @Nonnull String path)
throws IOException
{
return new FileInputStream(getFile(path));
// return context.openFileInput(name);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public File getFile (final @Nonnull String name)
throws IOException
{
final File f = new File(name);
final File dir = context.getDir(f.getParent(), Context.MODE_WORLD_READABLE);
return new File(dir, f.getName());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnegative
public synchronized long getFreeSpace()
{
if (statFs == null)
{
statFs = new StatFs("/");
}
return (long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public boolean isWritable()
{
return true;
}
}