예제 #1
0
 public static ByteBuffer doDecompress(ByteBuffer buffer, int length) {
   byte[] byteArrayIn = new byte[1024];
   ByteArrayInputStream byteIn;
   if (buffer.hasArray()) {
     byteIn =
         new ByteArrayInputStream(
             buffer.array(), buffer.position() + buffer.arrayOffset(), buffer.remaining());
   } else {
     byte[] array = new byte[buffer.limit() - buffer.position()];
     buffer.get(array);
     byteIn = new ByteArrayInputStream(array);
   }
   ByteBuffer retBuff = ByteBuffer.allocate(length);
   int len = 0;
   try {
     GZIPInputStream in = new GZIPInputStream(byteIn);
     while ((len = in.read(byteArrayIn)) > 0) {
       retBuff.put(byteArrayIn, 0, len);
     }
     in.close();
   } catch (IOException e) {
     s_logger.error("Fail to decompress the request!", e);
   }
   retBuff.flip();
   return retBuff;
 }
  @Override
  public String getData() {
    if (httpMessage == null) {
      return "";
    }

    if (HttpHeader.GZIP.equals(
        httpMessage.getResponseHeader().getHeader(HttpHeader.CONTENT_ENCODING))) {
      // Uncompress gziped content
      try {
        ByteArrayInputStream bais =
            new ByteArrayInputStream(httpMessage.getResponseBody().getBytes());
        GZIPInputStream gis = new GZIPInputStream(bais);
        InputStreamReader isr = new InputStreamReader(gis);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
          sb.append(line);
        }
        br.close();
        isr.close();
        gis.close();
        bais.close();
        return sb.toString();
      } catch (IOException e) {
        // this.log.error(e.getMessage(), e);
        System.out.println(e);
      }
    }

    return httpMessage.getResponseBody().toString();
  }
  public byte[] handleResponse(HttpResponse hr) throws ClientProtocolException, IOException {
    if (hr.getStatusLine().getStatusCode() == 200) {
      if (hr.getEntity().getContentEncoding() == null) {
        return EntityUtils.toByteArray(hr.getEntity());
      }

      if (hr.getEntity().getContentEncoding().getValue().contains("gzip")) {
        GZIPInputStream gis = null;
        try {
          gis = new GZIPInputStream(hr.getEntity().getContent());
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          byte[] b = new byte[1024];
          int n;
          while ((n = gis.read(b)) != -1) baos.write(b, 0, n);
          System.out.println("Gzipped");
          return baos.toByteArray();
        } catch (IOException e) {
          throw e;
        } finally {
          try {
            if (gis != null) gis.close();
          } catch (IOException ex) {
            throw ex;
          }
        }
      }
      return EntityUtils.toByteArray(hr.getEntity());
    }

    if (hr.getStatusLine().getStatusCode() == 302) {
      throw new IOException("302");
    }
    return null;
  }
예제 #4
0
  @Override
  public String getResponseBodyAsString() throws IOException {
    GZIPInputStream gzin;
    if (getResponseBody() != null || getResponseStream() != null) {

      if (getResponseHeader("Content-Encoding") != null
          && getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1) {
        // For GZip response
        InputStream is = getResponseBodyAsStream();
        gzin = new GZIPInputStream(is);

        InputStreamReader isr = new InputStreamReader(gzin, getResponseCharSet());
        java.io.BufferedReader br = new java.io.BufferedReader(isr);
        StringBuffer sb = new StringBuffer();
        String tempbf;
        while ((tempbf = br.readLine()) != null) {
          sb.append(tempbf);
          sb.append("\r\n");
        }
        isr.close();
        gzin.close();
        return sb.toString();
      } else {
        // For deflate response
        return super.getResponseBodyAsString();
      }
    } else {
      return null;
    }
  }
예제 #5
0
  @Test
  public void testJavaDataApiUtils()
      throws InvalidReferenceException, DataTransferException, IOException {
    JavaDataApiUtils util = new JavaDataApiUtils(service);
    String dfid = "URN:LSID:caarray.nci.nih.gov:gov.nih.nci.caarray.external.v1_0.data.File:1";
    CaArrayEntityReference fileRef = new CaArrayEntityReference(dfid);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // test gziped
    util.copyFileContentsToOutputStream(fileRef, true, stream);
    ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray());
    GZIPInputStream zin = new GZIPInputStream(in);
    assertFalse(-1 == zin.read());

    // test non gzip
    stream.reset();
    util.copyFileContentsToOutputStream(fileRef, false, stream);
    in = new ByteArrayInputStream(stream.toByteArray());
    try {
      zin = new GZIPInputStream(in);
      zin.read();
      fail("expected an IOException");
    } catch (IOException e) {
      assertEquals("Not in GZIP format", e.getMessage());
    }
  }
예제 #6
0
  @SuppressWarnings("unchecked")
  private HashMap<String, Integer> loadWinPoints() {
    String filename = "winpoints";
    HashMap<String, Integer> out = null;
    if (!new File(pl.getDataFolder(), filename).exists()) {
      HashMap<String, Integer> newHM = new HashMap<String, Integer>();
      saveWinPoints(newHM);
    }
    try {
      FileInputStream fin = new FileInputStream(new File(pl.getDataFolder(), filename));
      GZIPInputStream gzin = new GZIPInputStream(fin);
      ObjectInputStream in = new ObjectInputStream(gzin);
      Object o = in.readObject();
      if (!(o instanceof HashMap<?, ?>)) {

        pl.getLogger().severe("Fatal Error! Winpoints file was corrupted!");
        out = new HashMap<String, Integer>();
      } else {
        out = (HashMap<String, Integer>) o;
      }
      in.close();
      gzin.close();
      fin.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return out;
  }
예제 #7
0
  protected void setResult(HttpURLConnection conn, ByteArrayOutputStream output)
      throws IOException {

    setResponseHeader(conn);

    byte[] ret = output.toByteArray();
    // 没有数据返回
    if (ret == null || ret.length == 0) {
      responseData = null;
      return;
    }

    final HashMap<String, String> mResponseHeader = getResponseHeader();
    if (null != mResponseHeader) {
      String encode = mResponseHeader.get("content-encoding");

      if (null != encode && encode.contains("gzip")) {
        ByteArrayInputStream input = new ByteArrayInputStream(ret);
        ByteArrayOutputStream tmpOutput = new ByteArrayOutputStream(BUFFERSIZE);

        GZIPInputStream gin = new GZIPInputStream(input);
        int count;
        byte data[] = new byte[BUFFERSIZE];
        while ((count = gin.read(data, 0, BUFFERSIZE)) != -1) {
          tmpOutput.write(data, 0, count);
        }
        gin.close();

        ret = tmpOutput.toByteArray();
      }
    }

    responseData = ret;
  }
예제 #8
0
  /**
   * Decodes data from Base64 notation, automatically detecting gzip-compressed data and
   * decompressing it.
   *
   * @param s the string to decode
   * @return the decoded data
   * @since 1.4
   */
  public static byte[] decode(String s) {
    byte[] bytes;
    try {
      bytes = s.getBytes(PREFERRED_ENCODING);
    } // end try
    catch (java.io.UnsupportedEncodingException uee) {
      bytes = s.getBytes();
    } // end catch
    // </change>

    // Decode
    bytes = decode(bytes, 0, bytes.length);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)
    if (bytes != null && bytes.length >= 4) {

      int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
      if (java.util.zip.GZIPInputStream.GZIP_MAGIC == head) {
        java.io.ByteArrayInputStream bais = null;
        java.util.zip.GZIPInputStream gzis = null;
        java.io.ByteArrayOutputStream baos = null;
        byte[] buffer = new byte[2048];
        int length = 0;

        try {
          baos = new java.io.ByteArrayOutputStream();
          bais = new java.io.ByteArrayInputStream(bytes);
          gzis = new java.util.zip.GZIPInputStream(bais);

          while ((length = gzis.read(buffer)) >= 0) {
            baos.write(buffer, 0, length);
          } // end while: reading input

          // No error? Get new bytes.
          bytes = baos.toByteArray();

        } // end try
        catch (java.io.IOException e) {
          // Just return originally-decoded bytes
        } // end catch
        finally {
          try {
            baos.close();
          } catch (Exception e) {
          }
          try {
            gzis.close();
          } catch (Exception e) {
          }
          try {
            bais.close();
          } catch (Exception e) {
          }
        } // end finally
      } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
  } // end decode
예제 #9
0
  public static String unGzipBytesToString(InputStream in) {

    try {
      PushbackInputStream pis = new PushbackInputStream(in, 2);
      byte[] signature = new byte[2];
      pis.read(signature);
      pis.unread(signature);
      int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00));
      if (head != GZIPInputStream.GZIP_MAGIC) {
        return new String(toByteArray(pis), "UTF-8").trim();
      }
      GZIPInputStream gzip = new GZIPInputStream(pis);
      byte[] readBuf = new byte[8 * 1024];
      ByteArrayOutputStream outputByte = new ByteArrayOutputStream();
      int readCount = 0;
      do {
        readCount = gzip.read(readBuf);
        if (readCount > 0) {
          outputByte.write(readBuf, 0, readCount);
        }
      } while (readCount > 0);
      closeQuietly(gzip);
      closeQuietly(pis);
      closeQuietly(in);
      if (outputByte.size() > 0) {
        return new String(outputByte.toByteArray(), "UTF-8");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
예제 #10
0
  @SuppressWarnings("unused")
  private String unzipBody(InputStream str) {
    InputStreamReader sr = null;
    StringWriter writer = null;
    GZIPInputStream gis = null;
    try {
      char[] buffer = new char[10240];
      gis = new GZIPInputStream(str);
      sr = new InputStreamReader(gis, "UTF-8");
      writer = new StringWriter();

      for (int i = 0; (i = sr.read(buffer)) > 0; ) {
        writer.write(buffer, 0, i);
      }

      return writer.toString();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        if (writer != null) writer.close();
        if (sr != null) sr.close();
        if (gis != null) gis.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return null;
  }
예제 #11
0
    @Override
    public PointSet load(String pointSetId) throws Exception {

      File cachedFile;

      if (!workOffline) {
        // get pointset metadata from S3
        cachedFile = new File(POINT_DIR, pointSetId + ".json");
        if (!cachedFile.exists()) {
          POINT_DIR.mkdirs();

          S3Object obj = s3.getObject(pointsetBucket, pointSetId + ".json.gz");
          ObjectMetadata objMet = obj.getObjectMetadata();
          FileOutputStream fos = new FileOutputStream(cachedFile);
          GZIPInputStream gis = new GZIPInputStream(obj.getObjectContent());
          try {
            ByteStreams.copy(gis, fos);
          } finally {
            fos.close();
            gis.close();
          }
        }
      } else cachedFile = new File(POINT_DIR, pointSetId + ".json");

      // grab it from the cache

      return PointSet.fromGeoJson(cachedFile);
    }
 /**
  * gzip 解壓縮
  *
  * @param value
  * @return
  */
 public static byte[] ungzip(byte[] value) {
   byte[] result = new byte[0];
   ByteArrayInputStream in = null;
   GZIPInputStream gin = null;
   ByteArrayOutputStream out = null;
   try {
     //
     in = new ByteArrayInputStream(value);
     gin = new GZIPInputStream(in, BUFFER_LENGTH);
     out = new ByteArrayOutputStream();
     //
     byte[] buff = new byte[BUFFER_LENGTH];
     int read = 0;
     while ((read = gin.read(buff)) > -1) {
       out.write(buff, 0, read);
     }
     //
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     IoHelper.close(in);
     IoHelper.close(gin);
     IoHelper.close(out);
   }
   return result;
 }
예제 #13
0
  public void WritingToUnzippedFile(String fileName) throws IOException {

    // open the input (compressed) file.
    FileInputStream stream = new FileInputStream(fileName);
    FileOutputStream output = null;
    try {
      // open the gziped file to decompress.
      GZIPInputStream gzipstream = new GZIPInputStream(stream);
      byte[] buffer = new byte[2048];

      // create the output file without the .gz extension.
      String outname = fileName.substring(0, fileName.length() - 3);
      output = new FileOutputStream(outname);

      // and copy it to a new file
      int len;
      while ((len = gzipstream.read(buffer)) > 0) {
        output.write(buffer, 0, len);
      }
    } finally {
      // both streams must always be closed.
      if (output != null) output.close();
      stream.close();
    }
  }
예제 #14
0
파일: FileIO.java 프로젝트: Rubusch/java
 /**
  * Ctor.
  *
  * @param sz The filename.
  */
 public gunzip(String sz) {
   String zipname, source;
   if (sz.toLowerCase().endsWith(".gz")) {
     zipname = sz;
     source = zipname.substring(0, zipname.length() - 3);
   } else {
     zipname = sz + ".gz";
     source = sz;
   }
   GZIPInputStream gzis = null;
   OutputStream fos = null;
   try {
     gzis = new GZIPInputStream(new FileInputStream(zipname));
     fos = new FileOutputStream(source);
     byte[] buffer = new byte[BLOCKSIZE];
     for (int length; (length = gzis.read(buffer, 0, BLOCKSIZE)) != -1; )
       fos.write(buffer, 0, length);
   } catch (IOException e) {
     System.out.println("Error: Couldn't decompress " + sz);
   } finally {
     if (fos != null)
       try {
         fos.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     if (gzis != null)
       try {
         gzis.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
   }
 }
예제 #15
0
  /**
   * Returns an gunzipped copy of the input array, truncated to <code>sizeLimit</code> bytes, if
   * necessary. If the gzipped input has been truncated or corrupted, a best-effort attempt is made
   * to unzip as much as possible. If no data can be extracted <code>null</code> is returned.
   */
  public static final byte[] unzipBestEffort(byte[] in, int sizeLimit) {
    try {
      // decompress using GZIPInputStream
      ByteArrayOutputStream outStream =
          new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

      GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in));

      byte[] buf = new byte[BUF_SIZE];
      int written = 0;
      while (true) {
        try {
          int size = inStream.read(buf);
          if (size <= 0) break;
          if ((written + size) > sizeLimit) {
            outStream.write(buf, 0, sizeLimit - written);
            break;
          }
          outStream.write(buf, 0, size);
          written += size;
        } catch (Exception e) {
          break;
        }
      }
      try {
        outStream.close();
      } catch (IOException e) {
      }

      return outStream.toByteArray();

    } catch (IOException e) {
      return null;
    }
  }
예제 #16
0
  public static void unZip(String inputFile) {
    // inputFile = inputFile.substring(0, inputFile.length() - 3);
    try {

      String inFilename = inputFile;
      System.out.println("Opening the gzip file........		.................. :  opened");

      GZIPInputStream gzipInputStream = null;
      FileInputStream fileInputStream = null;
      gzipInputStream = new GZIPInputStream(new FileInputStream(inFilename));
      System.out.println("Opening the output file..		........... : opened");

      String outFilename = inFilename + ".out";
      OutputStream out = new FileOutputStream(outFilename);
      System.out.println(
          "Transferring bytes from the 	compressed file to the output file........:     Transfer successful");

      byte[] buf = new byte[1024]; // size can be 		changed according to programmer's need.
      int len;
      while ((len = gzipInputStream.read(buf)) != -1) {
        out.write(buf, 0, len);
      }
      gzipInputStream.close();
      out.close();
    } catch (IOException e) {
      System.out.println("Exception has been thrown" + e);
    }
  }
  public byte[] decompress(byte[] buffer) throws IOException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    // Open the compressed stream
    GZIPInputStream gzip = new GZIPInputStream(inputStream);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
      // Transfer bytes from the compressed stream to the output stream
      byte[] buf = new byte[unit];
      int len;
      while ((len = gzip.read(buf)) > 0) {
        out.write(buf, 0, len);
      }

      return out.toByteArray();
    } catch (IOException e) {
      throw e;
    } finally {
      // Close the file and stream
      gzip.close();
      out.close();
      inputStream.close();
    }
  }
예제 #18
0
  public static ExpandedResult processGzipEncoded(byte[] compressed, int sizeLimit)
      throws IOException {

    ByteArrayOutputStream outStream =
        new ByteArrayOutputStream(EXPECTED_GZIP_COMPRESSION_RATIO * compressed.length);
    GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(compressed));

    boolean isTruncated = false;
    byte[] buf = new byte[BUF_SIZE];
    int written = 0;
    while (true) {
      try {
        int size = inStream.read(buf);
        if (size == -1) {
          break;
        }

        if ((written + size) > sizeLimit) {
          isTruncated = true;
          outStream.write(buf, 0, sizeLimit - written);
          break;
        }

        outStream.write(buf, 0, size);
        written += size;
      } catch (Exception e) {
        LOGGER.trace("Exception unzipping content", e);
        break;
      }
    }

    IoUtils.safeClose(outStream);
    return new ExpandedResult(outStream.toByteArray(), isTruncated);
  }
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   byte[] body =
       "gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip "
           .getBytes("UTF-8");
   resp.setStatus(HttpServletResponse.SC_OK);
   if (containsHeader(req, "Content-Encoding", "gzip")) {
     GZIPInputStream gzipInputStream = new GZIPInputStream(req.getInputStream());
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     int byteCount = 0;
     byte[] buffer = new byte[4096];
     int bytesRead = -1;
     while ((bytesRead = gzipInputStream.read(buffer)) != -1) {
       byteArrayOutputStream.write(buffer, 0, bytesRead);
       byteCount += bytesRead;
     }
     byteArrayOutputStream.flush();
     gzipInputStream.close();
     assertEquals("Content length does not match", body.length, byteCount);
     assertTrue("Invalid body", Arrays.equals(byteArrayOutputStream.toByteArray(), body));
   } else {
     byte[] decompressedBody = FileCopyUtils.copyToByteArray(req.getInputStream());
     assertTrue("Invalid body", Arrays.equals(decompressedBody, body));
   }
 }
예제 #20
0
 private byte[] getBuffer(File f) throws Exception {
   if (!f.exists()) return null;
   byte[] buffer = new byte[(int) f.length()];
   DataInputStream dis = new DataInputStream(new FileInputStream(f));
   dis.readFully(buffer);
   dis.close();
   byte[] gzipInputBuffer = new byte[999999];
   int bufferlength = 0;
   GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(buffer));
   do {
     if (bufferlength == gzipInputBuffer.length) {
       System.out.println("Error inflating data.\nGZIP buffer overflow.");
       break;
     }
     int readByte =
         gzip.read(gzipInputBuffer, bufferlength, gzipInputBuffer.length - bufferlength);
     if (readByte == -1) break;
     bufferlength += readByte;
   } while (true);
   byte[] inflated = new byte[bufferlength];
   System.arraycopy(gzipInputBuffer, 0, inflated, 0, bufferlength);
   buffer = inflated;
   if (buffer.length < 10) return null;
   return buffer;
 }
예제 #21
0
  public OnDemandData getNextNode() {
    OnDemandData onDemandData;
    synchronized (aClass19_1358) {
      onDemandData = (OnDemandData) aClass19_1358.popHead();
    }
    if (onDemandData == null) return null;
    synchronized (nodeSubList) {
      onDemandData.unlist();
    }
    if (onDemandData.buffer == null) return onDemandData;
    int i = 0;
    try {
      GZIPInputStream gzipinputstream =
          new GZIPInputStream(new ByteArrayInputStream(onDemandData.buffer));
      do {
        if (i == gzipInputBuffer.length) throw new RuntimeException("buffer overflow!");
        int k = gzipinputstream.read(gzipInputBuffer, i, gzipInputBuffer.length - i);
        if (k == -1) break;
        i += k;
      } while (true);
    } catch (IOException _ex) {
      throw new RuntimeException("error unzipping");
    }
    onDemandData.buffer = new byte[i];
    System.arraycopy(gzipInputBuffer, 0, onDemandData.buffer, 0, i);

    return onDemandData;
  }
 /**
  * Decompress a compressed event string.
  *
  * @param str Compressed string
  * @return Decompressed string
  */
 private String decompress(String str) {
   ByteArrayInputStream byteInputStream = null;
   GZIPInputStream gzipInputStream = null;
   BufferedReader br = null;
   try {
     byteInputStream = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(str));
     gzipInputStream = new GZIPInputStream(byteInputStream);
     br = new BufferedReader(new InputStreamReader(gzipInputStream, CharEncoding.UTF_8));
     StringBuilder jsonStringBuilder = new StringBuilder();
     String line;
     while ((line = br.readLine()) != null) {
       jsonStringBuilder.append(line);
     }
     return jsonStringBuilder.toString();
   } catch (IOException e) {
     throw new RuntimeException(
         "Error occured while decompressing events string: " + e.getMessage(), e);
   } finally {
     try {
       if (byteInputStream != null) {
         byteInputStream.close();
       }
       if (gzipInputStream != null) {
         gzipInputStream.close();
       }
       if (br != null) {
         br.close();
       }
     } catch (IOException e) {
       log.error("Error occured while closing streams: " + e.getMessage(), e);
     }
   }
 }
예제 #23
0
 private byte[] ungzip5(final byte[] gzip) throws IOException {
   int size = 0;
   int counter = 0;
   GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzip));
   int bytesRead = 0;
   byte[] buffer = new byte[32768];
   byte[] tempBuffer = new byte[4096];
   counter = 0;
   while (bytesRead != -1) {
     bytesRead = gzipInputStream.read(tempBuffer);
     if (bytesRead != -1) {
       if (buffer.length < counter + bytesRead) {
         byte[] newBuffer = new byte[buffer.length + 32768];
         System.arraycopy(buffer, 0, newBuffer, 0, counter);
         buffer = newBuffer;
       }
       System.arraycopy(tempBuffer, 0, buffer, counter, bytesRead);
       counter += bytesRead;
     }
   }
   gzipInputStream.close();
   size = counter;
   byte[] unzipped = new byte[size];
   System.arraycopy(buffer, 0, unzipped, 0, counter);
   return unzipped;
 }
예제 #24
0
  public ByteArrayHub(long key, byte unitSize, int maxUnits, File basePath) {
    this.unitSize = unitSize;
    this.maxUnits = maxUnits;
    this.key = key;

    unitOffset = maxUnits * unitSize;

    path = new File(basePath, Long.toHexString(key).toUpperCase() + ".acache");
    data = new byte[unitOffset * ByteArrayCache.CACHE_MAX_SIZE + ByteArrayCache.HEADER_SIZE];
    returnCache = new byte[unitSize * maxUnits];
    if (path.exists()) {
      GZIPInputStream inStream;
      try {
        inStream = new GZIPInputStream(new FileInputStream(path));
        byte[] readBuffer = new byte[1024];
        int len;
        int offset = 0;
        while ((len = inStream.read(readBuffer)) != -1) {
          System.arraycopy(readBuffer, 0, data, offset, len);
          offset += len;
        }
        // Log.i("@@@@@ " + inStream.read(data));
        inStream.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
예제 #25
0
 public static String getStringFromCompressedFile(File file) throws IOException {
   GZIPInputStream in = new GZIPInputStream(new FileInputStream(file));
   try {
     return getStringFromInputStream(in);
   } finally {
     in.close();
   }
 }
예제 #26
0
 String getContent(HttpMethod httpMethod) {
   StringBuilder contentBuilder = new StringBuilder();
   if (isZipContent(httpMethod)) {
     InputStream is = null;
     GZIPInputStream gzin = null;
     InputStreamReader isr = null;
     BufferedReader br = null;
     try {
       is = httpMethod.getResponseBodyAsStream();
       gzin = new GZIPInputStream(is);
       isr =
           new InputStreamReader(
               gzin,
               ((HttpMethodBase) httpMethod).getResponseCharSet()); // ���ö�ȡ���ı����ʽ���Զ������
       br = new BufferedReader(isr);
       char[] buffer = new char[4096];
       int readlen = -1;
       while ((readlen = br.read(buffer, 0, 4096)) != -1) {
         contentBuilder.append(buffer, 0, readlen);
       }
     } catch (Exception e) {
       log.error("Unzip fail", e);
     } finally {
       try {
         br.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         isr.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         gzin.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         is.close();
       } catch (Exception e1) {
         // ignore
       }
     }
   } else {
     String content = null;
     try {
       content = httpMethod.getResponseBodyAsString();
     } catch (Exception e) {
       log.error("Fetch config error:", e);
     }
     if (null == content) {
       return null;
     }
     contentBuilder.append(content);
   }
   return contentBuilder.toString();
 }
예제 #27
0
 /**
  * Decompress GZIP (RFC 1952) compressed data
  *
  * @param compressedData
  * @return A string containing the decompressed data
  * @throws IOException
  */
 public static String decompressGzip(byte[] compressedData) throws IOException {
   byte[] buffer = new byte[compressedData.length];
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(compressedData));
   for (int bytesRead = 0; bytesRead != -1; bytesRead = in.read(buffer)) {
     out.write(buffer, 0, bytesRead);
   }
   return new String(out.toByteArray(), "UTF-8");
 }
  /** Uncompress a GZIP file. */
  private static File getGzFile(FileObject in, File out, boolean isTar) throws IOException {

    // Stream buffer
    final int BUFF_SIZE = 8192;
    final byte[] buffer = new byte[BUFF_SIZE];

    GZIPInputStream inputStream = null;
    FileOutputStream outStream = null;

    try {
      inputStream = new GZIPInputStream(new FileInputStream(in.getPath()));
      outStream = new FileOutputStream(out);

      if (isTar) {
        // Read Tar header
        int remainingBytes = readTarHeader(inputStream);

        // Read content
        ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE);
        byte[] tmpCache = new byte[BUFF_SIZE];
        int nRead, nGet;
        while ((nRead = inputStream.read(tmpCache)) != -1) {
          if (nRead == 0) {
            continue;
          }
          bb.put(tmpCache);
          bb.position(0);
          bb.limit(nRead);
          while (bb.hasRemaining() && remainingBytes > 0) {
            nGet = Math.min(bb.remaining(), BUFF_SIZE);
            nGet = Math.min(nGet, remainingBytes);
            bb.get(buffer, 0, nGet);
            outStream.write(buffer, 0, nGet);
            remainingBytes -= nGet;
          }
          bb.clear();
        }
      } else {
        int len;
        while ((len = inputStream.read(buffer)) > 0) {
          outStream.write(buffer, 0, len);
        }
      }
    } catch (IOException ex) {
      Exceptions.printStackTrace(ex);
    } finally {
      if (inputStream != null) {
        inputStream.close();
      }
      if (outStream != null) {
        outStream.close();
      }
    }

    return out;
  }
 public static byte[] gzDecompress(byte[] b) throws IOException {
   GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(b));
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   byte[] buf = new byte[1024];
   int len;
   while ((len = gzi.read(buf)) > 0) {
     out.write(buf, 0, len);
   }
   out.close();
   return out.toByteArray();
 }
예제 #30
0
  /**
   * 数据解压缩
   *
   * @param is
   * @param os
   * @throws Exception
   */
  public static void decompress(InputStream is, OutputStream os) throws Exception {

    GZIPInputStream gis = new GZIPInputStream(is);
    int count;
    byte data[] = new byte[BUFFER];
    while ((count = gis.read(data, 0, BUFFER)) != -1) {
      os.write(data, 0, count);
    }

    gis.close();
  }