1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.util;
21
22import java.io.BufferedInputStream;
23import java.io.BufferedOutputStream;
24import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.util.Random;
31
32//BEGIN android-changed: Stubbing out logging
33import org.apache.james.mime4j.Log;
34import org.apache.james.mime4j.LogFactory;
35//END android-changed
36
37/**
38 *
39 * @version $Id: SimpleTempStorage.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
40 */
41public class SimpleTempStorage extends TempStorage {
42    private static Log log = LogFactory.getLog(SimpleTempStorage.class);
43
44    private TempPath rootPath = null;
45    private Random random = new Random();
46
47    /**
48     * Creates a new <code>SimpleTempStorageManager</code> instance.
49     */
50    public SimpleTempStorage() {
51        rootPath = new SimpleTempPath(System.getProperty("java.io.tmpdir"));
52    }
53
54    private TempPath createTempPath(TempPath parent, String prefix)
55            throws IOException {
56
57        if (prefix == null) {
58            prefix = "";
59        }
60
61        File p = null;
62        int count = 1000;
63        do {
64            long n = Math.abs(random.nextLong());
65            p = new File(parent.getAbsolutePath(), prefix + n);
66            count--;
67        } while (p.exists() && count > 0);
68
69        if (p.exists() || !p.mkdirs()) {
70            log.error("Unable to mkdirs on " + p.getAbsolutePath());
71            throw new IOException("Creating dir '"
72                                    + p.getAbsolutePath() + "' failed.");
73        }
74
75        return new SimpleTempPath(p);
76    }
77
78    private TempFile createTempFile(TempPath parent, String prefix,
79                                    String suffix) throws IOException {
80
81        if (prefix == null) {
82            prefix = "";
83        }
84        if (suffix == null) {
85            suffix = ".tmp";
86        }
87
88        File f = null;
89
90        int count = 1000;
91        synchronized (this) {
92            do {
93                long n = Math.abs(random.nextLong());
94                f = new File(parent.getAbsolutePath(), prefix + n + suffix);
95                count--;
96            } while (f.exists() && count > 0);
97
98            if (f.exists()) {
99                throw new IOException("Creating temp file failed: "
100                                         + "Unable to find unique file name");
101            }
102
103            try {
104                f.createNewFile();
105            } catch (IOException e) {
106                throw new IOException("Creating dir '"
107                                        + f.getAbsolutePath() + "' failed.");
108            }
109        }
110
111        return new SimpleTempFile(f);
112    }
113
114    /**
115     * @see org.apache.james.mime4j.util.TempStorage#getRootTempPath()
116     */
117    public TempPath getRootTempPath() {
118        return rootPath;
119    }
120
121    private class SimpleTempPath implements TempPath {
122        private File path = null;
123
124        private SimpleTempPath(String path) {
125            this.path = new File(path);
126        }
127
128        private SimpleTempPath(File path) {
129            this.path = path;
130        }
131
132        /**
133         * @see org.apache.james.mime4j.util.TempPath#createTempFile()
134         */
135        public TempFile createTempFile() throws IOException {
136            return SimpleTempStorage.this.createTempFile(this, null, null);
137        }
138
139        /**
140         * @see org.apache.james.mime4j.util.TempPath#createTempFile(java.lang.String, java.lang.String)
141         */
142        public TempFile createTempFile(String prefix, String suffix)
143                throws IOException {
144
145            return SimpleTempStorage.this.createTempFile(this, prefix, suffix);
146        }
147
148        /**
149         * @see org.apache.james.mime4j.util.TempPath#createTempFile(java.lang.String, java.lang.String, boolean)
150         */
151        public TempFile createTempFile(String prefix, String suffix,
152                                       boolean allowInMemory)
153            throws IOException {
154
155            return SimpleTempStorage.this.createTempFile(this, prefix, suffix);
156        }
157
158        /**
159         * @see org.apache.james.mime4j.util.TempPath#getAbsolutePath()
160         */
161        public String getAbsolutePath() {
162            return path.getAbsolutePath();
163        }
164
165        /**
166         * Do nothing
167         */
168        public void delete() {
169        }
170
171        /**
172         * @see org.apache.james.mime4j.util.TempPath#createTempPath()
173         */
174        public TempPath createTempPath() throws IOException {
175            return SimpleTempStorage.this.createTempPath(this, null);
176        }
177
178        /**
179         * @see org.apache.james.mime4j.util.TempPath#createTempPath(java.lang.String)
180         */
181        public TempPath createTempPath(String prefix) throws IOException {
182            return SimpleTempStorage.this.createTempPath(this, prefix);
183        }
184
185    }
186
187    private class SimpleTempFile implements TempFile {
188        private File file = null;
189
190        private SimpleTempFile(File file) {
191            this.file = file;
192            this.file.deleteOnExit();
193        }
194
195        /**
196         * @see org.apache.james.mime4j.util.TempFile#getInputStream()
197         */
198        public InputStream getInputStream() throws IOException {
199            return new BufferedInputStream(new FileInputStream(file));
200        }
201
202        /**
203         * @see org.apache.james.mime4j.util.TempFile#getOutputStream()
204         */
205        public OutputStream getOutputStream() throws IOException {
206            return new BufferedOutputStream(new FileOutputStream(file));
207        }
208
209        /**
210         * @see org.apache.james.mime4j.util.TempFile#getAbsolutePath()
211         */
212        public String getAbsolutePath() {
213            return file.getAbsolutePath();
214        }
215
216        /**
217         * Do nothing
218         */
219        public void delete() {
220            // Not implementated
221        }
222
223        /**
224         * @see org.apache.james.mime4j.util.TempFile#isInMemory()
225         */
226        public boolean isInMemory() {
227            return false;
228        }
229
230        /**
231         * @see org.apache.james.mime4j.util.TempFile#length()
232         */
233        public long length() {
234            return file.length();
235        }
236
237    }
238}
239