| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.BufferedInputStream; |
| 36 | | | import java.io.ByteArrayOutputStream; |
| 37 | | | import java.io.File; |
| 38 | | | import java.io.FileInputStream; |
| 39 | | | import java.io.FileNotFoundException; |
| 40 | | | import java.io.FileOutputStream; |
| 41 | | | import java.io.IOException; |
| 42 | | | import java.io.InputStream; |
| 43 | | | import java.io.OutputStream; |
| 44 | | | import java.util.logging.Logger; |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | <p> |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | |
| 56 | | | </p> |
| 57 | | | |
| 58 | | | <p></p> |
| 59 | | | |
| 60 | | | @author |
| 61 | | | |
| 62 | | | public class LazyFileOutputStream |
| 63 | | | extends OutputStream |
| 64 | | | { |
| 65 | 75 | | private static final String CLASSNAME |
| 66 | | | = LazyFileOutputStream.class.getName(); |
| 67 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 68 | | | private boolean mBuffering; |
| 69 | | | private OutputStream mOutput; |
| 70 | | | private File mFile; |
| 71 | | | private InputStream mInputStream; |
| 72 | | | |
| 73 | | | |
| 74 | | | @see |
| 75 | | (1)(2) | public LazyFileOutputStream (File file, boolean append) |
| 76 | | (3) | throws FileNotFoundException |
| 77 | 100 | | { |
| 78 | 100 | | mFile = file; |
| 79 | 100 | | if (append) |
| 80 | | | { |
| 81 | 0 | | mBuffering = false; |
| 82 | 0 | | mOutput = new FileOutputStream(file, append); |
| 83 | | | } |
| 84 | | | else |
| 85 | | | { |
| 86 | 100 | | if (file.exists() && file.canRead()) |
| 87 | | | { |
| 88 | 100 | | mInputStream = |
| 89 | | | new BufferedInputStream(new FileInputStream(file)); |
| 90 | 100 | | mOutput = new ByteArrayOutputStream(); |
| 91 | 100 | | mBuffering = true; |
| 92 | | | } |
| 93 | | | else |
| 94 | | | { |
| 95 | 0 | | mOutput = new FileOutputStream(file, append); |
| 96 | 0 | | mBuffering = false; |
| 97 | | | } |
| 98 | | | } |
| 99 | 100 | | } |
| 100 | | | |
| 101 | | | @see |
| 102 | | (4) | public LazyFileOutputStream (File file) |
| 103 | | (5) | throws FileNotFoundException |
| 104 | | | { |
| 105 | 100 | | this(file, false); |
| 106 | 100 | | } |
| 107 | | | |
| 108 | | | @see |
| 109 | | (6)(7) | public LazyFileOutputStream (String name, boolean append) |
| 110 | | (8) | throws FileNotFoundException |
| 111 | | | { |
| 112 | 0 | | this(new File(name), append); |
| 113 | 0 | | } |
| 114 | | | |
| 115 | | | @see |
| 116 | | (9) | public LazyFileOutputStream (String name) |
| 117 | | (10) | throws FileNotFoundException |
| 118 | | | { |
| 119 | 0 | | this(new File(name)); |
| 120 | 0 | | } |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | @return |
| 128 | | | |
| 129 | | | public boolean isBuffered () |
| 130 | | | { |
| 131 | 100 | | return mBuffering; |
| 132 | | | } |
| 133 | | | |
| 134 | | | {@inheritDoc} |
| 135 | | | public void write (int b) |
| 136 | | | throws IOException |
| 137 | | | { |
| 138 | 100 | | ensureOpen(); |
| 139 | 100 | | if (mBuffering && mInputStream.read() != b) |
| 140 | | | { |
| 141 | 100 | | stopBuffering(); |
| 142 | | | } |
| 143 | 100 | | mOutput.write(b); |
| 144 | 100 | | } |
| 145 | | | |
| 146 | | (11) | |
| 147 | | | |
| 148 | | | {@inheritDoc} |
| 149 | | | public void close () |
| 150 | | | throws IOException |
| 151 | | | { |
| 152 | 100 | | if (mBuffering && mInputStream.read() != -1) |
| 153 | | | { |
| 154 | 100 | | stopBuffering(); |
| 155 | | | } |
| 156 | 100 | | IoUtil.close(mInputStream); |
| 157 | 100 | | if (mOutput != null) |
| 158 | | | { |
| 159 | 100 | | mOutput.close(); |
| 160 | 100 | | mOutput = null; |
| 161 | 100 | | if (mBuffering) |
| 162 | | | { |
| 163 | 100 | | logger.fine("Avoided to touch " + mFile); |
| 164 | | | } |
| 165 | | | } |
| 166 | 100 | | } |
| 167 | | | |
| 168 | | | |
| 169 | | | |
| 170 | | | <code></code> |
| 171 | | | |
| 172 | | | |
| 173 | | | @exception |
| 174 | | | @see |
| 175 | | | |
| 176 | | | protected void finalize () |
| 177 | | | throws IOException, Throwable |
| 178 | | | { |
| 179 | 100 | | if (mOutput != null) |
| 180 | | | { |
| 181 | 0 | | close(); |
| 182 | | | } |
| 183 | 100 | | super.finalize(); |
| 184 | 100 | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | |
| 190 | | | |
| 191 | | | private void ensureOpen () |
| 192 | | | throws IOException |
| 193 | | | { |
| 194 | 100 | | if (mOutput == null) |
| 195 | | | { |
| 196 | 0 | | throw new IOException("Stream closed"); |
| 197 | | | } |
| 198 | 100 | | } |
| 199 | | | |
| 200 | | | private void stopBuffering () |
| 201 | | | throws IOException |
| 202 | | | { |
| 203 | 100 | | IoUtil.close(mInputStream); |
| 204 | 100 | | IoUtil.close(mOutput); |
| 205 | | | |
| 206 | 100 | | if (!(mOutput instanceof ByteArrayOutputStream)) |
| 207 | | | { |
| 208 | 0 | | throw new RuntimeException("Internal inconsistency"); |
| 209 | | | } |
| 210 | 100 | | final byte[] data |
| 211 | | | = ((ByteArrayOutputStream) mOutput).toByteArray(); |
| 212 | 100 | | mOutput = new FileOutputStream(mFile); |
| 213 | 100 | | mOutput.write(data); |
| 214 | 100 | | mBuffering = false; |
| 215 | 100 | | } |
| 216 | | | } |