KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.imagero.uio.buffer.fm;
33
34 import com.imagero.uio.buffer.MutableRAFBufferManager;
35 import com.imagero.uio.buffer.Buffer;
36 import com.imagero.uio.buffer.RAFBuffer;
37 import com.imagero.uio.io.IOutils;
38 import com.imagero.uio.Sys;
39
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.RandomAccessFile JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.awt.event.ActionEvent JavaDoc;
45
46 /**
47  * Special version of MutableBufferManager for work with FileManger
48  * @author Andrey Kuznetsov
49  */

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