1 2 12 package com.versant.core.jdo; 13 14 import java.io.IOException ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.OutputStream ; 18 19 24 public class RollingFile { 25 26 private String 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 filename) { 34 this.filename = filename; 35 } 36 37 public String getFilename() { 38 return filename; 39 } 40 41 public void setFilename(String 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 72 public void open() throws IOException { 73 close(); 74 if (firstOpen) { 75 firstOpen = false; 76 if (!append) { for (int i = 1; i <= maxBackupIndex; i++) { 78 File file = new File (filename + "." + i); 79 if (file.exists()) file.delete(); 80 } 81 } 82 } 83 out = new CountingOutputStream(new FileOutputStream (filename, append)); 84 if (append) out.setCount(new File (filename).length()); 85 } 86 87 90 public void close() throws IOException { 91 if (out != null) out.close(); 92 } 93 94 97 public OutputStream getOut() throws IOException { 98 if (out == null) open(); 99 return out; 100 } 101 102 107 public boolean isRolloverRequired() { 108 return out.getCount() > maxSize; 109 } 110 111 114 public void rollover() throws IOException { 115 File target, file; 116 117 if (maxBackupIndex > 0) { 119 file = new File (filename + '.' + maxBackupIndex); 121 if (file.exists()) file.delete(); 122 123 for (int i = maxBackupIndex - 1; i >= 1; i--) { 125 file = new File (filename + "." + i); 126 if (file.exists()) { 127 target = new File (filename + '.' + (i + 1)); 128 file.renameTo(target); 129 } 130 } 131 132 target = new File (filename + "." + 1); 134 135 out.close(); 136 137 file = new File (filename); 138 file.renameTo(target); 139 } 140 open(); 141 } 142 143 146 public static class CountingOutputStream extends OutputStream { 147 148 private OutputStream out; 149 private long count; 150 151 public CountingOutputStream(OutputStream out) { 152 this.out = out; 153 } 154 155 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 { 167 ++count; 168 out.write(b); 169 } 170 171 public void write(byte b[]) throws IOException { 172 count += b.length; 173 out.write(b); 174 } 175 176 public void write(byte b[], int off, int len) throws IOException { 177 count += len; 178 out.write(b, off, len); 179 } 180 181 public void flush() throws IOException { 182 out.flush(); 183 } 184 185 public void close() throws IOException { 186 out.close(); 187 } 188 } 189 190 } 191 | Popular Tags |