is = socket.getInputStream();
String line = new String(readInputStream(is, 0));
String[] tokens = line.split(" ");
if (tokens == null || tokens.length == 0) {
throw new BeanstalkException("no response");
}
response = new Response();
response.setResponseLine(line);
String status = tokens[0];
response.setStatus(status);
if (tokens.length > 1) {
response.setReponse(tokens[1]);
}
setState(request, response, status);
switch (request.getExpectedResponse()) {
case Map:
if (response.isMatchError()) {
break;
}
response.setData(parseForMap(is));
break;
case List:
response.setData(parseForList(is));
break;
case ByteArray:
if (response.isMatchError()) {
break;
}
int length = 0;
if (request.getExpectedDataLengthIndex() > 0 && tokens.length > request.getExpectedDataLengthIndex()) {
try {
length = Integer.parseInt(tokens[request.getExpectedDataLengthIndex()]);
} catch (NumberFormatException ex) {
length = 0;
}
}
byte[] data = readInputStream(is, length);
response.setData(data);
break;
default:
break;
}
} catch (Exception e) {
throw new BeanstalkException(e.getMessage());
}
return response;
}