コード例 #1
0
 /**
  * inflate stream 解壓縮
  *
  * @param value
  * @return
  */
 public static byte[] inflate(byte[] value) {
   byte[] result = new byte[0];
   //
   InflaterInputStream inflater = null;
   ByteArrayInputStream in = null;
   ByteArrayOutputStream out = null;
   try {
     in = new ByteArrayInputStream(value);
     out = new ByteArrayOutputStream();
     inflater = new InflaterInputStream(in);
     //
     byte[] buff = new byte[BUFFER_LENGTH];
     int read = 0;
     while ((read = inflater.read(buff)) > -1) {
       out.write(buff, 0, read);
     }
     //
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     IoHelper.close(in);
     IoHelper.close(inflater);
     IoHelper.close(out);
   }
   return result;
 }
コード例 #2
0
ファイル: Picture.java プロジェクト: BGCX261/zkpoi-svn-to-git
  private void fillImageContent() {
    byte[] rawContent = getRawContent();

    // HACK: Detect compressed images.  In reality there should be some way to determine
    //       this from the first 32 bytes, but I can't see any similarity between all the
    //       samples I have obtained, nor any similarity in the data block contents.
    if (matchSignature(rawContent, COMPRESSED1, 32)
        || matchSignature(rawContent, COMPRESSED2, 32)) {
      try {
        InflaterInputStream in =
            new InflaterInputStream(
                new ByteArrayInputStream(rawContent, 33, rawContent.length - 33));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int readBytes;
        while ((readBytes = in.read(buf)) > 0) {
          out.write(buf, 0, readBytes);
        }
        content = out.toByteArray();
      } catch (IOException e) {
        // Problems reading from the actual ByteArrayInputStream should never happen
        // so this will only ever be a ZipException.
        log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
      }
    } else {
      // Raw data is not compressed.
      content = rawContent;
    }
  }
コード例 #3
0
ファイル: ShowPackDelta.java プロジェクト: haochenx/jgit
  private static byte[] getDelta(ObjectReader reader, RevObject obj)
      throws IOException, MissingObjectException, StoredObjectRepresentationNotAvailableException {
    ObjectReuseAsIs asis = (ObjectReuseAsIs) reader;
    ObjectToPack target = asis.newObjectToPack(obj, obj.getType());

    PackWriter pw =
        new PackWriter(reader) {
          @Override
          public void select(ObjectToPack otp, StoredObjectRepresentation next) {
            otp.select(next);
          }
        };

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    asis.selectObjectRepresentation(
        pw, NullProgressMonitor.INSTANCE, Collections.singleton(target));
    asis.copyObjectAsIs(new PackOutputStream(NullProgressMonitor.INSTANCE, buf, pw), target, true);

    // At this point the object header has no delta information,
    // because it was output as though it were a whole object.
    // Skip over the header and inflate.
    //
    byte[] bufArray = buf.toByteArray();
    int ptr = 0;
    while ((bufArray[ptr] & 0x80) != 0) ptr++;
    ptr++;

    @SuppressWarnings("resource" /* java 7 */)
    TemporaryBuffer.Heap raw = new TemporaryBuffer.Heap(bufArray.length);
    InflaterInputStream inf =
        new InflaterInputStream(new ByteArrayInputStream(bufArray, ptr, bufArray.length));
    raw.copy(inf);
    inf.close();
    return raw.toByteArray();
  }
コード例 #4
0
ファイル: EncodingUtils.java プロジェクト: simudream/bixo
  public static byte[] processDeflateEncoded(byte[] compressed, int sizeLimit) throws IOException {
    ByteArrayOutputStream outStream =
        new ByteArrayOutputStream(EXPECTED_DEFLATE_COMPRESSION_RATIO * compressed.length);

    // "true" because HTTP does not provide zlib headers
    Inflater inflater = new Inflater(true);
    InflaterInputStream inStream =
        new InflaterInputStream(new ByteArrayInputStream(compressed), inflater);

    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) {
        LOGGER.trace("Exception inflating content", e);
        break;
      }
    }

    IoUtils.safeClose(outStream);
    return outStream.toByteArray();
  }
コード例 #5
0
ファイル: EInputStream.java プロジェクト: eriksoe/erjang
  public byte[] read_size_and_inflate() throws IOException {
    final int size = read4BE();
    final byte[] buf = new byte[size];
    final java.util.zip.Inflater inflater = new java.util.zip.Inflater();
    final java.util.zip.InflaterInputStream is =
        new java.util.zip.InflaterInputStream(this, inflater);

    try {
      int pos = 0;
      while (pos < size) { // Inflate fully
        final int dsize = is.read(buf, pos, size - pos);
        if (dsize == 0) break;
        pos += dsize;
      }
      if (pos < size) {
        throw new IOException("Decompression gave " + pos + " bytes, not " + size);
      }

      // Back up if the inflater read ahead:
      int read_ahead = inflater.getRemaining();
      setPos(getPos() - read_ahead);
    } finally {
      is.close();
    }

    return buf;
  }
