//# 0 errors, 146 messages
//#
/*
    //#TextFile.java:1:1: class: com.dmdirc.util.TextFile
    //#TextFile.java:1:1: method: com.dmdirc.util.TextFile.com.dmdirc.util.TextFile__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Allows reading and writing to a plain text file via a list of lines.
 * 
 * @author chris
 */
public class TextFile {
    
    /** The file we're dealing with. */
    private File file;
    
    /** The input stream we're dealing with. */
    private InputStream is;
    
    /** The lines we've read from the file. */
    private List<String> lines;

    /** The charset to use to read the file. */
    private final Charset charset;
    
    /**
     * Creates a new instance of TextFile for the specified file, and uses the
     * default charset.
     * 
     * @param filename The file to be read/written
     */
    public TextFile(final String filename) {
        this(new File(filename));
    //#TextFile.java:63: method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(String)
    //#input(void com.dmdirc.util.TextFile(String)): filename
    //#input(void com.dmdirc.util.TextFile(String)): this
    //#output(void com.dmdirc.util.TextFile(String)): new File(TextFile#1) num objects
    //#output(void com.dmdirc.util.TextFile(String)): this.charset
    //#output(void com.dmdirc.util.TextFile(String)): this.file
    //#new obj(void com.dmdirc.util.TextFile(String)): new File(TextFile#1)
    //#post(void com.dmdirc.util.TextFile(String)): init'ed(this.charset)
    //#post(void com.dmdirc.util.TextFile(String)): this.file == &new File(TextFile#1)
    //#post(void com.dmdirc.util.TextFile(String)): new File(TextFile#1) num objects == 1
    //#unanalyzed(void com.dmdirc.util.TextFile(String)): Effects-of-calling:java.nio.charset.Charset:defaultCharset
    }
    //#TextFile.java:64: end of method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(String)

    /**
     * Creates a new instance of TextFile for the specified File, and uses the
     * default charset.
     * 
     * @param file The file to read
     */
    public TextFile(final File file) {
        this(file, Charset.defaultCharset());
    //#TextFile.java:73: method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(File)
    //#input(void com.dmdirc.util.TextFile(File)): file
    //#input(void com.dmdirc.util.TextFile(File)): this
    //#output(void com.dmdirc.util.TextFile(File)): this.charset
    //#output(void com.dmdirc.util.TextFile(File)): this.file
    //#post(void com.dmdirc.util.TextFile(File)): init'ed(this.charset)
    //#post(void com.dmdirc.util.TextFile(File)): this.file == file
    //#post(void com.dmdirc.util.TextFile(File)): init'ed(this.file)
    }
    //#TextFile.java:74: end of method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(File)
    
    /**
     * Creates a new instance of TextFile for an input stream, and uses the
     * default charset.
     * 
     * @param is The input stream to read from
     */
    public TextFile(final InputStream is) {
        this(is, Charset.defaultCharset());
    //#TextFile.java:83: method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(InputStream)
    //#input(void com.dmdirc.util.TextFile(InputStream)): is
    //#input(void com.dmdirc.util.TextFile(InputStream)): this
    //#output(void com.dmdirc.util.TextFile(InputStream)): this.charset
    //#output(void com.dmdirc.util.TextFile(InputStream)): this.is
    //#post(void com.dmdirc.util.TextFile(InputStream)): init'ed(this.charset)
    //#post(void com.dmdirc.util.TextFile(InputStream)): this.is == is
    //#post(void com.dmdirc.util.TextFile(InputStream)): init'ed(this.is)
    }
    //#TextFile.java:84: end of method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(InputStream)

    /**
     * Creates a new instance of TextFile for the specified File, which is to
     * be read using the specified charset.
     *
     * @param file The file to read
     * @param charset The charset to read the file in
     * @since 0.6.3m1
     */
    public TextFile(final File file, final Charset charset) {
    //#TextFile.java:94: method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(File, Charset)
    //#input(void com.dmdirc.util.TextFile(File, Charset)): charset
    //#input(void com.dmdirc.util.TextFile(File, Charset)): file
    //#input(void com.dmdirc.util.TextFile(File, Charset)): this
    //#output(void com.dmdirc.util.TextFile(File, Charset)): this.charset
    //#output(void com.dmdirc.util.TextFile(File, Charset)): this.file
    //#post(void com.dmdirc.util.TextFile(File, Charset)): this.charset == charset
    //#post(void com.dmdirc.util.TextFile(File, Charset)): init'ed(this.charset)
    //#post(void com.dmdirc.util.TextFile(File, Charset)): this.file == file
    //#post(void com.dmdirc.util.TextFile(File, Charset)): init'ed(this.file)
        this.file = file;
        this.charset = charset;
    }
    //#TextFile.java:97: end of method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(File, Charset)

