📄 abstractbuffer.java
字号:
// ========================================================================// Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package org.mortbay.io;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * @author gregw * */public abstract class AbstractBuffer implements Buffer{ protected final static String __IMMUTABLE = "IMMUTABLE", __READONLY = "READONLY", __READWRITE = "READWRITE", __VOLATILE = "VOLATILE"; protected int _access; protected boolean _volatile; protected int _get; protected int _put; protected int _hash; protected int _hashGet; protected int _hashPut; protected int _mark; protected String _string; protected View _view; /** * Constructor for BufferView * * @param access 0==IMMUTABLE, 1==READONLY, 2==READWRITE */ public AbstractBuffer(int access, boolean isVolatile) { if (access == IMMUTABLE && isVolatile) throw new IllegalArgumentException("IMMUTABLE && VOLATILE"); setMarkIndex(-1); _access = access; _volatile = isVolatile; } /* * @see org.mortbay.io.Buffer#toArray() */ public byte[] asArray() { byte[] bytes = new byte[length()]; byte[] array = array(); if (array != null) Portable.arraycopy(array, getIndex(), bytes, 0, bytes.length); else peek(getIndex(), bytes, 0, length()); return bytes; } public ByteArrayBuffer duplicate(int access) { Buffer b=this.buffer(); if (b instanceof Buffer.CaseInsensitve) return new ByteArrayBuffer.CaseInsensitive(asArray(), 0, length(),access); else return new ByteArrayBuffer(asArray(), 0, length(), access); } /* * @see org.mortbay.io.Buffer#asNonVolatile() */ public Buffer asNonVolatileBuffer() { if (!isVolatile()) return this; return duplicate(_access); } public Buffer asImmutableBuffer() { if (isImmutable()) return this; return duplicate(IMMUTABLE); } /* * @see org.mortbay.util.Buffer#asReadOnlyBuffer() */ public Buffer asReadOnlyBuffer() { if (isReadOnly()) return this; return new View(this, markIndex(), getIndex(), putIndex(), READONLY); } public Buffer asMutableBuffer() { if (!isImmutable()) return this; Buffer b=this.buffer(); if (b.isReadOnly()) { return duplicate(READWRITE); } return new View(b, markIndex(), getIndex(), putIndex(), _access); } public Buffer buffer() { return this; } public void clear() { setMarkIndex(-1); setGetIndex(0); setPutIndex(0); } public void compact() { if (isReadOnly()) throw new IllegalStateException(__READONLY); int s = markIndex() >= 0 ? markIndex() : getIndex(); if (s > 0) { byte array[] = array(); int length = putIndex() - s; if (length > 0) { if (array != null) Portable.arraycopy(array(), s, array(), 0, length); else poke(0, peek(s, length)); } if (markIndex() > 0) setMarkIndex(markIndex() - s); setGetIndex(getIndex() - s); setPutIndex(putIndex() - s); } } public boolean equals(Object obj) { if (obj==this) return true; // reject non buffers; if (obj == null || !(obj instanceof Buffer)) return false; Buffer b = (Buffer) obj; if (this instanceof Buffer.CaseInsensitve || b instanceof Buffer.CaseInsensitve) return equalsIgnoreCase(b); // reject different lengths if (b.length() != length()) return false; // reject AbstractBuffer with different hash value if (_hash != 0 && obj instanceof AbstractBuffer) { AbstractBuffer ab = (AbstractBuffer) obj; if (ab._hash != 0 && _hash != ab._hash) return false; } // Nothing for it but to do the hard grind. int get=getIndex(); int bi=b.putIndex(); for (int i = putIndex(); i-->get;) { byte b1 = peek(i); byte b2 = b.peek(--bi); if (b1 != b2) return false; } return true; } public boolean equalsIgnoreCase(Buffer b) { if (b==this) return true; // reject different lengths if (b.length() != length()) return false; // reject AbstractBuffer with different hash value if (_hash != 0 && b instanceof AbstractBuffer) { AbstractBuffer ab = (AbstractBuffer) b; if (ab._hash != 0 && _hash != ab._hash) return false; } // Nothing for it but to do the hard grind. int get=getIndex(); int bi=b.putIndex(); byte[] array = array(); byte[] barray= b.array(); if (array!=null && barray!=null) { for (int i = putIndex(); i-->get;) { byte b1 = array[i]; byte b2 = barray[--bi]; if (b1 != b2) { if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); if (b1 != b2) return false; } } } else { for (int i = putIndex(); i-->get;) { byte b1 = peek(i); byte b2 = b.peek(--bi); if (b1 != b2) { if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); if (b1 != b2) return false; } } } return true; } public byte get() { return peek(_get++); } public int get(byte[] b, int offset, int length) { int gi = getIndex(); int l=length(); if (l==0) return -1; if (length>l) length=l; length = peek(gi, b, offset, length); if (length>0) setGetIndex(gi + length); return length; } public Buffer get(int length) { int gi = getIndex(); Buffer view = peek(gi, length); setGetIndex(gi + length); return view; } public final int getIndex() { return _get; } public boolean hasContent() { return _put > _get; } public int hashCode() { if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) { int get=getIndex(); byte[] array = array(); if (array==null) { for (int i = putIndex(); i-- >get;) { byte b = peek(i); if ('a' <= b && b <= 'z') b = (byte) (b - 'a' + 'A'); _hash = 31 * _hash + b; } } else { for (int i = putIndex(); i-- >get;) { byte b = array[i]; if ('a' <= b && b <= 'z') b = (byte) (b - 'a' + 'A'); _hash = 31 * _hash + b; } } if (_hash == 0) _hash = -1; _hashGet=_get; _hashPut=_put; } return _hash; } public boolean isImmutable() { return _access <= IMMUTABLE; } public boolean isReadOnly() { return _access <= READONLY; } public boolean isVolatile() { return _volatile; } public int length() { return _put - _get; } public void mark() { setMarkIndex(_get - 1); } public void mark(int offset) { setMarkIndex(_get + offset); } public int markIndex() { return _mark; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -