KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > data > SyncDataFileReader


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl.data;
19
20 import java.io.IOException JavaDoc;
21 import java.io.RandomAccessFile JavaDoc;
22 import org.apache.activemq.kaha.Marshaller;
23 import org.apache.activemq.kaha.StoreLocation;
24 import org.apache.activemq.util.DataByteArrayInputStream;
25 /**
26  * Optimized Store reader
27  *
28  * @version $Revision: 1.1.1.1 $
29  */

30 public final class SyncDataFileReader {
31     
32     private DataManagerImpl dataManager;
33     private DataByteArrayInputStream dataIn;
34
35     /**
36      * Construct a Store reader
37      *
38      * @param fileId
39      */

40     SyncDataFileReader(DataManagerImpl fileManager){
41         this.dataManager=fileManager;
42         this.dataIn=new DataByteArrayInputStream();
43     }
44
45     /* (non-Javadoc)
46      * @see org.apache.activemq.kaha.impl.data.DataFileReader#readDataItemSize(org.apache.activemq.kaha.impl.data.DataItem)
47      */

48     public synchronized byte readDataItemSize(DataItem item) throws IOException JavaDoc {
49         RandomAccessFile JavaDoc file = dataManager.getDataFile(item).getRandomAccessFile();
50         file.seek(item.getOffset()); // jump to the size field
51
byte rc = file.readByte();
52         item.setSize(file.readInt());
53         return rc;
54     }
55     
56     /* (non-Javadoc)
57      * @see org.apache.activemq.kaha.impl.data.DataFileReader#readItem(org.apache.activemq.kaha.Marshaller, org.apache.activemq.kaha.StoreLocation)
58      */

59     public synchronized Object JavaDoc readItem(Marshaller marshaller,StoreLocation item) throws IOException JavaDoc{
60         RandomAccessFile JavaDoc file=dataManager.getDataFile(item).getRandomAccessFile();
61         
62         // TODO: we could reuse the buffer in dataIn if it's big enough to avoid
63
// allocating byte[] arrays on every readItem.
64
byte[] data=new byte[item.getSize()];
65         file.seek(item.getOffset()+DataManagerImpl.ITEM_HEAD_SIZE);
66         file.readFully(data);
67         dataIn.restart(data);
68         return marshaller.readPayload(dataIn);
69     }
70 }
71
Popular Tags