MCPcopy Create free account
hub / github.com/benfry/processing4 / run

Method run

java/libraries/net/src/processing/net/Client.java:217–302  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

215
216
217 @Override
218 public void run() {
219 byte[] readBuffer;
220 { // make the read buffer same size as socket receive buffer so that
221 // we don't waste cycles calling listeners when there is more data waiting
222 int readBufferSize = 1 << 16; // 64 KB (default socket receive buffer size)
223 try {
224 readBufferSize = socket.getReceiveBufferSize();
225 } catch (SocketException ignore) { }
226 readBuffer = new byte[readBufferSize];
227 }
228 while (Thread.currentThread() == thread) {
229 try {
230 while (input != null) {
231 int readCount;
232
233 // try to read a byte using a blocking read.
234 // An exception will occur when the sketch is exits.
235 try {
236 readCount = input.read(readBuffer, 0, readBuffer.length);
237 } catch (SocketException e) {
238 System.err.println("Client SocketException: " + e.getMessage());
239 // the socket had a problem reading so don't try to read from it again.
240 stop();
241 return;
242 }
243
244 // read returns -1 if end-of-stream occurs (for example if the host disappears)
245 if (readCount == -1) {
246 System.err.println("Client got end-of-stream.");
247 stop();
248 return;
249 }
250
251 synchronized (bufferLock) {
252 int freeBack = buffer.length - bufferLast;
253 if (readCount > freeBack) {
254 // not enough space at the back
255 int bufferLength = bufferLast - bufferIndex;
256 byte[] targetBuffer = buffer;
257 if (bufferLength + readCount > buffer.length) {
258 // can't fit even after compacting, resize the buffer
259 // find the next power of two which can fit everything in
260 int newSize = Integer.highestOneBit(bufferLength + readCount - 1) << 1;
261 if (newSize > MAX_BUFFER_SIZE) {
262 // buffer is full because client is not reading (fast enough)
263 System.err.println("Client: can't receive more data, buffer is full. " +
264 "Make sure you read the data from the client.");
265 stop();
266 return;
267 }
268 targetBuffer = new byte[newSize];
269 }
270 // compact the buffer (either in-place or into the new bigger buffer)
271 System.arraycopy(buffer, bufferIndex, targetBuffer, 0, bufferLength);
272 bufferLast -= bufferIndex;
273 bufferIndex = 0;
274 buffer = targetBuffer;

Callers

nothing calls this directly

Calls 6

stopMethod · 0.95
arraycopyMethod · 0.80
getMessageMethod · 0.65
readMethod · 0.45
printlnMethod · 0.45
printStackTraceMethod · 0.45

Tested by

no test coverage detected