    /**
     * Creates a new instance of TextFile for an input stream, which is to
     * be read using the specified charset.
     *
     * @param is The input stream to read from
     * @param charset The charset to read the file in
     * @since 0.6.3m1
     */
    public TextFile(final InputStream is, final Charset charset) {
    //#TextFile.java:107: method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(InputStream, Charset)
    //#input(void com.dmdirc.util.TextFile(InputStream, Charset)): charset
    //#input(void com.dmdirc.util.TextFile(InputStream, Charset)): is
    //#input(void com.dmdirc.util.TextFile(InputStream, Charset)): this
    //#output(void com.dmdirc.util.TextFile(InputStream, Charset)): this.charset
    //#output(void com.dmdirc.util.TextFile(InputStream, Charset)): this.is
    //#post(void com.dmdirc.util.TextFile(InputStream, Charset)): this.charset == charset
    //#post(void com.dmdirc.util.TextFile(InputStream, Charset)): init'ed(this.charset)
    //#post(void com.dmdirc.util.TextFile(InputStream, Charset)): this.is == is
    //#post(void com.dmdirc.util.TextFile(InputStream, Charset)): init'ed(this.is)
        this.is = is;
        this.charset = charset;
    }
    //#TextFile.java:110: end of method: void com.dmdirc.util.TextFile.com.dmdirc.util.TextFile(InputStream, Charset)
    
    /**
     * Retrieves the contents of the file as a list of lines. If getLines() or
     * readLines() has previously been called, a cached version is returned.
     * 
     * @return A list of lines in the file
     * @throws IOException if an I/O exception occurs
     */
    public List<String> getLines() throws IOException {
        if (lines == null) {
    //#TextFile.java:120: method: List com.dmdirc.util.TextFile.getLines()
    //#TextFile.java:120: Warning: suspicious precondition
    //#    The precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.util.TextFile
    //#    method: List getLines()
    //#    suspicious precondition index: [3]
    //#    Attribs:  Soft
    //#input(List getLines()): __Descendant_Table[com/dmdirc/util/ConfigFile]
    //#input(List getLines()): __Descendant_Table[com/dmdirc/util/TextFile]
    //#input(List getLines()): __Descendant_Table[others]
    //#input(List getLines()): __Dispatch_Table.readLines()V
    //#input(List getLines()): com/dmdirc/util/ConfigFile.__Dispatch_Table.readLines()V
    //#input(List getLines()): this
    //#input(List getLines()): this.__Tag
    //#input(List getLines()): this.charset
    //#input(List getLines()): this.file
    //#input(List getLines()): this.is
    //#input(List getLines()): this.lines
    //#output(List getLines()): new ArrayList(readLines#4) num objects
    //#output(List getLines()): return_value
    //#output(List getLines()): this.lines
    //#new obj(List getLines()): new ArrayList(readLines#4)
    //#pre[1] (List getLines()): init'ed(this.lines)
    //#pre[3] (List getLines()): (soft) this.__Tag in {com/dmdirc/util/ConfigFile, com/dmdirc/util/TextFile}
    //#pre[5] (List getLines()): (soft) init'ed(this.file)
    //#pre[6] (List getLines()): (soft) init'ed(this.is)
    //#post(List getLines()): return_value == One-of{old this.lines, &new ArrayList(readLines#4)}
    //#post(List getLines()): return_value != null
    //#post(List getLines()): this.lines == return_value
    //#post(List getLines()): new ArrayList(readLines#4) num objects <= 1
    //#unanalyzed(List getLines()): Effects-of-calling:java.io.FileInputStream
    //#unanalyzed(List getLines()): Effects-of-calling:java.io.InputStreamReader
    //#unanalyzed(List getLines()): Effects-of-calling:java.io.BufferedReader
    //#unanalyzed(List getLines()): Effects-of-calling:java.util.ArrayList
    //#unanalyzed(List getLines()): Effects-of-calling:java.io.BufferedReader:readLine
    //#unanalyzed(List getLines()): Effects-of-calling:java.util.List:add
    //#unanalyzed(List getLines()): Effects-of-calling:java.io.BufferedReader:close
    //#test_vector(List getLines()): this.lines: Inverse{null}, Addr_Set{null}
            readLines();
        }
        
        return lines;
    //#TextFile.java:124: end of method: List com.dmdirc.util.TextFile.getLines()
    }
    
