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(); } }
/** 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); } }