Exemplo n.º 1
0
 private final void handleResp() {
   try {
     int available = inputStream.available();
     if ((toRead == 0) && (available >= 6)) {
       expectData = true;
       for (int i = 0; i < 6; i += inputStream.read(inputBuffer, i, 6 - i)) {;
       }
       int type = inputBuffer[0] & 0xff;
       int id = ((inputBuffer[1] & 0xff) << 8) + (inputBuffer[2] & 0xff);
       int size = ((inputBuffer[3] & 0xff) << 8) + (inputBuffer[4] & 0xff);
       int part = inputBuffer[5] & 0xff;
       onDemandNode = null;
       for (OnDemandNode ondemandnode = (OnDemandNode) sentRequests.getBack();
           ondemandnode != null;
           ondemandnode = (OnDemandNode) sentRequests.getPrevious()) {
         if ((ondemandnode.type == type) && (ondemandnode.id == id)) {
           onDemandNode = ondemandnode;
         }
         if (onDemandNode != null) {
           ondemandnode.cyclesSinceSend = 0;
         }
       }
       if (onDemandNode != null) {
         idleCycles = 0;
         if (size == 0) {
           SignLink.reportError("Rej: " + type + "," + id);
           onDemandNode.buffer = null;
           if (onDemandNode.immediate) {
             synchronized (completed) {
               completed.insertBack(onDemandNode);
             }
           } else {
             onDemandNode.remove();
           }
           onDemandNode = null;
         } else {
           if ((onDemandNode.buffer == null) && (part == 0)) {
             onDemandNode.buffer = new byte[size];
           }
           if ((onDemandNode.buffer == null) && (part != 0)) {
             throw new IOException("missing start of file");
           }
         }
       }
       offset = part * 500;
       toRead = 500;
       if (toRead > (size - (part * 500))) {
         toRead = size - (part * 500);
       }
     }
     if ((toRead <= 0) || (available < toRead)) {
       return;
     }
     expectData = true;
     byte[] buffer = inputBuffer;
     int bufferOffset = 0;
     if (onDemandNode != null) {
       buffer = onDemandNode.buffer;
       bufferOffset = offset;
     }
     for (int i = 0; i < toRead; i += inputStream.read(buffer, i + bufferOffset, toRead - i)) {;
     }
     if (((toRead + offset) >= buffer.length) && (onDemandNode != null)) {
       if (client.stores[0] != null) {
         client.stores[onDemandNode.type + 1].put(buffer.length, buffer, onDemandNode.id);
       }
       if (!onDemandNode.immediate && (onDemandNode.type == 3)) {
         onDemandNode.immediate = true;
         onDemandNode.type = 93;
       }
       if (onDemandNode.immediate) {
         synchronized (completed) {
           completed.insertBack(onDemandNode);
         }
       } else {
         onDemandNode.remove();
       }
     }
     toRead = 0;
   } catch (IOException ioexception) {
     try {
       socket.close();
     } catch (Exception exception) {
       /* empty */
     }
     socket = null;
     inputStream = null;
     outputStream = null;
     toRead = 0;
   }
 }