trunk/com/so/news/io/Resource.java
author chris <chris@marvin>
Tue Jan 20 10:21:03 2009 +0100 (2009-01-20)
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
     1 /*
     2  *   StarOffice News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package com.so.news.io;
    20 
    21 import java.io.BufferedReader;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    27 import java.net.URL;
    28 import java.nio.charset.Charset;
    29 
    30 /**
    31  * Provides method for loading of resources.
    32  * @author Christian Lins
    33  */
    34 public class Resource
    35 {
    36   /**
    37    * Loads a file as array of byte. As the file is completely loaded into
    38    * memory this method should only be used with small files.
    39    * @param file
    40    * @return
    41    */
    42   public static byte[] getBytes(File file)
    43   {
    44     try
    45     {
    46       FileInputStream in = new FileInputStream(file);
    47       byte[] buffer = new byte[(int)file.length()];
    48       
    49       in.read(buffer);
    50       
    51       return buffer;
    52     }
    53     catch(IOException ex)
    54     {
    55       System.err.println(ex.getLocalizedMessage());
    56       return null;
    57     }
    58   }
    59   
    60   /**
    61    * Loads a resource and returns it as URL reference.
    62    * The Resource's classloader is used to load the resource, not
    63    * the System's ClassLoader so it may be safe to use this method
    64    * in a sandboxed environment.
    65    * @return
    66    */
    67   public static URL getAsURL(String name)
    68   {
    69     return Resource.class.getClassLoader().getResource(name);
    70   }
    71   
    72   /**
    73    * Loads a resource and returns an InputStream to it.
    74    * @param name
    75    * @return
    76    */
    77   public static InputStream getAsStream(String name)
    78   {
    79     try
    80     {
    81       URL url = getAsURL(name);
    82       return url.openStream();
    83     }
    84     catch(IOException e)
    85     {
    86       e.printStackTrace();
    87       return null;
    88     }
    89   }
    90 
    91   /**
    92    * Loads a plain text resource.
    93    * @param withNewline If false all newlines are removed from the 
    94    * return String
    95    */
    96   public static String getAsString(String name, boolean withNewline)
    97   {
    98     try
    99     {
   100       BufferedReader in  = new BufferedReader(
   101           new InputStreamReader(getAsStream(name), Charset.forName("UTF-8")));
   102       StringBuffer   buf = new StringBuffer();
   103 
   104       for(;;)
   105       {
   106         String line = in.readLine();
   107         if(line == null)
   108           break;
   109 
   110         buf.append(line);
   111         if(withNewline)
   112           buf.append('\n');
   113       }
   114 
   115       return buf.toString();
   116     }
   117     catch(Exception e)
   118     {
   119       e.printStackTrace();
   120       return null;
   121     }
   122   }
   123 }