1 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 ; 42 import java.io.IOException ; 43 import java.io.RandomAccessFile ; 44 import java.awt.event.ActionListener ; 45 import java.awt.event.ActionEvent ; 46 47 51 class FMBufferManagerRO extends RAFBufferManager { 52 53 File file; 54 ActionListener listener; 55 volatile boolean reading; 56 57 public FMBufferManagerRO(File file, ActionListener listener) throws IOException { 58 super(new RandomAccessFile (file, "r")); 59 this.listener = listener; 60 this.file = file; 61 } 62 63 protected Buffer createBuffer(RandomAccessFile raf, long offset, int dsLength) { 64 long maxLength = 0; 65 try { 66 maxLength = raf.length() - offset; 67 } 68 catch (IOException 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 (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 get() throws IOException { 99 if(file == null) { 100 throw new NullPointerException ("This stream cannot be reopened!"); 101 } 102 boolean opened = false; 103 if (raf == null) { 104 raf = new RandomAccessFile (file, "r"); 105 listener.actionPerformed(new ActionEvent (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 (this, ActionEvent.ACTION_PERFORMED, OpenFileManager.GET)); 114 } 115 return raf; 116 } 117 118 class FMBufferRO extends RAFBufferRO { 119 120 public FMBufferRO(RandomAccessFile ra, long offset, int length) { 121 super(ra, offset, length); 122 } 123 124 protected void readData() throws IOException { 125 raf = get(); 126 super.readData(); 127 } 128 } 129 } 130 | Popular Tags |