KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > fm > FMBufferManagerRO


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.imagero.uio.buffer.fm;
34
35 import com.imagero.uio.buffer.RAFBufferManager;
36 import com.imagero.uio.buffer.Buffer;
37 import com.imagero.uio.buffer.RAFBufferRO;
38 import com.imagero.uio.io.IOutils;
39 import com.imagero.uio.Sys;
40
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.RandomAccessFile JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45 import java.awt.event.ActionEvent JavaDoc;
46
47 /**
48  * Special version of BufferManager for work with FileManager
49  * @author Andrey Kuznetsov
50  */

51 class FMBufferManagerRO extends RAFBufferManager {
52
53     File JavaDoc file;
54     ActionListener JavaDoc listener;
55     volatile boolean reading;
56
57     public FMBufferManagerRO(File JavaDoc file, ActionListener JavaDoc listener) throws IOException JavaDoc {
58         super(new RandomAccessFile JavaDoc(file, "r"));
59         this.listener = listener;
60         this.file = file;
61     }
62
63     protected Buffer createBuffer(RandomAccessFile JavaDoc raf, long offset, int dsLength) {
64         long maxLength = 0;
65         try {
66             maxLength = raf.length() - offset;
67         }
68         catch (IOException JavaDoc ex) {
69             ex.printStackTrace();
70         }
71         if (maxLength < 0) {
72             return new FMBufferRO(raf, offset, 0);
73         }
74         return new FMBufferRO(raf, offset, (int) Math.min(maxLength, dsLength));
75     }
76
77     boolean canClose() {
78         return !reading;
79     }
80
81     void _close() {
82         super.close();
83         listener = null;
84         file = null;
85         raf = null;
86     }
87
88     public void close() {
89         IOutils.closeStream(raf);
90         raf = null;
91         listener.actionPerformed(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, OpenFileManager.CLOSE));
92         if(OpenFileManager.debug && file != null) {
93             Sys.out.print("\nclose: ");
94             Sys.out.println(file.getAbsolutePath());
95         }
96     }
97
98     private RandomAccessFile JavaDoc get() throws IOException JavaDoc {
99         if(file == null) {
100             throw new NullPointerException JavaDoc("This stream cannot be reopened!");
101         }
102         boolean opened = false;
103         if (raf == null) {
104             raf = new RandomAccessFile JavaDoc(file, "r");
105             listener.actionPerformed(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, OpenFileManager.OPEN));
106             opened = true;
107             if(OpenFileManager.debug && file != null) {
108                 Sys.out.print("\nopen: ");
109                 Sys.out.println(file.getAbsolutePath());
110             }
111         }
112         if(!opened) {
113             listener.actionPerformed(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, OpenFileManager.GET));
114         }
115         return raf;
116     }
117
118     class FMBufferRO extends RAFBufferRO {
119
120         public FMBufferRO(RandomAccessFile JavaDoc ra, long offset, int length) {
121             super(ra, offset, length);
122         }
123
124         protected void readData() throws IOException JavaDoc {
125             raf = get();
126             super.readData();
127         }
128     }
129 }
130
Popular Tags