01: /*
02: * MailboxFileWriter.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: MailboxFileWriter.java,v 1.1.1.1 2002/09/24 16:09:45 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j.mail;
23:
24: import java.io.BufferedWriter;
25: import java.io.FileOutputStream;
26: import java.io.IOException;
27: import java.io.OutputStreamWriter;
28: import java.io.Writer;
29: import org.armedbear.j.File;
30: import org.armedbear.j.Log;
31:
32: public final class MailboxFileWriter extends BufferedWriter {
33: private long offset;
34:
35: private MailboxFileWriter(Writer out) {
36: super (out);
37: }
38:
39: public static MailboxFileWriter getInstance(File file,
40: boolean append) {
41: try {
42: FileOutputStream outputStream = new FileOutputStream(file
43: .canonicalPath(), append);
44: MailboxFileWriter writer = new MailboxFileWriter(
45: new OutputStreamWriter(outputStream, "ISO-8859-1"));
46: if (append && file.isFile())
47: writer.offset = file.length();
48: return writer;
49: } catch (Exception e) {
50: Log.error(e);
51: return null;
52: }
53: }
54:
55: public final long getOffset() {
56: return offset;
57: }
58:
59: public void write(int c) throws IOException {
60: super .write(c);
61: ++offset;
62: }
63:
64: public void write(char[] cbuf, int off, int len) throws IOException {
65: super .write(cbuf, off, len);
66: offset += len;
67: }
68:
69: public void write(String s, int off, int len) throws IOException {
70: super .write(s, off, len);
71: offset += len;
72: }
73:
74: public void newLine() throws IOException {
75: super .write('\n'); // Always use '\n' as line terminator.
76: ++offset;
77: }
78:
79: public void flush() throws IOException {
80: super .flush();
81: }
82:
83: public void close() throws IOException {
84: super.close();
85: }
86: }
|