コード例 #6
0
 /**
  * 解压缩Cookie
  *
  * @param cookie Cookie
  * @throws IOException
  */
 public static final void unCompressCookie(Cookie cookie) throws IOException {
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   if (StringUtils.isEmpty(cookie.getValue())) {
     return;
   }
   byte[] compress = new BASE64Decoder().decodeBuffer(cookie.getValue());
   InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(compress));
   try {
     byte[] b = new byte[1024];
     int count;
     while ((count = iis.read(b)) >= 0) {
       bos.write(b, 0, count);
     }
     iis.close();
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (ObjectUtils.isNotNull(iis)) {
       try {
         iis.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     if (ObjectUtils.isNotNull(bos)) {
       try {
         bos.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
コード例 #7
0
ファイル: Tools.java プロジェクト: nail/graylog2-server
 /**
  * Decompress ZLIB (RFC 1950) compressed data
  *
  * @param compressedData
  * @return A string containing the decompressed data
  * @throws IOException
  */
 public static String decompressZlib(byte[] compressedData) throws IOException {
   byte[] buffer = new byte[compressedData.length];
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   InflaterInputStream in = new InflaterInputStream(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");
 }
コード例 #8
0
ファイル: ZlibCodec.java プロジェクト: marcelomata/scifio
 @Override
 public byte[] decompress(final RandomAccessInputStream in, final CodecOptions options)
     throws FormatException, IOException {
   final InflaterInputStream i = new InflaterInputStream(in);
   final ByteVector bytes = new ByteVector();
   final byte[] buf = new byte[8192];
   int r = 0;
   // read until eof reached
   try {
     while ((r = i.read(buf, 0, buf.length)) > 0) bytes.add(buf, 0, r);
   } catch (final EOFException e) {
   }
   return bytes.toByteArray();
 }
コード例 #9
0
ファイル: PICT.java プロジェクト: pombredanne/STuneLite
 private byte[] read(byte[] data, int pos) throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   ByteArrayInputStream bis = new ByteArrayInputStream(data);
   Header header = new Header();
   header.read(data, pos);
   bis.skip(pos + header.getSize());
   InflaterInputStream inflater = new InflaterInputStream(bis);
   byte[] chunk = new byte[4096];
   int count;
   while ((count = inflater.read(chunk)) >= 0) {
     out.write(chunk, 0, count);
   }
   inflater.close();
   return out.toByteArray();
 }
コード例 #10
0
ファイル: CookieContainer.java プロジェクト: yinxujun/eterna
 /** 对被压缩的字符串进行解压操作. */
 private String doInflater(String str) {
   try {
     byte[] buf = ZIP_CODER.base64ToByteArray(str);
     ByteArrayInputStream byteIn = new ByteArrayInputStream(buf);
     InflaterInputStream in = new InflaterInputStream(byteIn);
     ByteArrayOutputStream byteOut = new ByteArrayOutputStream(str.length() * 3 + 128);
     Utility.copyStream(in, byteOut);
     in.close();
     byte[] result = byteOut.toByteArray();
     return new String(result, "UTF-8");
   } catch (IOException ex) {
     // 这里不会出现IO异常因为全是内存操作
     throw new Error();
   }
 }
コード例 #11
0
 /**
  * Closes this input stream and releases any system resources associated with the stream.
  *
  * @exception IOException if an I/O error has occurred
  */
 public void close() throws IOException {
   if (!closed) {
     super.close();
     eos = true;
     closed = true;
   }
 }
コード例 #12
0
  /**
   * Method declaration
   *
   * @throws SQLException
   */
  private void restoreBackup() throws SQLException {

    if (Trace.TRACE) {
      Trace.trace("not closed last time!");
    }

    if (!(new File(sFileBackup)).exists()) {

      // the backup don't exists because it was never made or is empty
      // the cache file must be deleted in this case
      (new File(sFileCache)).delete();

      return;
    }

    try {
      long time = 0;

      if (Trace.TRACE) {
        time = System.currentTimeMillis();
      }

      InflaterInputStream f =
          new InflaterInputStream(new FileInputStream(sFileBackup), new Inflater());
      FileOutputStream cache = new FileOutputStream(sFileCache);
      byte b[] = new byte[COPY_BLOCK_SIZE];

      while (true) {
        int l = f.read(b, 0, COPY_BLOCK_SIZE);

        if (l == -1) {
          break;
        }

        cache.write(b, 0, l);
      }

      cache.close();
      f.close();

      if (Trace.TRACE) {
        Trace.trace(time - System.currentTimeMillis());
      }
    } catch (Exception e) {
      throw Trace.error(Trace.FILE_IO_ERROR, sFileBackup);
    }
  }
コード例 #13
0
  /**
   * Decoding and deflating the encoded AuthReq
   *
   * @param encodedStr encoded AuthReq
   * @return decoded AuthReq
   */
  public static String decode(String encodedStr) throws SAMLSSOException {
    try {
      org.apache.commons.codec.binary.Base64 base64Decoder =
          new org.apache.commons.codec.binary.Base64();
      byte[] xmlBytes = encodedStr.getBytes("UTF-8");
      byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

      try {
        // TODO if the request came in POST, inflating is wrong
        Inflater inflater = new Inflater(true);
        inflater.setInput(base64DecodedByteArray);
        byte[] xmlMessageBytes = new byte[5000];
        int resultLength = inflater.inflate(xmlMessageBytes);

        if (inflater.getRemaining() > 0) {
          throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
        }

        inflater.end();
        String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");
        if (log.isDebugEnabled()) {
          log.debug("Request message " + decodedString);
        }
        return decodedString;

      } catch (DataFormatException e) {
        ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InflaterInputStream iis = new InflaterInputStream(bais);
        byte[] buf = new byte[1024];
        int count = iis.read(buf);
        while (count != -1) {
          baos.write(buf, 0, count);
          count = iis.read(buf);
        }
        iis.close();
        String decodedStr = new String(baos.toByteArray(), Charset.forName("UTF-8"));
        if (log.isDebugEnabled()) {
          log.debug("Request message " + decodedStr);
        }
        return decodedStr;
      }
    } catch (IOException e) {
      throw new SAMLSSOException("Error when decoding the SAML Request.", e);
    }
  }
コード例 #14
0
 public static String inflateCompressedString(byte[] data, int length) {
   try {
     final ByteArrayInputStream bais = new ByteArrayInputStream(data);
     final InflaterInputStream iis = new InflaterInputStream(bais);
     final byte uncompressed[] = new byte[length];
     int read;
     int offset = 0;
     while (length > 0 && (read = iis.read(uncompressed, offset, length)) != -1) {
       offset += read;
       length -= read;
     }
     iis.close();
     Assert.assertEquals(0, length);
     return new String(uncompressed, "UTF-8");
   } catch (final IOException e) {
     throw new AssertionError(e);
   }
 }
コード例 #15
0
 @Override
 public void close() throws IOException {
   if (closed) {
     return;
   }
   closed = true;
   inf.end();
   super.close();
 }
コード例 #16
0
ファイル: MapData.java プロジェクト: Chrishayes94/JEngine
  public static File decodeFile(String location) {
    try {
      FileInputStream fin = new FileInputStream(location);
      InflaterInputStream in = new InflaterInputStream(fin);

      FileOutputStream fout = new FileOutputStream("raw_maps/temporary.txt");

      int i;
      while ((i = in.read()) != -1) {
        fout.write((byte) i);
        fout.flush();
      }

      fin.close();
      fout.close();
      in.close();
      return new File("temporary.txt");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    return null;
  }
コード例 #17
0
ファイル: EMF.java プロジェクト: aw1621107/hacks
  @Override
  public byte[] getData() {
    try {
      byte[] rawdata = getRawData();

      ByteArrayOutputStream out = new ByteArrayOutputStream();
      InputStream is = new ByteArrayInputStream(rawdata);
      Header header = new Header();
      header.read(rawdata, CHECKSUM_SIZE);
      is.skip(header.getSize() + CHECKSUM_SIZE);

      InflaterInputStream inflater = new InflaterInputStream(is);
      byte[] chunk = new byte[4096];
      int count;
      while ((count = inflater.read(chunk)) >= 0) {
        out.write(chunk, 0, count);
      }
      inflater.close();
      return out.toByteArray();
    } catch (IOException e) {
      throw new HSLFException(e);
    }
  }
コード例 #18
0
 /**
  * Closes the zip file.
  *
  * @exception IOException if a i/o error occured.
  */
 public void close() throws IOException {
   super.close();
   crc = null;
   entry = null;
   entryAtEOF = true;
 }
コード例 #19
0
    @Override
    protected Download doInBackground(Download... downloads) {
      Download download = downloads[0];

      URL url;
      RandomAccessFile file = null;
      InflaterInputStream iis = null;

      try {
        url = new URL(AnkiDroidProxy.SYNC_URL + "fulldown");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        // FIXME: The connection always returns all bytes, regardless of what is indicated in range
        // property, so
        // resuming downloads of personal decks is not possible at the moment
        // Fix this when the connection is fixed on AnkiOnline
        // Log.i(AnkiDroidApp.TAG, "Range = " + download.getDownloaded());
        // connection.setRequestProperty("Range","bytes=" + download.getDownloaded() + "-");
        connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

        connection.connect();

        long startTime = System.currentTimeMillis();

        DataOutputStream ds = new DataOutputStream(connection.getOutputStream());
        String data =
            "p="
                + URLEncoder.encode(mPassword, "UTF-8")
                + "&u="
                + URLEncoder.encode(mUsername, "UTF-8")
                + "&d="
                + URLEncoder.encode(download.getTitle(), "UTF-8");
        ds.writeBytes(data);
        Log.i(AnkiDroidApp.TAG, "Closing streams...");
        ds.flush();
        ds.close();

        // Make sure response code is in the 200 range.
        if (connection.getResponseCode() / 100 != 2) {
          download.setStatus(Download.STATUS_ERROR);
          publishProgress();
        } else {
          download.setStatus(Download.STATUS_DOWNLOADING);
          publishProgress();
        }

        Log.i(AnkiDroidApp.TAG, "Response code = " + connection.getResponseCode());

        // Check for valid content length.
        Log.i(AnkiDroidApp.TAG, "Connection length = " + connection.getContentLength());
        int contentLength = connection.getContentLength();
        if (contentLength < 1) {
          Log.i(AnkiDroidApp.TAG, "Content Length = -1");
          // download.setStatus(Download.ERROR);
        }

        // Set the size for this download if it hasn't been already set
        if (download.getSize() == -1 && contentLength != -1) {
          download.setSize(contentLength);
          Log.i(AnkiDroidApp.TAG, "File size = " + contentLength);
        }

        // Open file
        file =
            new RandomAccessFile(mDestination + "/tmp/" + download.getTitle() + ".anki.tmp", "rw");
        // FIXME: Uncomment next line when the connection is fixed on AnkiOnline (= when the
        // connection only
        // returns the bytes specified on the range property)
        // file.seek(download.getDownloaded());

        iis = new InflaterInputStream(connection.getInputStream());

        while (download.getStatus() == Download.STATUS_DOWNLOADING) {
          // Size buffer according to how much of the file is left to download
          Log.v(AnkiDroidApp.TAG, "Downloading... " + download.getDownloaded());
          byte[] buffer;
          // if (size - downloaded > MAX_BUFFER_SIZE) {
          buffer = new byte[MAX_BUFFER_SIZE];
          // } else {
          // buffer = new byte[size - downloaded];
          // }

          // Read from server into buffer.
          int read = iis.read(buffer);
          if (read == -1) {
            break;
          }

          // Write buffer to file.
          file.write(buffer, 0, read);
          download.setDownloaded(download.getDownloaded() + read);
          publishProgress();
        }

        if (download.getStatus() == Download.STATUS_DOWNLOADING) {
          // Change status to complete if this point was reached because downloading has finished
          download.setStatus(Download.STATUS_COMPLETE);
          new File(mDestination + "/tmp/" + download.getTitle() + ".anki.tmp")
              .renameTo(new File(mDestination + "/" + download.getTitle() + ".anki"));
          long finishTime = System.currentTimeMillis();
          Log.i(AnkiDroidApp.TAG, "Finished in " + ((finishTime - startTime) / 1000) + " seconds!");
          Log.i(AnkiDroidApp.TAG, "Downloaded = " + download.getDownloaded());
        } else if (download.getStatus() == Download.STATUS_CANCELLED) {
          // Cancelled download, clean up
          new File(mDestination + "/tmp/" + download.getTitle() + ".anki.tmp").delete();
          Log.i(AnkiDroidApp.TAG, "Download cancelled.");
        }
        publishProgress();
        connection.disconnect();
      } catch (Exception e) {
        e.printStackTrace();
        Log.i(AnkiDroidApp.TAG, "Exception Error = " + e.getMessage());
        download.setStatus(Download.STATUS_ERROR);
        publishProgress();
      } finally {
        Log.i(AnkiDroidApp.TAG, "finally");
        // Close file
        if (file != null) {
          try {
            Log.i(AnkiDroidApp.TAG, "closing file");
            file.close();
          } catch (Exception e) {
            Log.i(AnkiDroidApp.TAG, "exception closing file");
          }
        }

        // Close connection to server
        if (iis != null) {
          try {
            Log.i(AnkiDroidApp.TAG, "closing iis");
            iis.close();
            Log.i(AnkiDroidApp.TAG, "closed iis");
          } catch (Exception e) {
            Log.i(AnkiDroidApp.TAG, "exception closing iis: " + e.getMessage());
          }
        }
      }

      return download;
    }