zlibmodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 751 行 · 第 1/2 页
JAVA
751 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Charles Reich */package com.caucho.quercus.lib.zlib;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.file.BinaryInput;import com.caucho.quercus.lib.file.BinaryOutput;import com.caucho.quercus.lib.file.BinaryStream;import com.caucho.quercus.lib.file.FileModule;import com.caucho.quercus.lib.OutputModule;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.L10N;import com.caucho.vfs.StreamImplOutputStream;import com.caucho.vfs.TempBuffer;import com.caucho.vfs.TempStream;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.io.InputStream;import java.util.logging.Level;import java.util.logging.Logger;import java.util.zip.Adler32;import java.util.zip.Deflater;import java.util.zip.Inflater;import java.util.zip.InflaterInputStream;/** * PHP Zlib */public class ZlibModule extends AbstractQuercusModule { private static final Logger log = Logger.getLogger(ZlibModule.class.getName()); private static final L10N L = new L10N(ZlibModule.class); public static final int FORCE_GZIP = 0x1; public static final int FORCE_DEFLATE = 0x2; public String []getLoadedExtensions() { return new String[] { "zlib" }; } /** * * @param env * @param fileName * @param mode * @param useIncludePath always on * @return Zlib */ @ReturnNullAsFalse public static BinaryStream gzopen(Env env, StringValue fileName, String mode, @Optional("false") boolean useIncludePath) { String filemode = getFileMode(mode); int compressionLevel = getCompressionLevel(mode); int compressionStrategy = getCompressionStrategy(mode); Object val = FileModule.fopen(env, fileName, mode, useIncludePath, null); if (val == null) return null; try { int ch = filemode.charAt(0); if (ch == 'r') { BinaryInput is = (BinaryInput) val; return new ZlibInputStream(env, is); } else if (ch == 'w') { return new ZlibOutputStream(((BinaryOutput) val).getOutputStream(), compressionLevel, compressionStrategy); } else if (ch == 'a') { return new ZlibOutputStream(((BinaryOutput) val).getOutputStream(), compressionLevel, compressionStrategy); } else if (ch == 'x') { return new ZlibOutputStream(((BinaryOutput) val).getOutputStream(), compressionLevel, compressionStrategy); } } catch (IOException e) { log.log(Level.FINE, e.getMessage(), e); env.warning(L.l(e.getMessage())); } return null; } /** * * @param env * @param fileName * @param useIncludePath * @return array of uncompressed lines from fileName */ @ReturnNullAsFalse public static ArrayValue gzfile(Env env, StringValue fileName, @Optional boolean useIncludePath) { BinaryInput is = (BinaryInput) gzopen(env, fileName, "r", useIncludePath); if (is == null) return null; try { ArrayValue result = new ArrayValueImpl(); StringValue line; while ((line = is.readLine(Integer.MAX_VALUE)) != null && line.length() > 0) result.put(line); return result; } catch (IOException e) { throw new QuercusModuleException(e); } finally { is.close(); } } public static Value ob_gzhandler(Env env, StringValue buffer, int state) { return OutputModule.ob_gzhandler(env, buffer, state); } /** * outputs uncompressed bytes directly to browser, writes a warning message * if an error has occured * Note: PHP5 is supposed to print an error message but it doesn't do it * * @param env * @param fileName * @param useIncludePath * @return number of bytes read from file, or FALSE if an error occurred */ public static Value readgzfile(Env env, StringValue fileName, @Optional boolean useIncludePath) { BinaryInput is = (BinaryInput) gzopen(env, fileName, "r", useIncludePath); if (is == null) return BooleanValue.FALSE; try { return LongValue.create(env.getOut().writeStream(is.getInputStream())); } catch (IOException e) { throw new QuercusModuleException(e); } finally { is.close(); } } /** * Writes a string to the gzip stream. */ public static int gzwrite(@NotNull BinaryOutput os, InputStream is, @Optional("0x7fffffff") int length) { if (os == null) return 0; try { return os.write(is, length); } catch (IOException e) { throw new QuercusModuleException(e); } } /** * * @param env * @param zp * @param s * @param length * @return alias of gzwrite */ public int gzputs(Env env, @NotNull BinaryOutput os, InputStream is, @Optional("0x7ffffff") int length) { if (os == null) return 0; try { return os.write(is, length); } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Closes the stream. */ public boolean gzclose(@NotNull BinaryStream os) { if (os == null) return false; os.close(); return true; } /** * Returns true if the GZip stream is ended. */ public boolean gzeof(@NotNull BinaryStream binaryStream) { if (binaryStream == null) return true; return binaryStream.isEOF(); } /** * Reads a character from the stream. */ public static Value gzgetc(Env env, @NotNull BinaryInput is) { if (is == null) return BooleanValue.FALSE; try { int ch = is.read(); if (ch < 0) return BooleanValue.FALSE; else { StringValue sb = env.createBinaryBuilder(1); sb.appendByte(ch); return sb; } } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Reads a chunk of data from the gzip stream. */ @ReturnNullAsFalse public StringValue gzread(@NotNull BinaryInput is, int length) { if (is == null) return null; try { return is.read(length); } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Reads a line from the input stream. */ public static Value gzgets(Env env, @NotNull BinaryInput is, int length) { return FileModule.fgets(env, is, length); } /** * Reads a line from the zip stream, stripping tags. */ public static Value gzgetss(Env env, @NotNull BinaryInput is, int length, @Optional String allowedTags) { return FileModule.fgetss(env, is, length, allowedTags); } /** * Rewinds the stream to the very beginning */ public boolean gzrewind(@NotNull BinaryStream binaryStream) { if (binaryStream == null) return false; return binaryStream.setPosition(0); } /** * Set stream position to the offset * @param offset absolute position to set stream to * @param whence if set, changes the interpretation of offset like fseek * @return 0 upon success, else -1 for error */ public int gzseek(@NotNull BinaryStream binaryStream, long offset, @Optional("FileModule.SEEK_SET") int whence) { if (binaryStream == null) return -1; if (binaryStream.seek(offset, whence) == -1) return -1; return 0; } /** * Gets the current position in the stream * @return the position in the stream, or FALSE for error */ public Value gztell(@NotNull BinaryStream binaryStream) { if (binaryStream == null) return BooleanValue.FALSE; return new LongValue(binaryStream.getPosition()); } /** * Prints out the remaining data in the stream to stdout */ public Value gzpassthru(Env env, @NotNull BinaryInput is) { WriteStream out = env.getOut(); TempBuffer tempBuf = TempBuffer.allocate(); byte[] buffer = tempBuf.getBuffer(); int length = 0; try { int sublen = is.read(buffer, 0, buffer.length); while (sublen > 0) { out.write(buffer, 0, sublen); length += sublen; sublen = is.read(buffer, 0, buffer.length);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?