comparison src/org/prevayler/implementation/DelegatingByteCountStream.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /*
2 The copyright of all source code included in this Prevayler distribution is
3 held by Klaus Wuestefeld, except the files that specifically state otherwise.
4 All rights are reserved. "PREVAYLER" is a trademark of Klaus Wuestefeld.
5
6
7 BSD License:
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 - Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 - Neither the name of Prevayler nor the names of its contributors may be used
20 to endorse or promote products derived from this software without specific
21 prior written permission.
22
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 package org.prevayler.implementation;
38
39 import java.io.*;
40
41 /**
42 A FileOutputStream that counts the number of bytes written and forces all
43 buffers to synchronize with the underlying device when flushed.
44 */
45 final class DelegatingByteCountStream extends OutputStream {
46 private OutputStream theBuffer;
47 private FileOutputStream theFile;
48
49
50 DelegatingByteCountStream(File file, int aBufferSize)
51 throws IOException {
52
53 // System.err.println("Buffer size: " + aBufferSize);
54
55 System.err.println("Open: " + file);
56
57 theFile = new FileOutputStream(file);
58
59 if (aBufferSize > 0)
60 theBuffer = new BufferedOutputStream(theFile, aBufferSize);
61 else
62 theBuffer = null;
63 }
64
65 public void close() throws IOException {
66 // System.err.println("Close");
67
68 flush();
69 getTopStream().close();
70 }
71
72 public void flush() throws IOException {
73 // System.err.println("Flush");
74
75 /*
76 "The flush method of OutputStream does nothing." - JDK1.3 API
77 documentation. I'm calling it just in case it starts doing
78 something in a future version of FileOutputStream or
79 OutputStream.
80 */
81 getTopStream().flush();
82
83 /*
84 As we're JDK 1.4, this saves a bit of time because we don't
85 bother flushing meta data (we only care about flushing content)
86 */
87 theFile.getChannel().force(false);
88
89 // Original
90 // getFD().sync(); //"Force all system buffers to synchronize with the underlying device." - JDK1.3 API documentation.
91 }
92
93 private OutputStream getTopStream() {
94 if (theBuffer == null)
95 return theFile;
96 else
97 return theBuffer;
98 }
99
100 public void write(byte[] b) throws IOException {
101 getTopStream().write(b);
102 bytesWritten += b.length;
103 }
104
105 public void write(byte[] b, int off, int len) throws IOException {
106 getTopStream().write(b, off, len);
107 bytesWritten += len;
108 }
109
110 public void write(int b) throws IOException {
111 getTopStream().write(b);
112 ++bytesWritten;
113 }
114
115 public long bytesWritten() {
116 return bytesWritten;
117 }
118
119 private long bytesWritten;
120 }