KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > InputStreamContainer


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.InputStreamContainer
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.data;
23
24 import org.apache.derby.impl.store.raw.data.FileContainer;
25
26 import org.apache.derby.iapi.store.raw.ContainerKey;
27
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.iapi.store.raw.log.LogInstant;
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31 import org.apache.derby.impl.store.raw.data.BaseDataFileFactory;
32
33 import org.apache.derby.iapi.services.io.InputStreamUtil;
34 import org.apache.derby.iapi.reference.SQLState;
35
36 import org.apache.derby.io.StorageFile;
37
38 import java.io.InputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.DataInputStream JavaDoc;
41
42 /**
43     A class that uses a ZipEntry to be a single container file,
44     but read-only.
45
46 */

47
48 final class InputStreamContainer extends FileContainer {
49
50     private StorageFile containerPath;
51     
52     /*
53      * Constructors
54      */

55
56     InputStreamContainer(BaseDataFileFactory factory) {
57         super(factory);
58         canUpdate = false;
59     }
60
61     final boolean openContainer(ContainerKey newIdentity) throws StandardException {
62         DataInputStream JavaDoc dis = null;
63         try
64         {
65             InputStream JavaDoc is = null;
66             containerPath = dataFactory.getContainerPath(newIdentity, false);
67             try
68             {
69                 is = containerPath.getInputStream();
70             }
71             catch (IOException JavaDoc ioe)
72             {
73                 // Maybe it's been stubbified.
74
containerPath = dataFactory.getContainerPath(newIdentity, true);
75                 try
76                 {
77                     is = getInputStream();
78                 }
79                 catch (IOException JavaDoc ioe2)
80                 {
81                     containerPath = null;
82                     return false;
83                 }
84             }
85
86             dis = new DataInputStream JavaDoc(is);
87             
88             // FileData has to be positioned just at the beginning
89
// of the first allocation page. And it is because we
90
// just opened the stream and the first allocation page
91
// is located at the beginning of the file.
92
readHeader(dis);
93
94             return true;
95
96         } catch (IOException JavaDoc ioe) {
97             throw StandardException.
98                 newException(SQLState.FILE_CONTAINER_EXCEPTION, ioe, this);
99         } finally {
100             if (dis != null) {
101                 try {
102                     dis.close();
103                 } catch (IOException JavaDoc ioe) {}
104             }
105         }
106     } // end of openContainer
107

108     void closeContainer()
109     {
110         containerPath = null;
111     }
112
113     /**
114         Write out the header information for this container. If an i/o exception
115         occurs then ...
116
117         @see org.apache.derby.iapi.services.cache.Cacheable#clean
118         @exception StandardException Standard Cloudscape error policy
119     */

120     public final void clean(boolean forRemove) throws StandardException {
121
122         // Nothing to do since we are inherently read-only.
123

124     }
125
126     /**
127         Preallocate page.
128     */

129     protected final int preAllocate(long lastPreallocPagenum, int preAllocSize) {
130
131         // Nothing to do since we are inherently read-only.
132
return 0;
133     }
134
135     protected void truncatePages(long lastValidPagenum)
136     {
137         // Nothing to do since we are inherently read-only.
138
return;
139     }
140     
141
142     /*
143     ** Container creation, opening, and closing
144     */

145
146     /**
147         Create a new container, all references to identity must be through the
148         passed in identity, this object will no identity until after this method returns.
149     */

150     void createContainer(ContainerKey newIdentity) throws StandardException {
151         // RESOLVE - probably should throw an error ...
152
}
153
154
155
156     /**
157         Remove the container.
158     */

159     protected final void removeContainer(LogInstant instant, boolean leaveStub) throws StandardException
160     {
161     // RESOVE
162
}
163
164     /*
165     ** Methods used solely by StoredPage
166     */

167
168     /**
169         Read a page into the supplied array.
170
171         <BR> MT - thread safe
172     */

173     protected final void readPage(long pageNumber, byte[] pageData)
174          throws IOException JavaDoc, StandardException
175     {
176
177         if (SanityManager.DEBUG) {
178             SanityManager.ASSERT(!getCommittedDropState());
179         }
180
181         long pageOffset = pageNumber * pageSize;
182
183         readPositionedPage(pageOffset, pageData);
184
185         if (dataFactory.databaseEncrypted() &&
186             pageNumber != FIRST_ALLOC_PAGE_NUMBER)
187         {
188             decryptPage(pageData, pageSize);
189         }
190     }
191
192     /**
193         Read the page at the positioned offset.
194         This default implementation, opens the stream and skips to the offset
195         and then reads the data into pageData.
196     */

197     protected void readPositionedPage(long pageOffset, byte[] pageData) throws IOException JavaDoc {
198
199
200         InputStream JavaDoc is = null;
201         try {
202             // no need to synchronize as each caller gets a new stream
203
is = getInputStream();
204
205             InputStreamUtil.skipBytes(is, pageOffset);
206
207             InputStreamUtil.readFully(is, pageData, 0, pageSize);
208
209             is.close();
210             is = null;
211         } finally {
212             if (is != null) {
213                 try {is.close();} catch (IOException JavaDoc ioe) {}
214             }
215         }
216     }
217
218     /**
219         Write a page from the supplied array.
220
221         <BR> MT - thread safe
222     */

223     protected final void writePage(long pageNumber, byte[] pageData, boolean syncPage)
224         throws IOException JavaDoc, StandardException {
225     }
226
227     protected final void flushAll() {
228     }
229
230     /**
231         Get an input stream positioned at the beginning of the file
232     */

233     protected InputStream JavaDoc getInputStream() throws IOException JavaDoc
234     {
235         return containerPath.getInputStream();
236     }
237
238         
239     /**
240      * Backup the container.
241      * There is no support to backup this type of containers. It may not be any
242      * real use for users because users can simply make copies of the read only
243      * database in Zip files easily using OS utilities.
244      *
245      * @exception StandardException Standard Derby error policy
246      */

247     protected void backupContainer(BaseContainerHandle handle, String JavaDoc backupLocation)
248         throws StandardException
249     {
250         throw StandardException.newException(
251                 SQLState.STORE_FEATURE_NOT_IMPLEMENTED);
252     }
253
254
255     /**
256      * Encrypt the container. There is no support to encrypt
257      * this type of containers.
258      *
259      * @exception StandardException Standard Derby error policy
260      */

261     protected void encryptContainer(BaseContainerHandle handle,
262                                     String JavaDoc newFilePath)
263         throws StandardException
264     {
265         throw StandardException.newException(
266                 SQLState.STORE_FEATURE_NOT_IMPLEMENTED);
267     }
268
269 }
270
Popular Tags