1 21 package com.db4o.foundation.network; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.foundation.*; 27 28 32 class ByteBuffer4 { 33 34 private final int DISCARD_BUFFER_SIZE = 500; 35 protected byte[] i_cache; 36 private boolean i_closed = false; 37 protected int i_readOffset; 38 protected int i_timeout; 39 protected int i_writeOffset; 40 protected final Lock4 i_lock = new Lock4(); 41 42 public ByteBuffer4(int timeout) { 43 i_timeout = timeout; 44 } 45 46 protected int available() { 47 return i_writeOffset - i_readOffset; 48 } 49 50 protected void checkDiscardCache() { 51 if (i_readOffset == i_writeOffset && i_cache.length > DISCARD_BUFFER_SIZE) { 52 i_cache = null; 53 i_readOffset = 0; 54 i_writeOffset = 0; 55 } 56 } 57 58 void close() { 59 i_closed = true; 60 } 61 62 protected void makefit(int length) { 63 if (i_cache == null) { 64 i_cache = new byte[length]; 65 } else { 66 if (i_writeOffset + length > i_cache.length) { 68 if (i_writeOffset + length - i_readOffset <= i_cache.length) { 70 byte[] temp = new byte[i_cache.length]; 71 System.arraycopy(i_cache, i_readOffset, temp, 0, i_cache.length - i_readOffset); 72 i_cache = temp; 73 i_writeOffset -= i_readOffset; 74 i_readOffset = 0; 75 76 } else { 78 byte[] temp = new byte[i_writeOffset + length]; 79 System.arraycopy(i_cache, 0, temp, 0, i_cache.length); 80 i_cache = temp; 81 } 82 } 83 } 84 } 85 86 public int read() throws IOException { 87 try{ 88 Integer ret = (Integer )i_lock.run(new Closure4() { 89 public Object run() throws Exception { 90 waitForAvailable(); 91 int retVal = i_cache[i_readOffset++]; 92 checkDiscardCache(); 93 return new Integer (retVal); 94 } 95 96 }); 97 return ret.intValue(); 98 }catch(IOException iex){ 99 throw iex; 100 }catch(Exception bex){ 101 } 103 return -1; 104 } 105 106 public int read(final byte[] a_bytes, final int a_offset, final int a_length) throws IOException { 107 try{ 108 Integer ret = (Integer )i_lock.run(new Closure4() { 109 public Object run() throws Exception { 110 waitForAvailable(); 111 int avail = available(); 112 int length = a_length; 113 if (avail < a_length) { 114 length = avail; 115 } 116 System.arraycopy(i_cache, i_readOffset, a_bytes, a_offset, length); 117 i_readOffset += length; 118 checkDiscardCache(); 119 return new Integer (avail); 120 } 121 122 }); 123 return ret.intValue(); 124 }catch(IOException iex){ 125 throw iex; 126 }catch(Exception bex){ 127 } 129 return -1; 130 } 131 132 public void setTimeout(int timeout) { 133 i_timeout = timeout; 134 } 135 136 protected void waitForAvailable() throws IOException { 137 while (available() == 0) { 138 try { 139 i_lock.snooze(i_timeout); 140 } catch (Exception e) { 141 throw new IOException(Messages.get(55)); 142 } 143 } 144 if (i_closed) { 145 throw new IOException(Messages.get(35)); 146 } 147 148 } 149 150 public void write(byte[] bytes) { 151 write(bytes, 0, bytes.length); 152 } 153 154 public void write(final byte[] bytes, final int off, final int len) { 155 try { 156 i_lock.run(new Closure4() { 157 public Object run() throws Exception { 158 makefit(len); 159 System.arraycopy(bytes, off, i_cache, i_writeOffset, len); 160 i_writeOffset += len; 161 i_lock.awake(); 162 return null; 163 } 164 }); 165 } catch (Exception e) { 166 167 169 } 170 } 171 172 public void write(final int i) { 173 try { 174 i_lock.run(new Closure4() { 175 public Object run() throws Exception { 176 makefit(1); 177 i_cache[i_writeOffset++] = (byte) i; 178 i_lock.awake(); 179 return null; 180 } 181 }); 182 } catch (Exception e) { 183 184 186 } 187 } 188 } 189 | Popular Tags |