001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.core.io.rmi;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.nio.channels.AsynchronousCloseException;
024: import java.nio.channels.ByteChannel;
025: import java.nio.channels.ClosedByInterruptException;
026: import java.nio.channels.ClosedChannelException;
027: import java.nio.channels.NonReadableChannelException;
028: import java.nio.channels.NonWritableChannelException;
029: import java.nio.channels.ReadableByteChannel;
030: import java.nio.channels.WritableByteChannel;
031:
032: import com.mobixess.jodb.core.JODBConfig;
033: import com.mobixess.jodb.core.JodbIOException;
034: import com.mobixess.jodb.core.io.IRandomAccessDataBuffer;
035: import com.mobixess.jodb.core.io.IRandomAccessBufferFactory.BUFFER_TYPE;
036:
037: public class RandomAccessDataBufferProxy implements
038: IRandomAccessDataBuffer {
039:
040: private IRandomAccessDataBuffer _tempFile;
041: private File _file = File.createTempFile("JODB", "RemoteBuffer");
042: private IOTicketRemoteInterface _ticketRemoteInterface;
043: private long _offset;
044:
045: public RandomAccessDataBufferProxy(
046: IOTicketRemoteInterface ticketRemoteInterface)
047: throws IOException {
048: _tempFile = JODBConfig.getRandomAccessBufferFactory()
049: .createBuffer("", BUFFER_TYPE.CLIENT_TEMP_FILE, true);
050: _ticketRemoteInterface = ticketRemoteInterface;
051: }
052:
053: public void close() throws IOException {
054: _tempFile.close();
055: }
056:
057: public void delete() throws IOException {
058: close();
059: _file.delete();
060: }
061:
062: public ByteChannel getChannel() {
063: return _tempFile.getChannel();
064: }
065:
066: public long getCursorOffset() throws IOException {
067: return _tempFile.getCursorOffset() + _offset;
068: }
069:
070: public void seek(long pos) throws IOException {
071: if (pos < _offset || pos > _offset + _tempFile.length()) {
072: //prefetch(pos);
073: throw new JodbIOException("Read out of bounds " + pos);
074: }
075: _tempFile.seek(pos - _offset);
076: }
077:
078: /**
079: * Transfers bytes from this channel's file to the given writable byte
080: * channel.
081: *
082: * <p> An attempt is made to read up to <tt>count</tt> bytes starting at
083: * the given <tt>position</tt> in this channel's file and write them to the
084: * target channel. An invocation of this method may or may not transfer
085: * all of the requested bytes; whether or not it does so depends upon the
086: * natures and states of the channels. Fewer than the requested number of
087: * bytes are transferred if this channel's file contains fewer than
088: * <tt>count</tt> bytes starting at the given <tt>position</tt>, or if the
089: * target channel is non-blocking and it has fewer than <tt>count</tt>
090: * bytes free in its output buffer.
091: *
092: * <p> This method does not modify this channel's position. If the given
093: * position is greater than the file's current size then no bytes are
094: * transferred. If the target channel has a position then bytes are
095: * written starting at that position and then the position is incremented
096: * by the number of bytes written.
097: *
098: * <p> This method is potentially much more efficient than a simple loop
099: * that reads from this channel and writes to the target channel. Many
100: * operating systems can transfer bytes directly from the filesystem cache
101: * to the target channel without actually copying them. </p>
102: *
103: * @param position
104: * The position within the file at which the transfer is to begin;
105: * must be non-negative
106: *
107: * @param count
108: * The maximum number of bytes to be transferred; must be
109: * non-negative
110: *
111: * @param target
112: * The target channel
113: *
114: * @return The number of bytes, possibly zero,
115: * that were actually transferred
116: *
117: * @throws IllegalArgumentException
118: * If the preconditions on the parameters do not hold
119: *
120: * @throws NonReadableChannelException
121: * If this channel was not opened for reading
122: *
123: * @throws NonWritableChannelException
124: * If the target channel was not opened for writing
125: *
126: * @throws ClosedChannelException
127: * If either this channel or the target channel is closed
128: *
129: * @throws AsynchronousCloseException
130: * If another thread closes either channel
131: * while the transfer is in progress
132: *
133: * @throws ClosedByInterruptException
134: * If another thread interrupts the current thread while the
135: * transfer is in progress, thereby closing both channels and
136: * setting the current thread's interrupt status
137: *
138: * @throws IOException
139: * If some other I/O error occurs
140: */
141: public long transferTo(long position, long count,
142: WritableByteChannel target) throws IOException {
143: return _tempFile.transferTo(position - _offset, count, target);
144: }
145:
146: public boolean readBoolean() throws IOException {
147: return _tempFile.readBoolean();
148: }
149:
150: public byte readByte() throws IOException {
151:
152: return _tempFile.readByte();
153: }
154:
155: public char readChar() throws IOException {
156:
157: return _tempFile.readChar();
158: }
159:
160: public double readDouble() throws IOException {
161:
162: return _tempFile.readDouble();
163: }
164:
165: public float readFloat() throws IOException {
166:
167: return _tempFile.readFloat();
168: }
169:
170: public void readFully(byte[] b) throws IOException {
171:
172: _tempFile.readFully(b);
173: }
174:
175: public void readFully(byte[] b, int off, int len)
176: throws IOException {
177:
178: _tempFile.read(b, off, len);
179: }
180:
181: public int readInt() throws IOException {
182:
183: return _tempFile.readInt();
184: }
185:
186: public String readLine() throws IOException {
187:
188: return _tempFile.readLine();
189: }
190:
191: public long readLong() throws IOException {
192:
193: return _tempFile.readLong();
194: }
195:
196: public short readShort() throws IOException {
197:
198: return _tempFile.readShort();
199: }
200:
201: public String readUTF() throws IOException {
202:
203: return _tempFile.readUTF();
204: }
205:
206: public int readUnsignedByte() throws IOException {
207:
208: return _tempFile.readUnsignedByte();
209: }
210:
211: public int readUnsignedShort() throws IOException {
212:
213: return _tempFile.readUnsignedShort();
214: }
215:
216: public int skipBytes(int n) throws IOException {
217:
218: return _tempFile.skipBytes(n);
219: }
220:
221: public void read(byte[] b, int off, int len) throws IOException {
222: _tempFile.read(b, off, len);
223: }
224:
225: public long skip(long n) throws IOException {
226: long totalSkiped = 0;
227: while (n > 0) {
228: int skiped = _tempFile.skipBytes((int) Math.min(n,
229: Integer.MAX_VALUE));
230: if (skiped == 0) {
231: return totalSkiped;
232: }
233: totalSkiped += skiped;
234: n -= skiped;
235: }
236: return 0;
237: }
238:
239: @Override
240: public String toString() {
241: try {
242: return "transaction buffer length=" + length()
243: + " pointer=" + getCursorOffset() + " " + _tempFile;
244: } catch (IOException e) {
245: return super .toString();
246: }
247: }
248:
249: private void clear() throws IOException {
250: _offset = 0;
251: _tempFile.setLength(0);
252: }
253:
254: public void prefetch(long offset) throws IOException {
255: clear();
256: int len = _ticketRemoteInterface.fetchRecord(offset);
257: int transfered = 0;
258: while (transfered < len) {
259: byte[] data = _ticketRemoteInterface.getRecordData(
260: transfered, len - transfered);
261: if (data.length == 0) {
262: throw new IOException();
263: }
264: _tempFile.write(data);
265: transfered += data.length;
266: }
267: _offset = offset;
268: }
269:
270: public long length() throws IOException {
271: // TODO Auto-generated method stub
272: throw new RuntimeException("Not Implemented");
273: //return 0;
274: }
275:
276: public void resetToEnd() throws IOException {
277: // TODO Auto-generated method stub
278: throw new RuntimeException("Not Implemented");
279: //
280: }
281:
282: public void resetToStart() throws IOException {
283: // TODO Auto-generated method stub
284: throw new RuntimeException("Not Implemented");
285: //
286: }
287:
288: public void setLength(long newLength) throws IOException {
289: // TODO Auto-generated method stub
290: throw new RuntimeException("Not Implemented");
291: //
292: }
293:
294: public long transferFrom(ReadableByteChannel src, long position,
295: long count) throws IOException {
296: // TODO Auto-generated method stub
297: throw new RuntimeException("Not Implemented");
298: //return 0;
299: }
300:
301: public void write(int b) throws IOException {
302: // TODO Auto-generated method stub
303: throw new RuntimeException("Not Implemented");
304: //
305: }
306:
307: public void write(byte[] b) throws IOException {
308: // TODO Auto-generated method stub
309: throw new RuntimeException("Not Implemented");
310: //
311: }
312:
313: public void write(byte[] b, int off, int len) throws IOException {
314: // TODO Auto-generated method stub
315: throw new RuntimeException("Not Implemented");
316: //
317: }
318:
319: public void writeBoolean(boolean v) throws IOException {
320: // TODO Auto-generated method stub
321: throw new RuntimeException("Not Implemented");
322: //
323: }
324:
325: public void writeByte(int v) throws IOException {
326: // TODO Auto-generated method stub
327: throw new RuntimeException("Not Implemented");
328: //
329: }
330:
331: public void writeBytes(String s) throws IOException {
332: // TODO Auto-generated method stub
333: throw new RuntimeException("Not Implemented");
334: //
335: }
336:
337: public void writeChar(int v) throws IOException {
338: // TODO Auto-generated method stub
339: throw new RuntimeException("Not Implemented");
340: //
341: }
342:
343: public void writeChars(String s) throws IOException {
344: // TODO Auto-generated method stub
345: throw new RuntimeException("Not Implemented");
346: //
347: }
348:
349: public void writeDouble(double v) throws IOException {
350: // TODO Auto-generated method stub
351: throw new RuntimeException("Not Implemented");
352: //
353: }
354:
355: public void writeFloat(float v) throws IOException {
356: // TODO Auto-generated method stub
357: throw new RuntimeException("Not Implemented");
358: //
359: }
360:
361: public void writeInt(int v) throws IOException {
362: // TODO Auto-generated method stub
363: throw new RuntimeException("Not Implemented");
364: //
365: }
366:
367: public void writeLong(long v) throws IOException {
368: // TODO Auto-generated method stub
369: throw new RuntimeException("Not Implemented");
370: //
371: }
372:
373: public void writeShort(int v) throws IOException {
374: // TODO Auto-generated method stub
375: throw new RuntimeException("Not Implemented");
376: //
377: }
378:
379: public void writeUTF(String str) throws IOException {
380: // TODO Auto-generated method stub
381: throw new RuntimeException("Not Implemented");
382: //
383: }
384:
385: }
|