Program: 3999
Project: 3999
Buggy source:
/** 
 * Frames and sends a piece of data according to the HyBi WebSocket protocol.
 * @throws Exception 
 * @api private
 */
private boolean frameAndSend(int opcode,Object data,boolean finalFragment,boolean maskData,WriteCB cb) throws Exception {
  Log.d(TAG,"String_Node_Str");
  boolean canModifyData=false;
  boolean out=false;
  if (data == null) {
    ByteBuffer tbw;
    if (maskData) {
      tbw=ByteBuffer.allocate(6);
      tbw.put((byte)(opcode | (finalFragment ? 0x80 : 0)));
      tbw.put((byte)(0 | (maskData ? 0x80 : 0)));
      tbw.putInt(0);
    }
 else {
      tbw=ByteBuffer.allocate(2);
      tbw.put((byte)(opcode | (finalFragment ? 0x80 : 0)));
      tbw.put((byte)(0 | (maskData ? 0x80 : 0)));
    }
    tbw.flip();
    try {
      out=this._socket.write(tbw,null,cb);
    }
 catch (    Exception e) {
      if (cb != null)       cb.writeDone(e.toString());
 else       this.emit("String_Node_Str",e.toString());
    }
    return out;
  }
  if (!Util.isBuffer(data)) {
    if (Util.isString(data)) {
      canModifyData=true;
      data=Util.chunkToBuffer(data,"String_Node_Str");
    }
 else {
      if (cb != null)       cb.writeDone("String_Node_Str");
 else       this.emit("String_Node_Str","String_Node_Str");
      return out;
    }
  }
  Log.d(TAG,"String_Node_Str");
  int dataLength=Util.chunkByteLength(data,null);
  int dataOffset=maskData ? 6 : 2;
  int secondByte=dataLength;
  if (dataLength >= 65536) {
    dataOffset+=8;
    secondByte=127;
  }
 else   if (dataLength > 125) {
    dataOffset+=2;
    secondByte=126;
  }
  boolean mergeBuffers=dataLength < 32768 || (maskData && !canModifyData);
  int totalLength=mergeBuffers ? dataLength + dataOffset : dataOffset;
  ByteBuffer outputBuffer=ByteBuffer.allocate(totalLength);
  outputBuffer.put(0,(byte)(finalFragment ? opcode | 0x80 : opcode));
switch (secondByte) {
case 126:
    outputBuffer.order(ByteOrder.BIG_ENDIAN).putShort(2,(short)dataLength);
  break;
case 127:
outputBuffer.order(ByteOrder.BIG_ENDIAN).putInt(2,0);
outputBuffer.order(ByteOrder.BIG_ENDIAN).putInt(6,dataLength);
break;
}
if (maskData) {
outputBuffer.put(1,(byte)(secondByte | 0x80));
byte[] mask=this._randomMask != null ? this._randomMask : (this._randomMask=getRandomMask());
outputBuffer.put(dataOffset - 4,mask[0]);
outputBuffer.put(dataOffset - 3,mask[1]);
outputBuffer.put(dataOffset - 2,mask[2]);
outputBuffer.put(dataOffset - 1,mask[3]);
if (mergeBuffers) {
BufferUtil.mask((ByteBuffer)data,mask,outputBuffer,dataOffset,dataLength);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
out=this._socket.write(outputBuffer,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
 else {
BufferUtil.mask((ByteBuffer)data,mask,(ByteBuffer)data,0,dataLength);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
this._socket.write(outputBuffer,null,null);
BufferUtil.renewBuffer((ByteBuffer)data);
Log.d(TAG,"String_Node_Str" + (ByteBuffer)data);
out=this._socket.write(data,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
}
 else {
outputBuffer.put(1,(byte)secondByte);
if (mergeBuffers) {
ByteBuffer tfc=(ByteBuffer)data;
BufferUtil.fastCopy(tfc.capacity(),tfc,outputBuffer,dataOffset);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
out=this._socket.write(outputBuffer,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
 else {
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
this._socket.write(outputBuffer,null,null);
BufferUtil.renewBuffer((ByteBuffer)data);
Log.d(TAG,"String_Node_Str" + (ByteBuffer)data);
out=this._socket.write(data,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
}
Log.d(TAG,"String_Node_Str");
return out;
}

Fixed version (ground truth):
/** 
 * Frames and sends a piece of data according to the HyBi WebSocket protocol.
 * @throws Exception 
 * @api private
 */
private boolean frameAndSend(int opcode,Object data,boolean finalFragment,boolean maskData,WriteCB cb) throws Exception {
  Log.d(TAG,"String_Node_Str" + opcode + "String_Node_Str"+ data+ "String_Node_Str"+ maskData);
  if (data != null && data instanceof ByteBuffer) {
    ByteBuffer bd=(ByteBuffer)data;
    String dstr="String_Node_Str";
    for (int i=0; i < bd.capacity(); i++)     dstr+="String_Node_Str" + bd.get(i);
    Log.d(TAG,dstr);
  }
  boolean canModifyData=false;
  boolean out=false;
  if (data == null) {
    ByteBuffer tbw;
    if (maskData) {
      tbw=ByteBuffer.allocate(6);
      tbw.put((byte)(opcode | (finalFragment ? 0x80 : 0)));
      tbw.put((byte)(0 | (maskData ? 0x80 : 0)));
      tbw.putInt(0);
    }
 else {
      tbw=ByteBuffer.allocate(2);
      tbw.put((byte)(opcode | (finalFragment ? 0x80 : 0)));
      tbw.put((byte)(0 | (maskData ? 0x80 : 0)));
    }
    tbw.flip();
    try {
      out=this._socket.write(tbw,null,cb);
    }
 catch (    Exception e) {
      if (cb != null)       cb.writeDone(e.toString());
 else       this.emit("String_Node_Str",e.toString());
    }
    return out;
  }
  if (!Util.isBuffer(data)) {
    if (Util.isString(data)) {
      canModifyData=true;
      data=Util.chunkToBuffer(data,"String_Node_Str");
    }
 else {
      if (cb != null)       cb.writeDone("String_Node_Str");
 else       this.emit("String_Node_Str","String_Node_Str");
      return out;
    }
  }
  Log.d(TAG,"String_Node_Str" + data.toString());
  int dataLength=Util.chunkByteLength(data,null);
  int dataOffset=maskData ? 6 : 2;
  int secondByte=dataLength;
  if (dataLength >= 65536) {
    dataOffset+=8;
    secondByte=127;
  }
 else   if (dataLength > 125) {
    dataOffset+=2;
    secondByte=126;
  }
  boolean mergeBuffers=dataLength < 32768 || (maskData && !canModifyData);
  int totalLength=mergeBuffers ? dataLength + dataOffset : dataOffset;
  ByteBuffer outputBuffer=ByteBuffer.allocate(totalLength);
  outputBuffer.put(0,(byte)(finalFragment ? opcode | 0x80 : opcode));
switch (secondByte) {
case 126:
    outputBuffer.order(ByteOrder.BIG_ENDIAN).putShort(2,(short)dataLength);
  break;
case 127:
outputBuffer.order(ByteOrder.BIG_ENDIAN).putInt(2,0);
outputBuffer.order(ByteOrder.BIG_ENDIAN).putInt(6,dataLength);
break;
}
if (maskData) {
outputBuffer.put(1,(byte)(secondByte | 0x80));
byte[] mask=this._randomMask != null ? this._randomMask : (this._randomMask=getRandomMask());
outputBuffer.put(dataOffset - 4,mask[0]);
outputBuffer.put(dataOffset - 3,mask[1]);
outputBuffer.put(dataOffset - 2,mask[2]);
outputBuffer.put(dataOffset - 1,mask[3]);
if (mergeBuffers) {
BufferUtil.mask((ByteBuffer)data,mask,outputBuffer,dataOffset,dataLength);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
out=this._socket.write(outputBuffer,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
 else {
BufferUtil.mask((ByteBuffer)data,mask,(ByteBuffer)data,0,dataLength);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
this._socket.write(outputBuffer,null,null);
BufferUtil.renewBuffer((ByteBuffer)data);
Log.d(TAG,"String_Node_Str" + (ByteBuffer)data);
out=this._socket.write(data,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
}
 else {
outputBuffer.put(1,(byte)secondByte);
if (mergeBuffers) {
ByteBuffer tfc=(ByteBuffer)data;
BufferUtil.fastCopy(tfc.capacity(),tfc,outputBuffer,dataOffset);
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
out=this._socket.write(outputBuffer,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
 else {
try {
BufferUtil.renewBuffer(outputBuffer);
Log.d(TAG,"String_Node_Str" + outputBuffer);
this._socket.write(outputBuffer,null,null);
BufferUtil.renewBuffer((ByteBuffer)data);
Log.d(TAG,"String_Node_Str" + (ByteBuffer)data);
out=this._socket.write(data,null,cb);
}
 catch (Exception e) {
if (cb != null) cb.writeDone(e.toString());
 else this.emit("String_Node_Str",e.toString());
}
}
}
Log.d(TAG,"String_Node_Str");
return out;
}