use of java.util.zip.GZIPInputStream in project jersey by jersey.
the class InterceptorGzipTest method testGzipInterceptorOnlyOnServer.
@Test
public void testGzipInterceptorOnlyOnServer() throws IOException {
client().register(GZIPWriterTestInterceptor.class);
WebTarget target = target().path("test");
String entity = "hello, this is text entity";
Response response = target.request().put(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
InputStream is = response.readEntity(InputStream.class);
GZIPInputStream gzipIs = new GZIPInputStream(is);
BufferedReader br = new BufferedReader(new InputStreamReader(gzipIs));
String str = br.readLine();
assertEquals(entity + FROM_RESOURCE, str);
}
use of java.util.zip.GZIPInputStream in project FastDev4Android by jiangqqlmj.
the class IoUtils method unGZip.
/**
* GZip解压
*
* @param bContent
* @return
*/
public static byte[] unGZip(byte[] bContent) {
byte[] data = new byte[1024];
try {
ByteArrayInputStream in = new ByteArrayInputStream(bContent);
GZIPInputStream pIn = new GZIPInputStream(in);
DataInputStream objIn = new DataInputStream(pIn);
int len = 0;
int count = 0;
while ((count = objIn.read(data, len, len + 1024)) != -1) {
len = len + count;
}
byte[] trueData = new byte[len];
System.arraycopy(data, 0, trueData, 0, len);
objIn.close();
pIn.close();
in.close();
return trueData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of java.util.zip.GZIPInputStream in project JessMA by ldcsaa.
the class HttpHelper method unGZip.
/** GZip 解压 */
public static final byte[] unGZip(byte[] bytes) throws IOException {
byte[] buffer = new byte[4096];
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
GZIPInputStream gzip = new GZIPInputStream(bis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int r;
while ((r = gzip.read(buffer, 0, buffer.length)) != -1) baos.write(buffer, 0, r);
return baos.toByteArray();
} finally {
try {
if (baos != null)
baos.close();
if (gzip != null)
gzip.close();
if (bis != null)
bis.close();
} catch (IOException e) {
}
}
}
use of java.util.zip.GZIPInputStream in project KJFrameForAndroid by kymjs.
the class HttpUtils method responseToBytes.
public static byte[] responseToBytes(KJHttpResponse response) throws IOException, KJHttpException {
PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(ByteArrayPool.get(), (int) response.getContentLength());
byte[] buffer = null;
try {
InputStream in = response.getContentStream();
if (isGzipContent(response) && !(in instanceof GZIPInputStream)) {
in = new GZIPInputStream(in);
}
if (in == null) {
throw new KJHttpException("服务器连接异常");
}
buffer = ByteArrayPool.get().getBuf(1024);
int count;
while ((count = in.read(buffer)) != -1) {
bytes.write(buffer, 0, count);
}
return bytes.toByteArray();
} finally {
try {
// Close the InputStream and release the resources by
// "consuming the content".
// entity.consumeContent();
response.getContentStream().close();
} catch (IOException e) {
// This can happen if there was an exception above that left the
// entity in
// an invalid state.
KJLoger.debug("Error occured when calling consumingContent");
}
ByteArrayPool.get().returnBuf(buffer);
bytes.close();
}
}
use of java.util.zip.GZIPInputStream in project c-geo by just-radovan.
the class cgBase method requestJSON.
public String requestJSON(String scheme, String host, String path, String method, String params) {
int httpCode = -1;
String httpLocation = null;
if (method == null) {
method = "GET";
} else {
method = method.toUpperCase();
}
boolean methodPost = false;
if (method.equalsIgnoreCase("POST")) {
methodPost = true;
}
URLConnection uc = null;
HttpURLConnection connection = null;
Integer timeout = 30000;
final StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 3; i++) {
if (i > 0) {
Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
}
buffer.delete(0, buffer.length());
timeout = 30000 + (i * 15000);
try {
try {
URL u = null;
if (methodPost) {
u = new URL(scheme + host + path);
} else {
u = new URL(scheme + host + path + "?" + params);
}
if (u.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) u.openConnection();
https.setHostnameVerifier(doNotVerify);
uc = https;
} else {
uc = (HttpURLConnection) u.openConnection();
}
uc.setRequestProperty("Host", host);
uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
if (methodPost) {
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uc.setRequestProperty("Content-Length", Integer.toString(params.length()));
uc.setRequestProperty("X-HTTP-Method-Override", "GET");
} else {
uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
}
uc.setRequestProperty("X-Requested-With", "XMLHttpRequest");
connection = (HttpURLConnection) uc;
connection.setReadTimeout(timeout);
connection.setRequestMethod(method);
// TODO: Fix these (FilCab)
HttpURLConnection.setFollowRedirects(false);
connection.setDoInput(true);
if (methodPost) {
connection.setDoOutput(true);
final OutputStream out = connection.getOutputStream();
final OutputStreamWriter wr = new OutputStreamWriter(out);
wr.write(params);
wr.flush();
wr.close();
} else {
connection.setDoOutput(false);
}
final String encoding = connection.getContentEncoding();
InputStream ins;
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
ins = new GZIPInputStream(connection.getInputStream());
} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
} else {
ins = connection.getInputStream();
}
final InputStreamReader inr = new InputStreamReader(ins);
final BufferedReader br = new BufferedReader(inr);
readIntoBuffer(br, buffer);
httpCode = connection.getResponseCode();
final String paramsLog = params.replaceAll(passMatch, "password=***");
Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog);
connection.disconnect();
br.close();
ins.close();
inr.close();
} catch (IOException e) {
httpCode = connection.getResponseCode();
Log.e(cgSettings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": " + connection.getResponseMessage() + " ~ " + e.toString());
}
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeoBase.requestJSON: " + e.toString());
}
if (buffer != null && buffer.length() > 0) {
break;
}
if (httpCode == 403) {
// we're not allowed to download content, so let's move
break;
}
}
String page = null;
if (httpCode == 302 && httpLocation != null) {
final Uri newLocation = Uri.parse(httpLocation);
if (newLocation.isRelative() == true) {
page = requestJSONgc(host, path, params);
} else {
page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params);
}
} else {
page = replaceWhitespace(buffer);
}
if (page != null) {
return page;
} else {
return "";
}
}
Aggregations