break;
}
// Receive a single answer
byte[] resp = receiveMessage(34000);
TypesReader tr = new TypesReader(resp);
int t = tr.readByte();
listener.read(Packet.forName(t));
// Search the pending queue
OutstandingReadRequest req = pendingReadQueue.remove(tr.readUINT32());
if (null == req)
{
throw new RequestMismatchException();
}
// Evaluate the answer
if (t == Packet.SSH_FXP_STATUS)
{
/* In any case, stop sending more packets */
int code = tr.readUINT32();
String msg = tr.readString();
listener.read(msg);
if (log.isDebugEnabled())
{
String[] desc = ErrorCodes.getDescription(code);
log.debug("Got SSH_FXP_STATUS (" + req.req_id + ") (" + ((desc != null) ? desc[0] : "UNKNOWN") + ")");
}
// Flag to read all pending requests but don't send any more.
errorOccured = true;
if (pendingReadQueue.isEmpty())
{
if (ErrorCodes.SSH_FX_EOF == code)
{
return -1;
}
throw new SFTPException(msg, code);
}
}
else if (t == Packet.SSH_FXP_DATA)
{
// OK, collect data
int readLen = tr.readUINT32();
if ((readLen < 0) || (readLen > req.len))
{
throw new IOException("The server sent an invalid length field in a SSH_FXP_DATA packet.");
}
if (log.isDebugEnabled())
{
log.debug("Got SSH_FXP_DATA (" + req.req_id + ") " + req.serverOffset + "/" + readLen
+ " (requested: " + req.len + ")");
}
// Read bytes into buffer
tr.readBytes(req.buffer, req.dstOffset, readLen);
if (readLen < req.len)
{
/* Send this request packet again to request the remaing data in this slot. */
req.req_id = generateNextRequestID();