You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by js...@apache.org on 2003/12/24 11:40:40 UTC

cvs commit: jakarta-jmeter/src/jorphan/org/apache/jorphan/io TextFile.java

jsalvata    2003/12/24 02:40:40

  Modified:    src/jorphan/org/apache/jorphan/io TextFile.java
  Log:
  Added capability to handle text files with explicit encodings
  (different from the platform's default).
  Also removed useless constructor.
  
  Revision  Changes    Path
  1.5       +103 -8    jakarta-jmeter/src/jorphan/org/apache/jorphan/io/TextFile.java
  
  Index: TextFile.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/io/TextFile.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TextFile.java	15 Sep 2003 23:11:54 -0000	1.4
  +++ TextFile.java	24 Dec 2003 10:40:40 -0000	1.5
  @@ -51,48 +51,111 @@
    * individuals on behalf of the Apache Software Foundation.  For more
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
  + * 
  + * @author Giles Cope (gilescope at users.sourceforge.net)
  + * @author Michael Stover (mstover1 at apache.org)
  + * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
  + * @version $Id$
    */
   package org.apache.jorphan.io;
   
   import java.io.BufferedReader;
   import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.IOException;
  +import java.io.InputStreamReader;
  +import java.io.OutputStreamWriter;
  +import java.io.Reader;
   import java.io.Writer;
   
   import org.apache.jorphan.logging.LoggingManager;
   import org.apache.log.Logger;
   
   /**
  - * @author Giles Cope (gilescope at users.sourceforge.net)
  - * @author Michael Stover (mstover1 at apache.org)
  - * @version $Revision$
  + * Utility class to handle text files as a single lump of text.
  + * <p>
  + * Note this is just as memory-inefficient as handling a text file can be. Use
  + * with restraint.
    */
   public class TextFile extends File
   {
       transient private static Logger log = LoggingManager.getLoggerForClass();
   
  +    /**
  +     * File encoding. null means use the platform's default.
  +     */
  +    private String encoding= null;
  +    
  +    /**
  +     * Create a TextFile object to handle the named file with the given encoding.
  +     * 
  +     * @param filename File to be read & written through this object.
  +     * @param encoding Encoding to be used when reading & writing this file.
  +     */
  +    public TextFile(File filename, String encoding)
  +    {
  +        super(filename.toString());
  +        setEncoding(encoding);
  +    }
  +    
  +    /**
  +     * Create a TextFile object to handle the named file with the platform
  +     * default encoding.
  +     * 
  +     * @param filename File to be read & written through this object.
  +     */
       public TextFile(File filename)
       {
           super(filename.toString());
       }
   
  -    public TextFile()
  +    /**
  +     * Create a TextFile object to handle the named file with the platform
  +     * default encoding.
  +     * 
  +     * @param filename Name of the file to be read & written through this object.
  +     */
  +    public TextFile(String filename)
       {
  -        super("");
  +        super(filename);
       }
   
  -    public TextFile(String filename)
  +    /**
  +     * Create a TextFile object to handle the named file with the given
  +     * encoding.
  +     * 
  +     * @param filename Name of the file to be read & written through this object.
  +     * @param encoding Encoding to be used when reading & writing this file.
  +     */
  +    public TextFile(String filename, String encoding)
       {
           super(filename);
       }
   
  +    /**
  +     * Create the file with the given string as content -- or replace it's
  +     * content with the given string if the file already existed.
  +     * 
  +     * @param body New content for the file.
  +     */
       public void setText(String body)
       {
           try
           {
  -            Writer writer = new FileWriter(this);
  +            Writer writer;
  +            if (encoding == null)
  +            {
  +                writer = new FileWriter(this);
  +            }
  +            else
  +            {
  +                writer = new OutputStreamWriter(
  +                    new FileOutputStream(this),
  +                    encoding);
  +            }
               writer.write(body);
               writer.flush();
               writer.close();
  @@ -103,13 +166,29 @@
           }
       }
   
  +    /**
  +     * Read the whole file content and return it as a string.
  +     *  
  +     * @return the content of the file
  +     */
       public String getText()
       {
           String lineEnd = System.getProperty("line.separator");
           StringBuffer sb = new StringBuffer();
           try
           {
  -            BufferedReader br = new BufferedReader(new FileReader(this));
  +            Reader reader;
  +            if (encoding == null)
  +            {
  +                reader= new FileReader(this);
  +            }
  +            else
  +            {
  +                reader= new InputStreamReader(
  +                    new FileInputStream(this),
  +                    encoding);
  +            }
  +            BufferedReader br = new BufferedReader(reader);
               String line = "NOTNULL";
               while (line != null)
               {
  @@ -125,5 +204,21 @@
               log.error("", ioe);
           }
           return sb.toString();
  +    }
  +
  +    /**
  +     * @return Encoding being used to read & write this file.
  +     */
  +    public String getEncoding()
  +    {
  +        return encoding;
  +    }
  +
  +    /**
  +     * @param string Encoding to be used to read & write this file.
  +     */
  +    public void setEncoding(String string)
  +    {
  +        encoding= string;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org