    /**
     * Reads the contents of the file into this TextFile's line cache.
     * 
     * @throws IOException If an I/O exception occurs
     */
    public void readLines() throws IOException {
        final BufferedReader reader = new BufferedReader(
    //#TextFile.java:133: method: void com.dmdirc.util.TextFile.readLines()
    //#input(void readLines()): this
    //#input(void readLines()): this.charset
    //#input(void readLines()): this.file
    //#input(void readLines()): this.is
    //#output(void readLines()): new ArrayList(readLines#4) num objects
    //#output(void readLines()): this.lines
    //#new obj(void readLines()): new ArrayList(readLines#4)
    //#pre[3] (void readLines()): init'ed(this.file)
    //#pre[4] (void readLines()): (soft) init'ed(this.is)
    //#post(void readLines()): this.lines == &new ArrayList(readLines#4)
    //#post(void readLines()): new ArrayList(readLines#4) num objects == 1
    //#test_vector(void readLines()): java.io.BufferedReader:readLine(...)@140: Addr_Set{null}, Inverse{null}
                new InputStreamReader(file == null ? is : new FileInputStream(file),
                charset));
        lines = new ArrayList<String>();
        
        String line;
        
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        
        reader.close();
    }
    //#TextFile.java:145: end of method: void com.dmdirc.util.TextFile.readLines()
    
    /**
     * Determines if this file is writable or not.
     * 
     * @return True if the file is writable, false otherwise
     */
    public boolean isWritable() {
        return file != null;
    //#TextFile.java:153: method: bool com.dmdirc.util.TextFile.isWritable()
    //#input(bool isWritable()): this
    //#input(bool isWritable()): this.file
    //#output(bool isWritable()): return_value
    //#pre[2] (bool isWritable()): init'ed(this.file)
    //#post(bool isWritable()): init'ed(return_value)
    //#TextFile.java:153: end of method: bool com.dmdirc.util.TextFile.isWritable()
    }
    
    /**
     * Writes the specified list of lines to the file.
     * 
     * @param lines The lines to be written
     * @throws IOException if an I/O exception occurs
     */
    public void writeLines(final List<String> lines) throws IOException {
        if (file == null) {
    //#TextFile.java:163: method: void com.dmdirc.util.TextFile.writeLines(List)
    //#input(void writeLines(List)): lines
    //#input(void writeLines(List)): this
    //#input(void writeLines(List)): this.file
    //#pre[1] (void writeLines(List)): lines != null
    //#pre[3] (void writeLines(List)): this.file != null
    //#test_vector(void writeLines(List)): java.util.Iterator:hasNext(...)@170: {0}, {1}
            throw new UnsupportedOperationException("Cannot write to TextFile "
                    + "opened with an InputStream");
        }
        
        final BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        
        for (String line : lines) {
            writer.write(line);
            writer.newLine();
        }
        
        writer.close();
    }
    //#TextFile.java:176: end of method: void com.dmdirc.util.TextFile.writeLines(List)

    /**
     * Retrieves the File for this TextFile, if there is one.
     * 
     * @return This TextFile's file, or null
     */
    public File getFile() {
        return file;
    //#TextFile.java:184: method: File com.dmdirc.util.TextFile.getFile()
    //#input(File getFile()): this
    //#input(File getFile()): this.file
    //#output(File getFile()): return_value
    //#pre[2] (File getFile()): init'ed(this.file)
    //#post(File getFile()): return_value == this.file
    //#post(File getFile()): init'ed(return_value)
    //#TextFile.java:184: end of method: File com.dmdirc.util.TextFile.getFile()
    }
    
    /**
     * Deletes the file associated with this textfile, if there is one.
     */
    public void delete() {
        if (file == null) {
    //#TextFile.java:191: method: void com.dmdirc.util.TextFile.delete()
    //#input(void delete()): this
    //#input(void delete()): this.file
    //#pre[2] (void delete()): this.file != null
            throw new UnsupportedOperationException("Cannot delete TextFile "
                    + "opened with an InputStream");
        }
        
        file.delete();
    }
    //#TextFile.java:197: end of method: void com.dmdirc.util.TextFile.delete()

}
    //#output(com.dmdirc.util.TextFile__static_init): __Descendant_Table[com/dmdirc/util/TextFile]
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.delete()V
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.getFile()Ljava/io/File;
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.getLines()Ljava/util/List;
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.isWritable()Z
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.readLines()V
    //#output(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.writeLines(Ljava/util/List;)V
    //#post(com.dmdirc.util.TextFile__static_init): __Descendant_Table[com/dmdirc/util/TextFile] == &__Dispatch_Table
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.delete()V == &delete
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.getFile()Ljava/io/File; == &getFile
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.getLines()Ljava/util/List; == &getLines
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.isWritable()Z == &isWritable
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.readLines()V == &readLines
    //#post(com.dmdirc.util.TextFile__static_init): __Dispatch_Table.writeLines(Ljava/util/List;)V == &writeLines
    //#TextFile.java:: end of method: com.dmdirc.util.TextFile.com.dmdirc.util.TextFile__static_init
    //#TextFile.java:: end of class: com.dmdirc.util.TextFile
