KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > MailboxFileWriter


1 /*
2  * MailboxFileWriter.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: MailboxFileWriter.java,v 1.1.1.1 2002/09/24 16:09:45 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import org.armedbear.j.File;
30 import org.armedbear.j.Log;
31
32 public final class MailboxFileWriter extends BufferedWriter JavaDoc
33 {
34     private long offset;
35
36     private MailboxFileWriter(Writer JavaDoc out)
37     {
38         super(out);
39     }
40
41     public static MailboxFileWriter getInstance(File file, boolean append)
42     {
43         try {
44             FileOutputStream JavaDoc outputStream =
45                 new FileOutputStream JavaDoc(file.canonicalPath(), append);
46             MailboxFileWriter writer = new MailboxFileWriter(
47                 new OutputStreamWriter JavaDoc(outputStream, "ISO-8859-1"));
48             if (append && file.isFile())
49                 writer.offset = file.length();
50             return writer;
51         }
52         catch (Exception JavaDoc e) {
53             Log.error(e);
54             return null;
55         }
56     }
57
58     public final long getOffset()
59     {
60         return offset;
61     }
62
63     public void write(int c) throws IOException JavaDoc
64     {
65         super.write(c);
66         ++offset;
67     }
68
69     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc
70     {
71         super.write(cbuf, off, len);
72         offset += len;
73     }
74
75     public void write(String JavaDoc s, int off, int len) throws IOException JavaDoc
76     {
77         super.write(s, off, len);
78         offset += len;
79     }
80
81     public void newLine() throws IOException JavaDoc
82     {
83         super.write('\n'); // Always use '\n' as line terminator.
84
++offset;
85     }
86
87     public void flush() throws IOException JavaDoc
88     {
89         super.flush();
90     }
91
92     public void close() throws IOException JavaDoc
93     {
94         super.close();
95     }
96 }
97
Popular Tags