示例#1
0
文件: IPCapture.java 项目: star003/sb
 public void read() {
   try {
     ByteArrayInputStream jpgIn = new ByteArrayInputStream(curFrame);
     BufferedImage bufImg = ImageIO.read(jpgIn);
     jpgIn.close();
     int w = bufImg.getWidth();
     int h = bufImg.getHeight();
     if (w != this.width || h != this.height) {
       this.resize(bufImg.getWidth(), bufImg.getHeight());
     }
     bufImg.getRGB(0, 0, w, h, this.pixels, 0, w);
     this.updatePixels();
     frameAvailable = false;
   } catch (IOException e) {
     System.err.println("Error acquiring the frame: " + e.getMessage());
   }
 }
  public void testSimpleWrite() throws Exception {
    Message one, two, three;
    one = q("query one");
    two = g(7123);
    three = s(8134);

    assertEquals(0, STATS.getSent());
    assertFalse(SINK.interested());

    WRITER.send(one);
    WRITER.send(two);
    WRITER.send(three);
    assertEquals(3, STATS.getSent());

    assertTrue(SINK.interested());

    assertEquals(0, SENT.size());

    assertFalse(WRITER.handleWrite()); // nothing left to write.
    assertEquals(3, SENT.size());

    ByteBuffer buffer = SINK.getBuffer();
    assertEquals(
        one.getTotalLength() + two.getTotalLength() + three.getTotalLength(), buffer.limit());
    ByteArrayInputStream in = new ByteArrayInputStream(buffer.array(), 0, buffer.limit());
    Message in1, in2, in3;
    in1 = read(in);
    in2 = read(in);
    in3 = read(in);
    assertEquals(-1, in.read());

    assertEquals(buffer(one), buffer(in1));
    assertEquals(buffer(two), buffer(in2));
    assertEquals(buffer(three), buffer(in3));
    assertEquals(buffer(one), buffer(SENT.next()));
    assertEquals(buffer(two), buffer(SENT.next()));
    assertEquals(buffer(three), buffer(SENT.next()));

    assertFalse(SINK.interested());
    assertEquals(3, STATS.getSent());
  }
示例#3
0
  public void send(ByteArrayInputStream sendBytes) throws SocketException {
    // A byte array representing a data packet
    byte[] packet;

    // While there are more than 127 bytes still to send ...
    while (sendBytes.available() > 127) {
      // ... create a 128 byte packet, ...
      packet = new byte[128];

      // ... set the header to 255, ...
      packet[0] = (byte) 255;

      // ... read 127 bytes from the sequence of bytes to send, ...
      sendBytes.read(packet, 1, 127);

      // ... and send the packet to the external process.
      try {
        output.write(packet);
        output.flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // Create a packet for any remaining data
    packet = new byte[sendBytes.available() + 1];

    // Set the header appropriately
    packet[0] = (byte) (sendBytes.available());

    // Read the remaining bytes into the packet
    sendBytes.read(packet, 1, sendBytes.available());

    // Send the packet to the external process
    try {
      output.write(packet);
      output.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#4
0
  /** Processes an incoming SNMP v3 message. */
  protected void ProcessIncomingMessage(AsnDecoder rpdu, ByteArrayInputStream in)
      throws DecodingException, IOException {
    byte[] bu = null;
    // need to duplicate the message for V3 to rewrite
    int nb = in.available();
    bu = new byte[nb];
    in.read(bu);
    in = new ByteArrayInputStream(bu);

    AsnSequence asnTopSeq = rpdu.DecodeSNMPv3(in);
    int msgId = rpdu.getMsgId(asnTopSeq);
    Integer rid = (Integer) msgIdHash.get(new Integer(msgId));
    if (rid != null) {
      if (AsnObject.debug > 6) {
        System.out.println(
            "SnmpContextv3.ProcessIncomingMessage(): msgId=" + msgId + ", Pdu reqId=" + rid);
      }
      Pdu pdu = getPdu(rid);
      try {
        AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, bu);
        if (pduSeq != null) {
          // got a message
          Integer rid2 = new Integer(pduSeq.getReqId());
          if (AsnObject.debug > 6) {
            System.out.println("SnmpContextv3.ProcessIncomingMessage():" + " rid2=" + rid2);
          }

          Pdu newPdu = null;
          if (rid2.intValue() != rid.intValue()) {
            newPdu = getPdu(rid2);
            if (AsnObject.debug > 3) {
              System.out.println(
                  "ProcessIncomingMessage(): "
                      + "pduReqId of msgId ("
                      + rid.intValue()
                      + ") != pduReqId of Pdu ("
                      + rid2.intValue()
                      + ")");
            }
            if (newPdu == null) {
              if (AsnObject.debug > 3) {
                System.out.println(
                    "ProcessIncomingMessage(): "
                        + "Using pduReqId of msgId ("
                        + rid.intValue()
                        + ")");
              }
            }
          }

          if (newPdu != null) {
            pdu = newPdu;
          }
        } else {
          if (AsnObject.debug > 6) {
            System.out.println("SnmpContextv3.ProcessIncomingMessage():" + " pduSeq is null.");
          }
        }

        if (pdu != null) {
          pdu.fillin(pduSeq);
        } else {
          if (AsnObject.debug > 6) {
            System.out.println("ProcessIncomingMessage(): No Pdu with reqid " + rid.intValue());
          }
        }
      } catch (DecodingException exc) {
        if (pdu != null) {
          pdu.setErrorStatus(AsnObject.SNMP_ERR_DECODING_EXC, exc);
          pdu.fillin(null);
        } else {
          throw exc;
        }
      }
    } else {
      if (AsnObject.debug > 3) {
        System.out.println("Pdu of msgId " + msgId + " is already answered");
      }
      rid = new Integer(-1);
    }
  }