View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.io;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.annotation.concurrent.NotThreadSafe;
28  
29  /**
30   * Handles resources and stores them in-memory.
31   */
32  @NotThreadSafe
33  public final class MemoryStreamHandler implements StreamHandler
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The in-memory store.
43     */
44    private final Map<String, byte[]> dataStore = new HashMap<String, byte[]>();
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Helper to add the streamed information upon closing the stream.
52     */
53    private final class CloseListenerOutputStream extends FilterOutputStream
54    {
55      /**
56       * The identifier of the resource being monitored.
57       */
58      private final String resourceId;
59  
60      /**
61       * Reference to the stream whose closing is monitored.
62       */
63      private final ByteArrayOutputStream out;
64  
65      private CloseListenerOutputStream(final String resourceId,
66          final ByteArrayOutputStream out)
67      {
68        super(out);
69        this.out = out;
70        this.resourceId = resourceId;
71      }
72  
73      @Override
74      public void close() throws IOException
75      {
76        super.close();
77        final byte[] data = out.toByteArray();
78        dataStore.put(resourceId, data);
79      }
80    }
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    // --- get&set --------------------------------------------------------------
89  
90    // --- business -------------------------------------------------------------
91  
92    /**
93     * Allows direct access to the data stored.
94     *
95     * @param resourceId the identifier of the resource requested.
96     * @return the data from the store or <code>null</code> if there is no data
97     *         for this key.
98     */
99    public byte[] getData(final String resourceId)
100   {
101     final byte[] data = dataStore.get(resourceId);
102     return data;
103   }
104 
105   /**
106    * {@inheritDoc}
107    */
108   @Override
109   public InputStream openToRead(final String resourceId) throws IOException
110   {
111     final byte[] data = dataStore.get(resourceId);
112     if (data == null)
113     {
114       throw new IOException("Resource '" + resourceId + "' cannot be found.");
115     }
116 
117     final InputStream in = new ByteArrayInputStream(data);
118     return in;
119   }
120 
121   /**
122    * {@inheritDoc}
123    */
124   @Override
125   public OutputStream openToWrite(final String resourceId) throws IOException
126   {
127     final ByteArrayOutputStream out = new ByteArrayOutputStream(4092);
128     final CloseListenerOutputStream clout =
129         new CloseListenerOutputStream(resourceId, out);
130     dataStore.remove(resourceId);
131     return clout;
132   }
133 
134   // --- object basics --------------------------------------------------------
135 
136 }