View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.common.io;
14  
15  import java.io.ByteArrayInputStream;
16  import java.io.ByteArrayOutputStream;
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  /**
25   * This is temporary storage. It stores first defined number of bytes in memory and if more are
26   * written then it creates a temporary file and continues to write to the file
27   *
28   * @author Daniel Sendula
29   */
30  public class TempStorage {
31  
32      /** Default maximum number of bytes to be stored in memory */
33      public static final int MAX_MEMORY = 102400;
34  
35      /** Number of bytes to be stored in memory */
36      protected int maxMemory = MAX_MEMORY;
37  
38      /** Temporary file */
39      protected File file = null;
40  
41      /** Default output stream */
42      protected OutputStream defaultOutputStream = new OutputStreamImpl();
43  
44      /** Output stream */
45      protected OutputStream os = null;
46  
47      /** Buffer array output stream */
48      protected ByteArrayOutputStream buffer = new ByteArrayOutputStream(MAX_MEMORY);
49  
50      /** Size */
51      protected int size = 0;
52  
53      /** Prefix */
54      protected String prefix = "tmp";
55  
56      /** Suffix */
57      protected String suffix = ".tmp";
58  
59      /**
60       * Constructor
61       */
62      public TempStorage() {
63      }
64  
65      /**
66       * Constructor
67       * @param prefix file prefix
68       * @param suffix file suffix
69       */
70      public TempStorage(String prefix, String suffix) {
71          this.prefix = prefix;
72          this.suffix = suffix;
73      }
74  
75      /**
76       * Constructor
77       * @param prefix prefix
78       * @param suffix suffix
79       * @param maxMemory maximum number of bytes
80       */
81      public TempStorage(String prefix, String suffix, int maxMemory) {
82          this(prefix, suffix);
83          this.maxMemory = maxMemory;
84      }
85  
86      /**
87       * Returns temporary file (or null)
88       * @return temporary file (or null)
89       */
90      public File getFile() {
91          return file;
92      }
93  
94      /**
95       * Returns size of storage
96       * @return size of storage
97       */
98      public int getSize() {
99          return size;
100     }
101 
102     /**
103      * Returns default output stream
104      * @return default output stream
105      */
106     public OutputStream getOutputStream() {
107         return defaultOutputStream;
108     }
109 
110     /**
111      * Returns input stream (file or memory buffer)
112      * @return input stream (file or memory buffer)
113      * @throws IOException
114      */
115     public InputStream getInputStream() throws IOException {
116         if (file != null) {
117             return new FileInputStream(file);
118         } else {
119             return  new ByteArrayInputStream(buffer.toByteArray());
120         }
121     }
122 
123     /**
124      * Clears storage for new use
125      * @throws IOException
126      */
127     public void clear() throws IOException {
128         if (file != null) {
129             file.delete();
130             os.close();
131         }
132         buffer = new ByteArrayOutputStream(MAX_MEMORY);
133         size = 0;
134     } // clear
135 
136     /**
137      * Output stream implementation
138      *
139      * @author Daniel Sendula
140      */
141     protected class OutputStreamImpl extends OutputStream {
142 
143         @Override
144         public void write(int b) throws IOException {
145             byte[] bs = new byte[1];
146             bs[0] = (byte)b;
147             write(bs);
148         }
149 
150         @Override
151         public void write(byte b[]) throws IOException {
152             write(b, 0, b.length);
153         }
154 
155         @Override
156         public void write(byte[] b, int off, int len) throws IOException {
157             if (file != null) {
158                 os.write(b, off, len);
159             } else {
160                 if (size + b.length > MAX_MEMORY) {
161                     file = File.createTempFile(prefix, suffix);
162                     file.deleteOnExit();
163                     os = new FileOutputStream(file);
164                     os.write(buffer.toByteArray());
165                     os.write(b, off, len);
166                     buffer = null;
167                 } else {
168                     buffer.write(b, off, len);
169                 }
170             }
171             size = size + len;
172         }
173     }
174 }