001    /*
002     * Copyright (C) 2008 Google Inc.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.io;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.annotations.VisibleForTesting;
021    
022    import java.io.ByteArrayInputStream;
023    import java.io.ByteArrayOutputStream;
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    
031    /**
032     * An {@link OutputStream} that starts buffering to a byte array, but
033     * switches to file buffering once the data reaches a configurable size.
034     *
035     * <p>This class is thread-safe.
036     *
037     * @author Chris Nokleberg
038     * @since 1
039     */
040    @Beta
041    public final class FileBackedOutputStream extends OutputStream {
042    
043      private final int fileThreshold;
044      private final boolean resetOnFinalize;
045      private final InputSupplier<InputStream> supplier;
046    
047      private OutputStream out;
048      private MemoryOutput memory;
049      private File file;
050    
051      /** ByteArrayOutputStream that exposes its internals. */
052      private static class MemoryOutput extends ByteArrayOutputStream {
053        byte[] getBuffer() {
054          return buf;
055        }
056    
057        int getCount() {
058          return count;
059        }
060      }
061    
062      /** Returns the file holding the data (possibly null). */
063      @VisibleForTesting synchronized File getFile() {
064        return file;
065      }
066    
067      /**
068       * Creates a new instance that uses the given file threshold.
069       * Equivalent to {@code ThresholdOutputStream(fileThreshold, false)}.
070       *
071       * @param fileThreshold the number of bytes before the stream should
072       *     switch to buffering to a file
073       */
074      public FileBackedOutputStream(int fileThreshold) {
075        this(fileThreshold, false);
076      }
077    
078      /**
079       * Creates a new instance that uses the given file threshold, and
080       * optionally resets the data when the {@link InputSupplier} returned
081       * by {@link #getSupplier} is finalized.
082       *
083       * @param fileThreshold the number of bytes before the stream should
084       *     switch to buffering to a file
085       * @param resetOnFinalize if true, the {@link #reset} method will
086       *     be called when the {@link InputSupplier} returned by {@link
087       *     #getSupplier} is finalized
088       */
089      public FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize) {
090        this.fileThreshold = fileThreshold;
091        this.resetOnFinalize = resetOnFinalize;
092        memory = new MemoryOutput();
093        out = memory;
094    
095        if (resetOnFinalize) {
096          supplier = new InputSupplier<InputStream>() {
097            public InputStream getInput() throws IOException {
098              return openStream();
099            }
100    
101            @Override protected void finalize() {
102              try {
103                reset();
104              } catch (Throwable t) {
105                t.printStackTrace(System.err);
106              }
107            }
108          };
109        } else {
110          supplier = new InputSupplier<InputStream>() {
111            public InputStream getInput() throws IOException {
112              return openStream();
113            }
114          };
115        }
116      }
117    
118      /**
119       * Returns a supplier that may be used to retrieve the data buffered
120       * by this stream.
121       */
122      public InputSupplier<InputStream> getSupplier() {
123        return supplier;
124      }
125    
126      private synchronized InputStream openStream() throws IOException {
127        if (file != null) {
128          return new FileInputStream(file);
129        } else {
130          return new ByteArrayInputStream(
131              memory.getBuffer(), 0, memory.getCount());
132        }
133      }
134    
135      /**
136       * Calls {@link #close} if not already closed, and then resets this
137       * object back to its initial state, for reuse. If data was buffered
138       * to a file, it will be deleted.
139       *
140       * @throws IOException if an I/O error occurred while deleting the file buffer
141       */
142      public synchronized void reset() throws IOException {
143        try {
144          close();
145        } finally {
146          if (memory == null) {
147            memory = new MemoryOutput();
148          } else {
149            memory.reset();
150          }
151          out = memory;
152          if (file != null) {
153            File deleteMe = file;
154            file = null;
155            if (!deleteMe.delete()) {
156              throw new IOException("Could not delete: " + deleteMe);
157            }
158          }
159        }
160      }
161    
162      @Override public synchronized void write(int b) throws IOException {
163        update(1);
164        out.write(b);
165      }
166    
167      @Override public synchronized void write(byte[] b) throws IOException {
168        write(b, 0, b.length);
169      }
170    
171      @Override public synchronized void write(byte[] b, int off, int len)
172          throws IOException {
173        update(len);
174        out.write(b, off, len);
175      }
176    
177      @Override public synchronized void close() throws IOException {
178        out.close();
179      }
180    
181      @Override public synchronized void flush() throws IOException {
182        out.flush();
183      }
184    
185      /**
186       * Checks if writing {@code len} bytes would go over threshold, and
187       * switches to file buffering if so.
188       */
189      private void update(int len) throws IOException {
190        if (file == null && (memory.getCount() + len > fileThreshold)) {
191          File temp = File.createTempFile("FileBackedOutputStream", null);
192          if (resetOnFinalize) {
193            // Finalizers are not guaranteed to be called on system shutdown;
194            // this is insurance.
195            temp.deleteOnExit();
196          }
197          FileOutputStream transfer = new FileOutputStream(temp);
198          transfer.write(memory.getBuffer(), 0, memory.getCount());
199          transfer.flush();
200    
201          // We've successfully transferred the data; switch to writing to file
202          out = transfer;
203          file = temp;
204          memory = null;
205        }
206      }
207    }