zlibmodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 751 行 · 第 1/2 页

JAVA
751
字号
      }      return new LongValue(length);    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);      return BooleanValue.FALSE;    } finally {      TempBuffer.free(tempBuf);    }  }  /**   * Returns the encoding type both allowed by the server   *   and supported by the user's browser.   */  public Value zlib_get_coding_type(Env env)  {    String ini = env.getIniString("zlib.output_compression");    if (ini == null || ini == "")      return BooleanValue.FALSE;    //zlib_get_coding_type can also be an integer > 0    if (! ini.equalsIgnoreCase("on")) {      int ch = ini.charAt(0);      if (ch < '0' || ch > '9')        return BooleanValue.FALSE;    }    ServerArrayValue sav = new ServerArrayValue(env);    Value val = sav.get(env.createString("HTTP_ACCEPT_ENCODING"));    if (!val.isset())      return BooleanValue.FALSE;    String s = val.toString();    if (s.contains("gzip"))      return env.createString("gzip");    else if(s.contains("deflate"))      return env.createString("deflate");    else      return BooleanValue.FALSE;  }  /**   * compresses data using zlib   *   * @param data   * @param level (default is Deflater.DEFAULT_COMPRESSION)   * @return compressed string   */  public Value gzcompress(Env env,			  InputStream data,                          @Optional("6") int level)  {    TempBuffer tempBuf = TempBuffer.allocate();    byte []buffer = tempBuf.getBuffer();    try {      Deflater deflater = new Deflater(level, true);      Adler32 crc = new Adler32();      boolean isFinished = false;      StringValue out = env.createLargeBinaryBuilder();      buffer[0] = (byte) 0x78;      if (level <= 1)        buffer[1] = (byte) 0x01;      else if (level < 6)        buffer[1] = (byte) 0x5e;      else if (level == 6)        buffer[1] = (byte) 0x9c;      else        buffer[1] = (byte) 0xda;      out.append(buffer, 0, 2);      int len;      while (! isFinished) {        while (! isFinished && deflater.needsInput()) {          len = data.read(buffer, 0, buffer.length);          if (len > 0) {            crc.update(buffer, 0, len);            deflater.setInput(buffer, 0, len);          }          else {            isFinished = true;            deflater.finish();          }        }        while ((len = deflater.deflate(buffer, 0, buffer.length)) > 0) {          out.append(buffer, 0, len);        }      }      long value = crc.getValue();          buffer[0] = (byte) (value >> 24);      buffer[1] = (byte) (value >> 16);      buffer[2] = (byte) (value >> 8);      buffer[3] = (byte) (value >> 0);      out.append(buffer, 0, 4);      return out;    } catch (Exception e) {      throw QuercusModuleException.create(e);    } finally {      TempBuffer.free(tempBuf);    }  }  /**   *   * @param data   * @param length (maximum length of string returned)   * @return uncompressed string   */  public Value gzuncompress(Env env,			    InputStream is,                            @Optional("0") long length)  {    TempBuffer tempBuf = TempBuffer.allocate();    byte []buffer = tempBuf.getBuffer();    try {      if (length == 0)        length = Long.MAX_VALUE;      InflaterInputStream in = new InflaterInputStream(is);      StringValue sb = env.createLargeBinaryBuilder();      int len;      while ((len = in.read(buffer, 0, buffer.length)) >= 0) {        sb.append(buffer, 0, len);      }      in.close();      return sb;    } catch (Exception e) {      throw QuercusModuleException.create(e);    } finally {      TempBuffer.free(tempBuf);    }  }  private int _dbg;    /**   *   * @param level   * @return compressed using DEFLATE algorithm   */  public Value gzdeflate(Env env, InputStream data,                         @Optional("6") int level)  {    TempBuffer tempBuf = TempBuffer.allocate();    byte []buffer = tempBuf.getBuffer();    try {      Deflater deflater = new Deflater(level, true);      boolean isFinished = false;      TempStream out = new TempStream();      int len;      while (! isFinished) {        if (! isFinished && deflater.needsInput()) {          len = data.read(buffer, 0, buffer.length);          if (len > 0)            deflater.setInput(buffer, 0, len);          else {            isFinished = true;            deflater.finish();          }        }        while ((len = deflater.deflate(buffer, 0, buffer.length)) > 0) {          out.write(buffer, 0, len, false);        }      }      deflater.end();      return env.createBinaryString(out.getHead());    } catch (Exception e) {      throw QuercusModuleException.create(e);    } finally {      TempBuffer.free(tempBuf);    }  }  /**   * @param data compressed using Deflate algorithm   * @param length of data to decompress   *   * @return uncompressed string   */  public Value gzinflate(Env env,                         InputStream data,                         @Optional("0") int length)  {    if (length <= 0)      length = Integer.MAX_VALUE;        TempBuffer tempBuf = TempBuffer.allocate();    byte []buffer = tempBuf.getBuffer();    try {      Inflater inflater = new Inflater(true);      StringValue sb = env.createBinaryBuilder();      while (true) {        int sublen = Math.min(length, buffer.length);                  sublen = data.read(buffer, 0, sublen);        if (sublen > 0) {          inflater.setInput(buffer, 0, sublen);          length -= sublen;                    int inflatedLength;          while ((inflatedLength = inflater.inflate(buffer, 0, sublen)) > 0) {            sb.append(buffer, 0, inflatedLength);          }        }        else          break;      }      inflater.end();      return sb;    } catch (Exception e) {      env.warning(e);      return BooleanValue.FALSE;    } finally {      TempBuffer.free(tempBuf);    }  }  /**   *   * Compresses data using the Deflate algorithm, output is   * compatible with gzwrite's output   *   * @param data compressed with the Deflate algorithm   * @param level Deflate compresion level [0-9]   * @param encodingMode CRC32 trailer is not written if encoding mode   *    is FORCE_DEFLATE, default is to write CRC32   * @return StringValue with gzip header and trailer   */  public Value gzencode(Env env, InputStream is,                        @Optional("6") int level,                        @Optional("1") int encodingMode)  {    TempBuffer tempBuf = TempBuffer.allocate();    byte[] buffer = tempBuf.getBuffer();    TempStream ts = new TempStream();    StreamImplOutputStream out = new StreamImplOutputStream(ts);    try {      ZlibOutputStream gzOut;      gzOut = new ZlibOutputStream(out, level,				   Deflater.DEFAULT_STRATEGY,				   encodingMode);      int len;      while ((len = is.read(buffer, 0, buffer.length)) > 0) {        gzOut.write(buffer, 0, len);      }      gzOut.close();      StringValue sb = env.createBinaryBuilder();      for (TempBuffer ptr = ts.getHead(); ptr != null; ptr = ptr.getNext())	sb.append(ptr.getBuffer(), 0, ptr.getLength());      return sb;    } catch(IOException e) {      throw QuercusModuleException.create(e);    } finally {      TempBuffer.free(tempBuf);      ts.destroy();    }  }  /**   * Helper function to retrieve the filemode closest to the end   * Note: PHP5 unexpectedly fails when 'x' is the mode.   *   * XXX todo: toss a warning if '+' is found   *    (gzip cannot be open for both reading and writing at the same time)   *   */  private static String getFileMode(String input)  {    String modifier = "";    String filemode = input.substring(0, 1);    for (int i = 1; i < input.length(); i++ )    {      char ch = input.charAt(i);      switch (ch) {        case 'r':          filemode = "r";          break;        case 'w':          filemode = "w";          break;        case 'a':          filemode = "a";          break;        case 'b':          modifier = "b";          break;        case 't':          modifier = "t";          break;      }    }    return filemode + modifier;  }  /**   * Helper function to retrieve the compression level   *     - finds the compression level nearest to the end and returns that   */  private static int getCompressionLevel(String input)  {    for (int i = input.length() - 1; i >= 0; i--) {      char ch = input.charAt(i);            if (ch >= '0' && ch <= '9')        return ch - '0';    }        return Deflater.DEFAULT_COMPRESSION;  }  /**   * Helper function to retrieve the compression strategy.   *     - finds the compression strategy nearest to the end and returns that   */  private static int getCompressionStrategy(String input)  {    for (int i = input.length() - 1; i >= 0; i--) {      char ch = input.charAt(i);            switch (ch) {      case 'f':        return Deflater.FILTERED;	      case 'h':        return Deflater.HUFFMAN_ONLY;      }    }        return Deflater.DEFAULT_STRATEGY;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?