KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > RollingFile


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo;
13
14 import java.io.IOException JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18
19 /**
20  * File that rolls over to backups at a configurable max size. Parts of this
21  * file have been cut and pasted from org.apache.log4j.RollingFileAppender.
22  * @keep-all
23  */

24 public class RollingFile {
25
26     private String JavaDoc filename;
27     private long maxSize = 1024 * 1024;
28     private int maxBackupIndex = 1;
29     private boolean append;
30     private CountingOutputStream out;
31     private boolean firstOpen = true;
32
33     public RollingFile(String JavaDoc filename) {
34         this.filename = filename;
35     }
36
37     public String JavaDoc getFilename() {
38         return filename;
39     }
40
41     public void setFilename(String JavaDoc filename) {
42         this.filename = filename;
43     }
44
45     public long getMaxSize() {
46         return maxSize;
47     }
48
49     public void setMaxSize(long maxSize) {
50         this.maxSize = maxSize;
51     }
52
53     public int getMaxBackupIndex() {
54         return maxBackupIndex;
55     }
56
57     public void setMaxBackupIndex(int maxBackupIndex) {
58         this.maxBackupIndex = maxBackupIndex;
59     }
60
61     public boolean isAppend() {
62         return append;
63     }
64
65     public void setAppend(boolean append) {
66         this.append = append;
67     }
68
69     /**
70      * Open our file. This will close any existing file.
71      */

72     public void open() throws IOException JavaDoc {
73         close();
74         if (firstOpen) {
75             firstOpen = false;
76             if (!append) { // nuke old backups
77
for (int i = 1; i <= maxBackupIndex; i++) {
78                     File JavaDoc file = new File JavaDoc(filename + "." + i);
79                     if (file.exists()) file.delete();
80                 }
81             }
82         }
83         out = new CountingOutputStream(new FileOutputStream JavaDoc(filename, append));
84         if (append) out.setCount(new File JavaDoc(filename).length());
85     }
86
87     /**
88      * Close our file.
89      */

90     public void close() throws IOException JavaDoc {
91         if (out != null) out.close();
92     }
93
94     /**
95      * Get the stream that writes to our file.
96      */

97     public OutputStream JavaDoc getOut() throws IOException JavaDoc {
98         if (out == null) open();
99         return out;
100     }
101
102     /**
103      * Is a rollover required? This should be called after writes to out. We
104      * cannot rollover automatically as out will typically be wrapped by
105      * other streams.
106      */

107     public boolean isRolloverRequired() {
108         return out.getCount() > maxSize;
109     }
110
111     /**
112      * Perform a rollover to a new file.
113      */

114     public void rollover() throws IOException JavaDoc {
115         File JavaDoc target, file;
116
117         // If maxBackups <= 0, then there is no file renaming to be done.
118
if (maxBackupIndex > 0) {
119             // Delete the oldest file, to keep Windows happy.
120
file = new File JavaDoc(filename + '.' + maxBackupIndex);
121             if (file.exists()) file.delete();
122
123             // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
124
for (int i = maxBackupIndex - 1; i >= 1; i--) {
125                 file = new File JavaDoc(filename + "." + i);
126                 if (file.exists()) {
127                     target = new File JavaDoc(filename + '.' + (i + 1));
128                     file.renameTo(target);
129                 }
130             }
131
132             // Rename filename to filename.1
133
target = new File JavaDoc(filename + "." + 1);
134
135             out.close();
136
137             file = new File JavaDoc(filename);
138             file.renameTo(target);
139         }
140         open();
141     }
142
143     /**
144      * OutputStream that counts bytes written to another stream.
145      */

146     public static class CountingOutputStream extends OutputStream JavaDoc {
147
148         private OutputStream JavaDoc out;
149         private long count;
150
151         public CountingOutputStream(OutputStream JavaDoc out) {
152             this.out = out;
153         }
154
155         /**
156          * Get number of bytes written so far.
157          */

158         public long getCount() {
159             return count;
160         }
161
162         public void setCount(long count) {
163             this.count = count;
164         }
165
166         public void write(int b) throws IOException JavaDoc {
167             ++count;
168             out.write(b);
169         }
170
171         public void write(byte b[]) throws IOException JavaDoc {
172             count += b.length;
173             out.write(b);
174         }
175
176         public void write(byte b[], int off, int len) throws IOException JavaDoc {
177             count += len;
178             out.write(b, off, len);
179         }
180
181         public void flush() throws IOException JavaDoc {
182             out.flush();
183         }
184
185         public void close() throws IOException JavaDoc {
186             out.close();
187         }
188     }
189
190 }
191
Popular Tags