1 20 package fr.dyade.aaa.util; 21 22 import java.io.*; 23 24 import org.objectweb.util.monolog.api.BasicLevel; 25 import org.objectweb.util.monolog.api.Logger; 26 27 public class Pipe { 28 protected String name = null; 29 30 protected int size = 0; 31 32 38 protected int in = -1; 39 40 44 protected int out = 0; 45 46 private Logger logmon = null; 47 private long cpt1, cpt2; 48 49 public static final int DFLT_BUF_SIZE = 50; 50 public static final String DFLT_NAME = "noname"; 51 public static final String DFLT_DIR = null; 52 53 56 protected Object [] buffer = null; 57 58 62 protected long fbufinptr = 0; 63 67 protected long fbufoutptr = -1; 68 69 protected RandomAccessFile fbufin = null; 70 protected RandomAccessFile fbufout = null; 71 72 75 public Pipe() throws IOException { 76 this(DFLT_BUF_SIZE, DFLT_NAME, DFLT_DIR); 77 } 78 79 public final int getBufferSize() { 80 return size; 81 } 82 83 public final int getSizeInFile() { 84 if (fbufoutptr == -1) 85 return -1; 86 else 87 return (int) (fbufoutptr - fbufinptr); 88 } 89 90 95 public Pipe(int size, String name, String dir) throws IOException { 96 this.size = size; 97 buffer = new Object [size]; 98 99 this.name = name; 100 101 File tmp = null; 102 if (dir == null) 103 tmp = File.createTempFile(name, "pipe"); 104 else 105 tmp = File.createTempFile(name, "pipe", new File(dir)); 106 tmp.deleteOnExit(); 107 fbufin = new RandomAccessFile(tmp, "r"); 108 fbufout = new RandomAccessFile(tmp, "rw"); 109 110 logmon = Debug.getLogger("fr.dyade.aaa.util.Pipe.#" + name); 111 } 112 113 public synchronized void write(byte[] msg) throws IOException { 114 if ((fbufoutptr == -1) && (in != out)) { 115 if (in < 0) { 118 in = 0; 119 out = 0; 120 } 121 buffer[in++] = (byte[]) msg; 122 if (in == size) in = 0; 123 notify(); 124 } else { 125 if (fbufoutptr == -1) { 128 fbufout.seek(0); 129 fbufoutptr = 0; 130 } 131 fbufout.writeInt(msg.length); 132 fbufout.write(msg); 133 fbufoutptr += 1; 134 } 135 } 136 137 public synchronized int read(Object [] buf) throws IOException { 138 while (in < 0) { 139 if (fbufoutptr == -1) { 141 try { 142 wait(); 143 } catch (InterruptedException ex) { 144 throw new java.io.InterruptedIOException (); 145 } 146 continue; 147 } else { 148 in = 0; 149 out = 0; 150 int l; 151 152 153 while ((fbufinptr < fbufoutptr) && (in < size)) { 154 l = fbufin.readInt(); 155 byte[] msg = new byte[l]; 156 if (fbufin.read(msg) != l) 157 throw new IOException("buffer file corrupted"); 158 buffer[in++] = msg; 159 fbufinptr += 1; 160 } 161 if (fbufinptr == fbufoutptr) { 162 fbufinptr = 0; 164 fbufoutptr = -1; 165 fbufout.setLength(0); 166 fbufin.seek(0); 167 } 168 if (in == size) in = 0; 169 } 170 } 171 172 173 int idx = 0; 174 do { 176 buf[idx++] = buffer[out]; 177 buffer[out] = null; 178 out += 1; 179 if (out >= size) out = 0; 180 } while ((out != in) && (idx < buf.length)); 181 182 cpt1 += 1; cpt2 += idx; 183 if ((cpt1 %10000) == 0) { 184 if (logmon.isLoggable(BasicLevel.DEBUG)) { 185 logmon.log(BasicLevel.DEBUG, 186 "Pipe.#" + name + ": " + 187 cpt2 + '/' + cpt1 + '/' + in +'/' + out); 188 } 189 } 190 191 if (out == in) { 192 193 in = -1; 194 } 195 196 return idx; 197 } 198 } 199 | Popular Tags |