donky.microsoft.aspnet.signalr.client.http.StreamResponse Maven / Gradle / Ivy
/*
Copyright (c) Microsoft Open Technologies, Inc.
All Rights Reserved
See License.txt in the project root for license information.
*/
package donky.microsoft.aspnet.signalr.client.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import donky.microsoft.aspnet.signalr.client.Constants;
/**
* Response implementation based on an InputStream
*/
public class StreamResponse implements Response {
private BufferedReader mReader;
private int mStatus;
private InputStream mOriginalStream;
Map> mHeaders;
/**
* Initializes the StreamResponse
*
* @param stream
* stream to read
* @param status
* HTTP status code
*/
public StreamResponse(InputStream stream, int status, Map> headers) {
mOriginalStream = stream;
mReader = new BufferedReader(new InputStreamReader(mOriginalStream, Constants.UTF8));
mHeaders = new HashMap>(headers);
mStatus = status;
}
public byte[] readAllBytes() throws IOException {
List bytes = new ArrayList();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = mOriginalStream.read(buffer, 0, bufferSize);
while (bytesRead != -1) {
for (int i = 0; i < bytesRead; i++) {
bytes.add(buffer[i]);
}
bytesRead = mOriginalStream.read(buffer, 0, bufferSize);
}
byte[] byteArray = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) {
byteArray[i] = bytes.get(i).byteValue();
}
return byteArray;
}
@Override
public String readToEnd() throws IOException {
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = mReader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
}
@Override
public int getStatus() {
return mStatus;
}
@Override
public String readLine() throws IOException {
return mReader.readLine();
}
@Override
public Map> getHeaders() {
return new HashMap>(mHeaders);
}
@Override
public List getHeader(String headerName) {
return mHeaders.get(headerName);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy