KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > attachments > ManagedMemoryDataSource


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 package org.jboss.axis.attachments;
9
10 import org.jboss.logging.Logger;
11
12 import javax.activation.DataSource JavaDoc;
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /**
23  * This class allows small attachments to be cached in memory.
24  *
25  * @author Thomas.Diesler@jboss.org
26  */

27 public class ManagedMemoryDataSource implements DataSource JavaDoc
28 {
29    private static Logger log = Logger.getLogger(ManagedMemoryDataSource.class);
30
31    /** Field contentType */
32    protected String JavaDoc contentType = "application/octet-stream"; // Is the default.
33

34    // The memory cache
35
private byte[] contentData;
36
37    // The file cache
38
private File JavaDoc cacheFile;
39
40    // Memory is allocated in these size chunks.
41

42    /** Field READ_CHUNK_SZ */
43    public static final int READ_CHUNK_SZ = 32 * 1024;
44
45    /** Field READ_CHUNK_SZ */
46    public static final int MAX_MEMORY_SZ = 64 * 1024;
47
48    /** Create a new in memory data source
49     *
50     * @param inputStream is the source input stream that is used to create this data source..
51     * @param contentType the mime type for this data stream
52     */

53    public ManagedMemoryDataSource(InputStream JavaDoc inputStream, String JavaDoc contentType) throws IOException JavaDoc
54    {
55       if (contentType != null)
56          this.contentType = contentType;
57
58       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(READ_CHUNK_SZ);
59       OutputStream JavaDoc os = baos;
60
61       byte[] bytes = new byte[READ_CHUNK_SZ];
62       int read = inputStream.read(bytes);
63       while (read > 0)
64       {
65          os.write(bytes, 0, read);
66
67          if (baos != null && baos.size() > MAX_MEMORY_SZ)
68          {
69             // Create a cache file, which is cleared in finalize
70
cacheFile = File.createTempFile("attachment", ".dat");
71             cacheFile.deleteOnExit();
72
73             log.debug("Created cache file: " + cacheFile.toURL());
74             os = new FileOutputStream JavaDoc(cacheFile);
75             os.write(baos.toByteArray());
76             baos = null;
77          }
78
79          read = inputStream.read(bytes);
80       }
81       os.flush();
82       os.close();
83
84       if (baos != null)
85       {
86          log.debug("Using in memory cache: " + baos.size());
87          this.contentData = baos.toByteArray();
88       }
89    }
90
91    public String JavaDoc getContentType()
92    {
93       return contentType;
94    }
95
96    public InputStream JavaDoc getInputStream() throws IOException JavaDoc
97    {
98       if (contentData != null)
99          return new ByteArrayInputStream JavaDoc(contentData);
100       else if (cacheFile != null)
101          return new FileInputStream JavaDoc(cacheFile);
102       else
103          throw new IllegalStateException JavaDoc("No cache available");
104    }
105
106    public String JavaDoc getName()
107    {
108       return null;
109    }
110
111    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
112    {
113       return null;
114    }
115
116    /** Delete the cache file if one exists */
117    protected void finalize() throws Throwable JavaDoc
118    {
119       super.finalize();
120
121       if (cacheFile != null)
122          cacheFile.delete();
123    }
124 }
125
Popular Tags