Subversion Repositories AndroidProjects

Rev

Rev 192 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.file;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;

/** File utilities and convenience functions. */
public class FileUtil
{
        private FileUtil() {}
       
        /** Load a file into a string from assets. */
        public static String loadStringFromAsset(Resources resources, String asset) throws IOException
        {
                InputStream is = resources.getAssets().open(asset);
               
                return loadString(is);
        }
       
        /** Load a file into a string from resources. */
        public static String loadStringFromResource(Resources resources, int id) throws IOException
        {
                InputStream is = resources.openRawResource(id);
               
                return loadString(is);
        }
       
        public static String loadString(InputStream is) throws IOException
        {
                byte[] buffer = new byte[is.available()];
                is.read(buffer);

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                os.write(buffer);
                os.close();
                       
                is.close();
                       
                return os.toString();
        }
       
        /** Load a file from assets. */
        public static File loadFileFromAsset(Resources resources, String asset) throws IOException
        {
                InputStream is = resources.getAssets().open(asset);
                return new File(is);
        }
       
        /** Load a file from resources. */
        public static File loadFileFromResource(Resources resources, int id) throws IOException
        {
                InputStream is = resources.openRawResource(id);
                return new File(is);
        }
}