filemodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,844 行 · 第 1/5 页
JAVA
2,844 行
if (ch == quote) { for (ch = is.read(); ch >= 0; ch = is.read()) { if (ch == quote) { ch = is.read(); if (ch == quote) sb.append((char) ch); else break; } else sb.append((char) ch); } array.append(sb); for (; ch >= 0 && ch == ' ' || ch == '\t'; ch = is.read()) { } } else { for (; ch >= 0 && ch != comma && ch != '\r' && ch != '\n'; ch = is.read()) { sb.append((char) ch); } array.append(sb); } if (ch < 0) return array; else if (ch == '\n') return array; else if (ch == '\r') { is.readOptionalLinefeed(); return array; } else if (ch == comma) { } else { env.warning("expected comma"); } } } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Returns the next line */ public static Value fgets(Env env, @NotNull BinaryInput is, @Optional("0x7fffffff") int length) { // php/1615 try { if (is == null) return BooleanValue.FALSE; StringValue value = is.readLine(length); if (value != null) return value; else return BooleanValue.FALSE; } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Returns the next line stripping tags */ public static Value fgetss(Env env, BinaryInput is, @Optional("0x7fffffff") int length, @Optional String allowedTags) { // php/161a try { if (is == null) { env.warning(L.l("{0} is null", "handle")); return BooleanValue.FALSE; } StringValue value = is.readLine(length); if (value != null) return StringModule.strip_tags(value, allowedTags); else return BooleanValue.FALSE; } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Parses the file, returning it in an array. Binary-safe. * * @param filename the file's name * @param useIncludePath if 1, use the include path * @param context the resource context */ public static Value file(Env env, StringValue filename, @Optional boolean useIncludePath, @Optional Value context) { if (filename.length() == 0) return BooleanValue.FALSE; try { BinaryStream stream = fopen(env, filename, "r", useIncludePath, context); if (stream == null) return BooleanValue.FALSE; BinaryInput is = (BinaryInput) stream; ArrayValue result = new ArrayValueImpl(); try { while (true) { StringValue bb = env.createBinaryBuilder(); for (int ch = is.read(); ch >= 0; ch = is.read()) { if (ch == '\n') { bb.appendByte(ch); break; } else if (ch == '\r') { bb.appendByte('\r'); int ch2 = is.read(); if (ch2 == '\n') bb.appendByte('\n'); else is.unread(); break; } else bb.appendByte(ch); } if (bb.length() > 0) result.append(bb); else return result; } } finally { is.close(); } } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Returns the file access time * * @param path the path to check */ public static Value fileatime(Env env, Path path) { if (! path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } long time = path.getLastAccessTime(); if (time <= 24 * 3600 * 1000L) return BooleanValue.FALSE; else return new LongValue(time / 1000L); } /** * Returns the file create time * * @param path the path to check */ public static Value filectime(Env env, Path path) { if (! path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } long time = path.getCreateTime(); if (time <= 24 * 3600 * 1000L) return BooleanValue.FALSE; else return new LongValue(time / 1000L); } /** * Returns the file's group * * @param path the path to check */ public static Value filegroup(Env env, Path path) { if (! path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } return new LongValue(path.getGroup()); } /** * Returns the file's inocde * * @param path the path to check */ public static Value fileinode(Env env, Path path) { if (! path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } return new LongValue(path.getInode()); } /** * Returns the file modified time * * @param path the path to check */ public static Value filemtime(Env env, Path path) { long time = path.getLastModified(); if (24 * 3600 * 1000L < time) return new LongValue(time / 1000L); else { if (!path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } return BooleanValue.FALSE; } } /** * Returns the file's owner * * @param path the path to check */ public static Value fileowner(Env env, Path path) { if (!path.canRead()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } return new LongValue(path.getOwner()); } /** * Returns the file's permissions * * @param path the path to check */ public static Value fileperms(Env env, Path path) { return new LongValue(path.getMode()); } /** * Returns the file's size * * @param path the path to check */ public static Value filesize(Env env, Path path) { if (path == null) { env.warning(L.l("path cannot be read")); return BooleanValue.FALSE; } if (! path.exists() || ! path.isFile()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } long length = path.getLength(); if (length < 0) return BooleanValue.FALSE; else return new LongValue(length); } /** * Returns the file's type * * @param path the path to check */ public static Value filetype(Env env, @NotNull Path path) { if (path == null) return BooleanValue.FALSE; else if (! path.exists()) { env.warning(L.l("{0} cannot be read", path.getFullPath())); return BooleanValue.FALSE; } else if (path.isLink()) return env.createString("link"); else if (path.isDirectory()) return env.createString("dir"); else if (path.isFile()) return env.createString("file"); else if (path.isFIFO()) return env.createString("fifo"); else if (path.isBlockDevice()) return env.createString("block"); else if (path.isCharacterDevice()) return env.createString("char"); else return env.createString("unknown"); } /** * Returns true if file exists * * @param path the path to check */ public static boolean file_exists(@NotNull Path path) { if (path != null) return path.exists(); else return false; } /** * Parses the file, returning it as a string array. * * @param filename the file's name * @param useIncludePath if true, use the include path * @param context the resource context */ @ReturnNullAsFalse public static StringValue file_get_contents(Env env, StringValue filename, @Optional boolean useIncludePath, @Optional Value context, @Optional long offset, @Optional("4294967296") long maxLen) { if (filename.length() == 0) { env.warning(L.l("file name must not be null")); return null; } BinaryStream s = fopen(env, filename, "r", useIncludePath, context); if (! (s instanceof BinaryInput)) return null; BinaryInput is = (BinaryInput) s; StringValue bb = env.createLargeBinaryBuilder(); bb.appendReadAll(is, maxLen); s.close(); return bb; } /** * Writes data to a file. */ public static Value file_put_contents(Env env, StringValue filename, Value data, @Optional int flags, @Optional Value context) { if (filename.length() == 0) { env.warning(L.l("file name must not be null")); return BooleanValue.FALSE; } // php/1634 BinaryStream s = null; try { boolean useIncludePath = (flags & FILE_USE_INCLUDE_PATH) != 0; String mode = (flags & FILE_APPEND) != 0 ? "a" : "w"; s = fopen(env, filename, mode, useIncludePath, context); if (! (s instanceof BinaryOutput)) return BooleanValue.FALSE; if ((flags & LOCK_EX) != 0) { if (s instanceof LockableStream) { if (! flock(env, (LockableStream) s, LOCK_EX, null)) return BooleanValue.FALSE; } else { return BooleanValue.FALSE; } } BinaryOutput os = (BinaryOutput) s; try { long dataWritten = 0; if (data instanceof ArrayValue) { for (Value item : ((ArrayValue) data).values()) { InputStream is = item.toInputStream(); dataWritten += os.write(is, Integer.MAX_VALUE); is.close(); } } else { InputStream is = data.toInputStream(); dataWritten += os.write(is, Integer.MAX_VALUE); is.close(); } return new LongValue(dataWritten); } finally { os.close(); } } catch (IOException e) { throw new QuercusModuleException(e); } finally { if (s != null && (s instanceof LockableStream) && ((flags & LOCK_EX) != 0)) flock(env, (LockableStream) s, LOCK_UN, null); } } /** * Advisory locking * * @param fileV the file handle * @param operation the locking operation * @param wouldBlock the resource context */ public static boolean flock(Env env, LockableStream fileV, int operation, @Optional Value wouldBlock) { // XXX: also wouldblock is a ref if (fileV == null) { env.warning(L.l("flock: file is null")); return false; } boolean shared = false; boolean block = true; if (operation > LOCK_NB) { block = false; operation -= LOCK_NB; } switch (operation) { case LOCK_SH: shared = true; break; case LOCK_EX: shared = false; break; case LOCK_UN: // flock($fd, LOCK_UN) returns true even // if no lock is held. fileV.unlock(); return true; default: // This is PHP's behavior... return true; } return fileV.lock(shared, block); } /** * Converts a glob pattern to a regular expression. */ private static String globToRegex(String pattern, int flags, boolean brace) { StringBuilder globRegex = new StringBuilder(); int bracketCount = 0; boolean inSquareBrackets = false; boolean inCurlyBrackets = false; char lastCh = ' '; for (int i = 0; i < pattern.length(); i++) { char ch = pattern.charAt(i); switch (ch) { case '*': if (inSquareBrackets || inCurlyBrackets) { globRegex.append("*"); if (inSquareBrackets) bracketCount++; } else { if ((flags & FNM_PATHNAME) != 0) globRegex.append("[^/]*"); else globRegex.append(".*"); } break; case '?': if (inSquareBrackets || inCurlyBrackets) { globRegex.append("*"); if (inSquareBrackets) bracketCount++; } else { if ((flags & FNM_PATHNAME) != 0) globRegex.append("[^/]"); else globRegex.append("."); } break; case '^': if (lastCh == '[') globRegex.append(ch); else { globRegex.append("\\" + ch); if (inSquareBrackets) bracketCount++; } break; case '!': if (lastCh == '[') globRegex.append('^'); else { globRegex.append(ch); if (inSquareBrackets) bracketCount++; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?