KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > FileSource


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: FileSource.java,v 1.32 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.io.IOException JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13 import java.nio.ByteBuffer JavaDoc;
14
15 import com.sleepycat.je.DatabaseException;
16 import com.sleepycat.je.dbi.EnvironmentImpl;
17
18 /**
19  * FileSource is used as a channel to a log file when faulting in objects
20  * from the log.
21  */

22 class FileSource implements LogSource {
23
24     private RandomAccessFile JavaDoc file;
25     private int readBufferSize;
26     private FileManager fileManager;
27
28     FileSource(RandomAccessFile JavaDoc file,
29            int readBufferSize,
30            FileManager fileManager) {
31         this.file = file;
32         this.readBufferSize = readBufferSize;
33     this.fileManager = fileManager;
34     }
35
36     /**
37      * @see LogSource#release
38      */

39     public void release()
40         throws DatabaseException {
41     }
42
43     /**
44      * @see LogSource#getBytes
45      */

46     public ByteBuffer JavaDoc getBytes(long fileOffset)
47         throws IOException JavaDoc {
48         
49         /* Fill up buffer from file. */
50         ByteBuffer JavaDoc destBuf = ByteBuffer.allocate(readBufferSize);
51         fileManager.readFromFile(file, destBuf, fileOffset);
52
53     assert EnvironmentImpl.maybeForceYield();
54
55         destBuf.flip();
56         return destBuf;
57     }
58
59     /**
60      * @see LogSource#getBytes
61      */

62     public ByteBuffer JavaDoc getBytes(long fileOffset, int numBytes)
63         throws IOException JavaDoc {
64
65         /* Fill up buffer from file. */
66         ByteBuffer JavaDoc destBuf = ByteBuffer.allocate(numBytes);
67         fileManager.readFromFile(file, destBuf, fileOffset);
68
69     assert EnvironmentImpl.maybeForceYield();
70
71         destBuf.flip();
72         
73         assert destBuf.remaining() >= numBytes:
74             "remaining=" + destBuf.remaining() +
75             " numBytes=" + numBytes;
76         return destBuf;
77     }
78 }
79
Popular Tags