📄 binarybuildervalue.java
字号:
/* * 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 Scott Ferguson */package com.caucho.quercus.env;import java.io.*;import java.util.*;import com.caucho.vfs.*;import com.caucho.quercus.QuercusRuntimeException;import com.caucho.util.*;/** * Represents a 8-bit PHP 6 style binary builder (unicode.semantics = on) */public class BinaryBuilderValue extends BinaryValue{ public static final BinaryBuilderValue EMPTY = new BinaryBuilderValue(""); private final static BinaryBuilderValue []CHAR_STRINGS; protected byte []_buffer; protected int _length; private String _value; public BinaryBuilderValue() { _buffer = new byte[128]; } public BinaryBuilderValue(int capacity) { if (capacity < 64) capacity = 128; else capacity = 2 * capacity; _buffer = new byte[capacity]; } public BinaryBuilderValue(byte []buffer, int offset, int length) { _buffer = new byte[length]; _length = length; System.arraycopy(buffer, offset, _buffer, 0, length); } public BinaryBuilderValue(byte []buffer) { this(buffer, 0, buffer.length); } public BinaryBuilderValue(String s) { int len = s.length(); _buffer = new byte[len]; _length = len; for (int i = 0; i < len; i++) _buffer[i] = (byte) s.charAt(i); } public BinaryBuilderValue(char []buffer) { _buffer = new byte[buffer.length]; _length = buffer.length; for (int i = 0; i < buffer.length; i++) _buffer[i] = (byte) buffer[i]; } public BinaryBuilderValue(char []s, Value v1) { int len = s.length; if (len < 128) _buffer = new byte[128]; else _buffer = new byte[len + 32]; _length = len; for (int i = 0; i < len; i++) { _buffer[i] = (byte) s[i]; } v1.appendTo(this); } public BinaryBuilderValue(Byte []buffer) { int length = buffer.length; _buffer = new byte[length]; _length = length; for (int i = 0; i < length; i++) { _buffer[i] = buffer[i].byteValue(); } } public BinaryBuilderValue(byte ch) { _buffer = new byte[1]; _length = 1; _buffer[0] = ch; } /** * Creates the string. */ public static StringValue create(int value) { if (value < CHAR_STRINGS.length) return CHAR_STRINGS[value]; else return new BinaryBuilderValue(value); } /* * Creates an empty string builder of the same type. */ public StringValue createEmptyStringBuilder() { return new BinaryBuilderValue(); } /* * Returns the empty string of same type. */ public StringValue getEmptyString() { return EMPTY; } /** * Returns the value. */ public String getValue() { return toString(); } /** * Returns the type. */ @Override public String getType() { return "string"; } /** * Returns the ValueType. */ @Override public ValueType getValueType() { return getValueType(_buffer, 0, _length); } /** * Returns true for a long */ @Override public boolean isLongConvertible() { byte []buffer = _buffer; int len = _length; if (len == 0) return true; for (int i = 0; i < len; i++) { int ch = _buffer[i]; if (! ('0' <= ch && ch <= '9')) return false; } return true; } /** * Returns true for a double */ public boolean isDouble() { return false; } /** * Returns true for a number */ @Override public boolean isNumber() { return false; } /** * Returns true for a scalar */ @Override public boolean isScalar() { return true; } /** * Converts to a boolean. */ @Override public boolean toBoolean() { if (_length == 0) return false; else if (_length == 1 && _buffer[0] == '0') return false; else return true; } /** * Converts to a long. */ @Override public long toLong() { return parseLong(_buffer, 0, _length); } /** * Converts to a double. */ @Override public double toDouble() { return toDouble(_buffer, 0, _length); } /** * Convert to an input stream. */ @Override public InputStream toInputStream() { return new BuilderInputStream(); } /** * Converts to a string. */ @Override public String toString() { // XXX: encoding if (_value == null) _value = new String(_buffer, 0, _length); return _value; } /** * Converts to an object. */ @Override public Object toJavaObject() { if (_value == null) _value = new String(_buffer, 0, _length); return _value; } /** * Converts to a string builder */ @Override public StringValue toStringBuilder() { // XXX: can this just return this, or does it need to return a copy? return new BinaryBuilderValue(_buffer, 0, _length); } /** * Converts to a BinaryValue. */ @Override public StringValue toBinaryValue(Env env) { return this; } /** * Converts to a BinaryValue in desired charset. */ @Override public StringValue toBinaryValue(Env env, String charset) { return this; } /** * Append to a string builder. */ public void appendTo(StringValue bb) { bb.append(_buffer, 0, _length); } /** * Converts to a key. */ @Override public Value toKey() { byte []buffer = _buffer; int len = _length; if (len == 0) return this; int sign = 1; long value = 0; int i = 0; int ch = buffer[i]; if (ch == '-') { sign = -1; i++; } for (; i < len; i++) { ch = buffer[i]; if ('0' <= ch && ch <= '9') value = 10 * value + ch - '0'; else return this; } return new LongValue(sign * value); } /** * Converts to a byte array, with no consideration of character encoding. * Each character becomes one byte, characters with values above 255 are * not correctly preserved. */ public byte[] toBytes() { byte[] bytes = new byte[_length]; System.arraycopy(_buffer, 0, bytes, 0, _length); return bytes; } // // Operations // /** * Returns the character at an index */ public Value get(Value key) { return charValueAt(key.toLong()); } /** * Returns the character at an index */ public Value getRef(Value key) { return charValueAt(key.toLong()); } /** * Returns the character at an index */ @Override public Value charValueAt(long index) { int len = _length; if (index < 0 || len <= index) return UnsetBinaryValue.UNSET; else return BinaryBuilderValue.create(_buffer[(int) index] & 0xff); } /** * sets the character at an index */ /* public Value setCharAt(long index, String value) { int len = _length; if (index < 0 || len <= index) return this; else { BinaryBuilderValue sb = new BinaryBuilderValue(_buffer, 0, (int) index); sb.append(value); sb.append(_buffer, (int) (index + 1), (int) (len - index - 1)); return sb; } } */ // // CharSequence // /** * Returns the length of the string. */ @Override public int length() { return _length; } /** * Returns the character at a particular location */ @Override public char charAt(int index) { return (char) (_buffer[index] & 0xff); } /** * Returns a subsequence */ @Override public CharSequence subSequence(int start, int end) { if (end <= start) return StringValue.EMPTY; return new BinaryBuilderValue(_buffer, start, end - start); } /** * Convert to lower case. */ @Override public StringValue toLowerCase() { int length = _length; BinaryBuilderValue string = new BinaryBuilderValue(length); byte []srcBuffer = _buffer; byte []dstBuffer = string._buffer; for (int i = 0; i < length; i++) { byte ch = srcBuffer[i]; if ('A' <= ch && ch <= 'Z') dstBuffer[i] = (byte) (ch + 'a' - 'A'); else dstBuffer[i] = ch; } string._length = length; return string; } /** * Convert to lower case. */ @Override public StringValue toUpperCase() { int length = _length; BinaryBuilderValue string = new BinaryBuilderValue(_length); byte []srcBuffer = _buffer; byte []dstBuffer = string._buffer; for (int i = 0; i < length; i++) { byte ch = srcBuffer[i]; if ('a' <= ch && ch <= 'z') dstBuffer[i] = (byte) (ch + 'A' - 'a'); else dstBuffer[i] = ch; } string._length = length; return string; } // // append code // /** * Creates a string builder of the same type. */ @Override public StringValue createStringBuilder() { return new BinaryBuilderValue(); } /** * Creates a string builder of the same type. */ @Override public StringValue createStringBuilder(int length) { return new BinaryBuilderValue(length); } /** * Converts to a string builder */ @Override public StringValue toStringBuilder(Env env) { return new BinaryBuilderValue(_buffer, 0, _length); } /** * Append a Java buffer to the value. */ @Override public final StringValue appendUnicode(char []buf, int offset, int length) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); appendTo(sb); sb.append(buf, offset, length); return sb; } /** * Append a Java buffer to the value. */ @Override public final StringValue append(CharSequence buf, int head, int tail) { int length = tail - head; if (_buffer.length < _length + length) ensureCapacity(_length + length); if (buf instanceof BinaryBuilderValue) { BinaryBuilderValue sb = (BinaryBuilderValue) buf; System.arraycopy(sb._buffer, head, _buffer, _length, tail - head); _length += tail - head; return this; } else { byte []buffer = _buffer; int bufferLength = _length; for (; head < tail; head++) { buffer[bufferLength++] = (byte) buf.charAt(head); } _length = bufferLength; return this; } } /** * Append a Java buffer to the value. */ // @Override public final StringValue append(BinaryBuilderValue sb, int head, int tail) { int length = tail - head; if (_buffer.length < _length + length) ensureCapacity(_length + length); System.arraycopy(sb._buffer, head, _buffer, _length, tail - head); _length += tail - head; return this; } /** * Append a Java value to the value. */ @Override public final StringValue append(Value v) { /* if (v.length() == 0) return this; else { // php/033a v.appendTo(this); return this; } */ return v.appendTo(this); } /** * Append a buffer to the value. */ public final StringValue append(byte []buf, int offset, int length) { if (_buffer.length < _length + length) ensureCapacity(_length + length); System.arraycopy(buf, offset, _buffer, _length, length); _length += length; return this; } /** * Append a double to the value. */ public final StringValue append(byte []buf) { return append(buf, 0, buf.length); } /** * Append a Java byte to the value without conversions. */ @Override public final StringValue append(char v) { int length = _length + 1; if (_buffer.length < length) ensureCapacity(length); _buffer[_length++] = (byte) v; return this; } /** * Append a Java byte to the value without conversions. */ public final StringValue append(byte v) { int length = _length + 1; if (_buffer.length < length) ensureCapacity(length); _buffer[_length++] = v; return this; } /** * Append a Java boolean to the value. */ @Override public final StringValue append(boolean v) { return append(v ? "true" : "false"); } /** * Append a Java long to the value. */ @Override public StringValue append(long v) { // XXX: this probably is frequent enough to special-case return append(String.valueOf(v)); } /** * Append a Java double to the value. */ @Override public StringValue append(double v) { return append(String.valueOf(v)); } /** * Append a bytes to the value. */ @Override public StringValue append(String s) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -