【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-ByteArrayBuffer.java,希望本篇文章对您学习有所帮助。
/*
Copyright (C) 2002-2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
/**
* Buffer contains code to read and write packets from/to the MySQL server.
*
* @version $Id: ByteArrayBuffer.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
* Exp $
* @author Mark Matthews
*/
class ByteArrayBuffer extends Buffer {
private int bufLength = 0;
private byte[] byteBuffer;
private int position = 0;
ByteArrayBuffer(byte[] buf) {
this.byteBuffer = buf;
setBufLength(buf.length);
}
ByteArrayBuffer(int size) {
this.byteBuffer = new byte[size];
setBufLength(this.byteBuffer.length);
this.position = MysqlIO.HEADER_LENGTH;
}
final void clear() {
this.position = MysqlIO.HEADER_LENGTH;
}
final void ensureCapacity(int additionalData) throws SQLException {
if ((this.position + additionalData) > getBufLength()) {
if ((this.position + additionalData) < this.byteBuffer.length) {
// byteBuffer.length is != getBufLength() all of the time
// due to re-using of packets (we don't shrink them)
//
// If we can, don't re-alloc, just set buffer length
// to size of current buffer
setBufLength(this.byteBuffer.length);
} else {
//
// Otherwise, re-size, and pad so we can avoid
// allocing again in the near future
//
int newLength = (int) (this.byteBuffer.length * 1.25);
if (newLength < (this.byteBuffer.length + additionalData)) {
newLength = this.byteBuffer.length
+ (int) (additionalData * 1.25);
}
if (newLength < this.byteBuffer.length) {
newLength = this.byteBuffer.length + additionalData;
}
byte[] newBytes = new byte[newLength];
System.arraycopy(this.byteBuffer, 0, newBytes, 0,
this.byteBuffer.length);
this.byteBuffer = newBytes;
setBufLength(this.byteBuffer.length);
}
}
}
/**
* Skip over a length-encoded string
*
* @return The position past the end of the string
*/
public int fastSkipLenString() {
long len = this.readFieldLength();
this.position += len;
return (int) len; // this is safe, as this is only
}
protected final byte[] getBufferSource() {
return this.byteBuffer;
}
int getBufLength() {
return this.bufLength;
}
/**
* Returns the array of bytes this Buffer is using to read from.
*
* @return byte array being read from
*/
public byte[] getByteBuffer() {
return this.byteBuffer;
}
final byte[] getBytes(int len) {
byte[] b = new byte[len];
System.arraycopy(this.byteBuffer, this.position, b, 0, len);
this.position += len; // update cursor
return b;
}
/*
* (non-Javadoc)
*
* @see com.mysql.jdbc.Buffer#getBytes(int, int)
*/
byte[] getBytes(int offset, int len) {
byte[] dest = new byte[len];
System.arraycopy(this.byteBuffer, offset, dest, 0, len);
return dest;
}
int getCapacity() {
return this.byteBuffer.length;
}
public ByteBuffer getNioBuffer() {
throw new IllegalArgumentException(Messages
.getString("ByteArrayBuffer.0")); //$NON-NLS-1$
}
/**
* Returns the current position to write to/ read from
*
* @return the current position to write to/ read from
*/
public int getPosition() {
return this.position;
}
// 2000-06-05 Changed
final boolean isLastDataPacket() {
return ((getBufLength() < 9) && ((this.byteBuffer[0] & 0xff) == 254));
}
final long newReadLength() {
int sw = this.byteBuffer[this.position++] & 0xff;
switch (sw) {
case 251:
return 0;
case 252:
return readInt();
case 253:
return readLongInt();
case 254: // changed for 64 bit lengths
return readLongLong();
default:
return sw;
}
}
final byte readByte() {
return this.byteBuffer[this.position++];
}
final byte readByte(int readAt) {
return this.byteBuffer[readAt];
}
final long readFieldLength() {
int sw = this.byteBuffer[this.position++] & 0xff;
switch (sw) {
case 251:
return NULL_LENGTH;
case 252:
return readInt();
case 253:
return readLongInt();
case 254:
return readLongLong();
default:
return sw;
}
}
// 2000-06-05 Changed
final int readInt() {
byte[] b = this.byteBuffer; // a little bit optimization
return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8);
}
final int readIntAsLong() {
byte[] b = this.byteBuffer;
return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8)
| ((b[this.position++] & 0xff) << 16)
| ((b[this.position++] & 0xff) << 24);
}
final byte[] readLenByteArray(int offset) {
long len = this.readFieldLength();
if (len == NULL_LENGTH) {
return null;
}
if (len == 0) {
return Constants.EMPTY_BYTE_ARRAY;
}
this.position += offset;
return getBytes((int) len);
}
final long readLength() {
int sw = this.byteBuffer[this.position++] & 0xff;
switch (sw) {
case 251:
return 0;
case 252:
return readInt();
case 253:
return readLongInt();
case 254:
return readLong();
default:
return sw;
}
}
// 2000-06-05 Fixed
final long readLong() {
byte[] b = this.byteBuffer;
return ((long)b[this.position++] & 0xff) | (((long)b[this.position++] & 0xff) << 8)
| ((long)(b[this.position++] & 0xff) << 16)
| ((long)(b[this.position++] & 0xff) << 24);
}
// 2000-06-05 Changed
final int readLongInt() {
byte[] b = this.byteBuffer;
return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8)
| ((b[this.position++] & 0xff) << 16);
}
// 2000-06-05 Fixed
final long readLongLong() {
byte[] b = this.byteBuffer;
return (b[this.position++] & 0xff)
| ((long) (b[this.position++] & 0xff) << 8)
| ((long) (b[this.position++] & 0xff) << 16)
| ((long) (b[this.position++] & 0xff) << 24)
| ((long) (b[this.position++] & 0xff) << 32)
| ((long) (b[this.position++] & 0xff) << 40)
| ((long) (b[this.position++] & 0xff) << 48)
| ((long) (b[this.position++] & 0xff) << 56);
}
final int readnBytes() {
int sw = this.byteBuffer[this.position++] & 0xff;
switch (sw) {
case 1:
return this
上一篇:
ByteArray.java
下一篇:
电咖汽车入选2018独角兽企业榜百强 发布“天际”品牌将推高估值