File Source: TextFile.java
/*
P/P * Method: com.dmdirc.util.TextFile__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.util;
24
25 import java.io.BufferedReader;
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.nio.charset.Charset;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * Allows reading and writing to a plain text file via a list of lines.
39 *
40 * @author chris
41 */
42 public class TextFile {
43
44 /** The file we're dealing with. */
45 private File file;
46
47 /** The input stream we're dealing with. */
48 private InputStream is;
49
50 /** The lines we've read from the file. */
51 private List<String> lines;
52
53 /** The charset to use to read the file. */
54 private final Charset charset;
55
56 /**
57 * Creates a new instance of TextFile for the specified file, and uses the
58 * default charset.
59 *
60 * @param filename The file to be read/written
61 */
62 public TextFile(final String filename) {
/*
P/P * Method: void com.dmdirc.util.TextFile(String)
*
* Postconditions:
* init'ed(this.charset)
* this.file == &new File(TextFile#1)
* new File(TextFile#1) num objects == 1
*/
63 this(new File(filename));
64 }
65
66 /**
67 * Creates a new instance of TextFile for the specified File, and uses the
68 * default charset.
69 *
70 * @param file The file to read
71 */
72 public TextFile(final File file) {
/*
P/P * Method: void com.dmdirc.util.TextFile(File)
*
* Postconditions:
* init'ed(this.charset)
* this.file == file
* init'ed(this.file)
*/
73 this(file, Charset.defaultCharset());
74 }
75
76 /**
77 * Creates a new instance of TextFile for an input stream, and uses the
78 * default charset.
79 *
80 * @param is The input stream to read from
81 */
82 public TextFile(final InputStream is) {
/*
P/P * Method: void com.dmdirc.util.TextFile(InputStream)
*
* Postconditions:
* init'ed(this.charset)
* this.is == is
* init'ed(this.is)
*/
83 this(is, Charset.defaultCharset());
84 }
85
86 /**
87 * Creates a new instance of TextFile for the specified File, which is to
88 * be read using the specified charset.
89 *
90 * @param file The file to read
91 * @param charset The charset to read the file in
92 * @since 0.6.3m1
93 */
/*
P/P * Method: void com.dmdirc.util.TextFile(File, Charset)
*
* Postconditions:
* this.charset == charset
* init'ed(this.charset)
* this.file == file
* init'ed(this.file)
*/
94 public TextFile(final File file, final Charset charset) {
95 this.file = file;
96 this.charset = charset;
97 }
98
99 /**
100 * Creates a new instance of TextFile for an input stream, which is to
101 * be read using the specified charset.
102 *
103 * @param is The input stream to read from
104 * @param charset The charset to read the file in
105 * @since 0.6.3m1
106 */
/*
P/P * Method: void com.dmdirc.util.TextFile(InputStream, Charset)
*
* Postconditions:
* this.charset == charset
* init'ed(this.charset)
* this.is == is
* init'ed(this.is)
*/
107 public TextFile(final InputStream is, final Charset charset) {
108 this.is = is;
109 this.charset = charset;
110 }
111
112 /**
113 * Retrieves the contents of the file as a list of lines. If getLines() or
114 * readLines() has previously been called, a cached version is returned.
115 *
116 * @return A list of lines in the file
117 * @throws IOException if an I/O exception occurs
118 */
119 public List<String> getLines() throws IOException {
/*
P/P * Method: List getLines()
*
* Preconditions:
* init'ed(this.lines)
* (soft) init'ed(this.file)
* (soft) init'ed(this.is)
*
* Postconditions:
* return_value == One-of{old this.lines, &new ArrayList(readLines#4)}
* return_value != null
* this.lines == return_value
* new ArrayList(readLines#4) num objects <= 1
*
* Test Vectors:
* this.lines: Inverse{null}, Addr_Set{null}
*/
120 if (lines == null) {
121 readLines();
122 }
123
124 return lines;
125 }
126
127 /**
128 * Reads the contents of the file into this TextFile's line cache.
129 *
130 * @throws IOException If an I/O exception occurs
131 */
132 public void readLines() throws IOException {
/*
P/P * Method: void readLines()
*
* Preconditions:
* init'ed(this.file)
* (soft) init'ed(this.is)
*
* Postconditions:
* this.lines == &new ArrayList(readLines#4)
* new ArrayList(readLines#4) num objects == 1
*
* Test Vectors:
* java.io.BufferedReader:readLine(...)@140: Addr_Set{null}, Inverse{null}
*/
133 final BufferedReader reader = new BufferedReader(
134 new InputStreamReader(file == null ? is : new FileInputStream(file),
135 charset));
136 lines = new ArrayList<String>();
137
138 String line;
139
140 while ((line = reader.readLine()) != null) {
141 lines.add(line);
142 }
143
144 reader.close();
145 }
146
147 /**
148 * Determines if this file is writable or not.
149 *
150 * @return True if the file is writable, false otherwise
151 */
152 public boolean isWritable() {
/*
P/P * Method: bool isWritable()
*
* Preconditions:
* init'ed(this.file)
*
* Postconditions:
* init'ed(return_value)
*/
153 return file != null;
154 }
155
156 /**
157 * Writes the specified list of lines to the file.
158 *
159 * @param lines The lines to be written
160 * @throws IOException if an I/O exception occurs
161 */
162 public void writeLines(final List<String> lines) throws IOException {
/*
P/P * Method: void writeLines(List)
*
* Preconditions:
* lines != null
* this.file != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@170: {0}, {1}
*/
163 if (file == null) {
164 throw new UnsupportedOperationException("Cannot write to TextFile "
165 + "opened with an InputStream");
166 }
167
168 final BufferedWriter writer = new BufferedWriter(new FileWriter(file));
169
170 for (String line : lines) {
171 writer.write(line);
172 writer.newLine();
173 }
174
175 writer.close();
176 }
177
178 /**
179 * Retrieves the File for this TextFile, if there is one.
180 *
181 * @return This TextFile's file, or null
182 */
183 public File getFile() {
/*
P/P * Method: File getFile()
*
* Preconditions:
* init'ed(this.file)
*
* Postconditions:
* return_value == this.file
* init'ed(return_value)
*/
184 return file;
185 }
186
187 /**
188 * Deletes the file associated with this textfile, if there is one.
189 */
190 public void delete() {
/*
P/P * Method: void delete()
*
* Preconditions:
* this.file != null
*/
191 if (file == null) {
192 throw new UnsupportedOperationException("Cannot delete TextFile "
193 + "opened with an InputStream");
194 }
195
196 file.delete();
197 }
198
199 }
SofCheck Inspector Build Version : 2.17854
| TextFile.java |
2009-Jun-25 01:54:24 |
| TextFile.class |
2009-Sep-02 17:04:11 |