| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0c74932 commit 4ca967f
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -209,15 +209,16 @@ public void dispose() { | |||
| 209 | 209 | ||
| 210 | 210 | ||
| 211 | 211 | public void run() { | |
| 212 | + byte[] readBuffer = new byte[2048]; // Ethernet MTU = 1500 B | ||
| 212 | 213 | while (Thread.currentThread() == thread) { | |
| 213 | 214 | try { | |
| 214 | 215 | while (input != null) { | |
| 215 | - int value; | ||
| 216 | + int readCount; | ||
| 216 | 217 | ||
| 217 | 218 | // try to read a byte using a blocking read. | |
| 218 | 219 | // An exception will occur when the sketch is exits. | |
| 219 | 220 | try { | |
| 220 | - value = input.read(); | ||
| 221 | + readCount = input.read(readBuffer, 0, readBuffer.length); | ||
| 221 | 222 | } catch (SocketException e) { | |
| 222 | 223 | System.err.println("Client SocketException: " + e.getMessage()); | |
| 223 | 224 | // the socket had a problem reading so don't try to read from it again. | |
@@ -226,7 +227,7 @@ public void run() { | |||
| 226 | 227 | } | |
| 227 | 228 | ||
| 228 | 229 | // read returns -1 if end-of-stream occurs (for example if the host disappears) | |
| 229 | - if (value == -1) { | ||
| 230 | + if (readCount == -1) { | ||
| 230 | 231 | System.err.println("Client got end-of-stream."); | |
| 231 | 232 | stop(); | |
| 232 | 233 | return; | |
@@ -235,31 +236,36 @@ public void run() { | |||
| 235 | 236 | synchronized (bufferLock) { | |
| 236 | 237 | // todo: at some point buffer should stop increasing in size, | |
| 237 | 238 | // otherwise it could use up all the memory. | |
| 238 | - if (bufferLast == buffer.length) { | ||
| 239 | - if (bufferIndex > 0) { | ||
| 240 | - // compact the buffer | ||
| 241 | - int bufferLength = bufferLast - bufferIndex; | ||
| 242 | - System.arraycopy(buffer, bufferIndex, buffer, 0, bufferLength); | ||
| 243 | - bufferLast -= bufferIndex; | ||
| 244 | - bufferIndex = 0; | ||
| 245 | - } else { | ||
| 246 | - // resize the buffer | ||
| 247 | - byte temp[] = new byte[bufferLast << 1]; | ||
| 248 | - System.arraycopy(buffer, 0, temp, 0, bufferLast); | ||
| 249 | - buffer = temp; | ||
| 239 | + int freeBack = buffer.length - bufferLast; | ||
| 240 | + if (readCount > freeBack) { | ||
| 241 | + // not enough space at the back | ||
| 242 | + int bufferLength = bufferLast - bufferIndex; | ||
| 243 | + byte[] targetBuffer = buffer; | ||
| 244 | + if (bufferLength + readCount > buffer.length) { | ||
| 245 | + // can't fit even after compacting, resize the buffer | ||
| 246 | + // find the next power of two which can fit everything in | ||
| 247 | + int newSize = Integer.highestOneBit(bufferLength + readCount - 1) << 1; | ||
| 248 | + targetBuffer = new byte[newSize]; | ||
| 250 | 249 | } | |
| 250 | + // compact the buffer (either in-place or into the new bigger buffer) | ||
| 251 | + System.arraycopy(buffer, bufferIndex, targetBuffer, 0, bufferLength); | ||
| 252 | + bufferLast -= bufferIndex; | ||
| 253 | + bufferIndex = 0; | ||
| 254 | + buffer = targetBuffer; | ||
| 251 | 255 | } | |
| 252 | - buffer[bufferLast++] = (byte)value; | ||
| 256 | + // copy all newly read bytes into the buffer | ||
| 257 | + System.arraycopy(readBuffer, 0, buffer, bufferLast, readCount); | ||
| 258 | + bufferLast += readCount; | ||
| 253 | 259 | } | |
| 254 | 260 | ||
| 255 | 261 | // now post an event | |
| 256 | 262 | if (clientEventMethod != null) { | |
| 257 | 263 | try { | |
| 258 | 264 | clientEventMethod.invoke(parent, new Object[] { this }); | |
| 259 | - } catch (Exception e) { | ||
| 260 | - System.err.println("error, disabling clientEvent() for " + host); | ||
| 261 | - e.printStackTrace(); | ||
| 262 | - clientEventMethod = null; | ||
| 265 | + } catch (Exception e) { | ||
| 266 | + System.err.println("error, disabling clientEvent() for " + host); | ||
| 267 | + e.printStackTrace(); | ||
| 268 | + clientEventMethod = null; | ||
| 263 | 269 | } | |
| 264 | 270 | } | |
| 265 | 271 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments