Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.io » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jorphan.io;
   20   
   21   import java.io.BufferedReader;
   22   import java.io.File;
   23   import java.io.FileInputStream;
   24   import java.io.FileOutputStream;
   25   import java.io.FileReader;
   26   import java.io.FileWriter;
   27   import java.io.IOException;
   28   import java.io.InputStreamReader;
   29   import java.io.OutputStreamWriter;
   30   import java.io.Reader;
   31   import java.io.Writer;
   32   
   33   import org.apache.jorphan.logging.LoggingManager;
   34   import org.apache.jorphan.util.JOrphanUtils;
   35   import org.apache.log.Logger;
   36   
   37   /**
   38    * Utility class to handle text files as a single lump of text.
   39    * <p>
   40    * Note this is just as memory-inefficient as handling a text file can be. Use
   41    * with restraint.
   42    *
   43    * @version $Revision: 674365 $
   44    */
   45   public class TextFile extends File {
   46       private static final Logger log = LoggingManager.getLoggerForClass();
   47   
   48       /**
   49        * File encoding. null means use the platform's default.
   50        */
   51       private String encoding = null;
   52   
   53       /**
   54        * Create a TextFile object to handle the named file with the given
   55        * encoding.
   56        *
   57        * @param filename
   58        *            File to be read & written through this object.
   59        * @param encoding
   60        *            Encoding to be used when reading & writing this file.
   61        */
   62       public TextFile(File filename, String encoding) {
   63           super(filename.toString());
   64           setEncoding(encoding);
   65       }
   66   
   67       /**
   68        * Create a TextFile object to handle the named file with the platform
   69        * default encoding.
   70        *
   71        * @param filename
   72        *            File to be read & written through this object.
   73        */
   74       public TextFile(File filename) {
   75           super(filename.toString());
   76       }
   77   
   78       /**
   79        * Create a TextFile object to handle the named file with the platform
   80        * default encoding.
   81        *
   82        * @param filename
   83        *            Name of the file to be read & written through this object.
   84        */
   85       public TextFile(String filename) {
   86           super(filename);
   87       }
   88   
   89       /**
   90        * Create a TextFile object to handle the named file with the given
   91        * encoding.
   92        *
   93        * @param filename
   94        *            Name of the file to be read & written through this object.
   95        * @param encoding
   96        *            Encoding to be used when reading & writing this file.
   97        */
   98       public TextFile(String filename, String encoding) {
   99           super(filename);
  100       }
  101   
  102       /**
  103        * Create the file with the given string as content -- or replace it's
  104        * content with the given string if the file already existed.
  105        *
  106        * @param body
  107        *            New content for the file.
  108        */
  109       public void setText(String body) {
  110           Writer writer = null;
  111           try {
  112               if (encoding == null) {
  113                   writer = new FileWriter(this);
  114               } else {
  115                   writer = new OutputStreamWriter(new FileOutputStream(this), encoding);
  116               }
  117               writer.write(body);
  118               writer.flush();
  119           } catch (IOException ioe) {
  120               log.error("", ioe);
  121           } finally {
  122               JOrphanUtils.closeQuietly(writer);
  123           }
  124       }
  125   
  126       /**
  127        * Read the whole file content and return it as a string.
  128        *
  129        * @return the content of the file
  130        */
  131       public String getText() {
  132           String lineEnd = System.getProperty("line.separator"); //$NON-NLS-1$
  133           StringBuffer sb = new StringBuffer();
  134           Reader reader = null;
  135           BufferedReader br = null;
  136           try {
  137               if (encoding == null) {
  138                   reader = new FileReader(this);
  139               } else {
  140                   reader = new InputStreamReader(new FileInputStream(this), encoding);
  141               }
  142               br = new BufferedReader(reader);
  143               String line = "NOTNULL"; //$NON-NLS-1$
  144               while (line != null) {
  145                   line = br.readLine();
  146                   if (line != null) {
  147                       sb.append(line + lineEnd);
  148                   }
  149               }
  150           } catch (IOException ioe) {
  151               log.error("", ioe); //$NON-NLS-1$
  152           } finally {
  153               JOrphanUtils.closeQuietly(br); // closes reader as well
  154           }
  155   
  156           return sb.toString();
  157       }
  158   
  159       /**
  160        * @return Encoding being used to read & write this file.
  161        */
  162       public String getEncoding() {
  163           return encoding;
  164       }
  165   
  166       /**
  167        * @param string
  168        *            Encoding to be used to read & write this file.
  169        */
  170       public void setEncoding(String string) {
  171           encoding = string;
  172       }
  173   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.io » [javadoc | source]