📄 countingbytebuffer.java
字号:
/* * @(#)$Id: CountingByteBuffer.java,v 1.4 2004/07/02 23:59:22 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package util.network.serialization;import java.nio.BufferUnderflowException;/** * Class CountingByteBuffer * */public class CountingByteBuffer implements GenericByteBuffer { protected int capacity; protected int position; protected int limit; public static final int BYTE_SIZE = 1; public static final int CHAR_SIZE = 2; public static final int SHORT_SIZE = 2; public static final int INT_SIZE = 4; public static final int FLOAT_SIZE = 4; public static final int DOUBLE_SIZE = 8; public static final int LONG_SIZE = 8; /** * Constructor CountingByteBuffer */ public CountingByteBuffer() { init(Integer.MAX_VALUE, 0, 0); } /** * Constructor CountingByteBuffer * * @param capacity */ public CountingByteBuffer(int capacity) { init(capacity, 0, 0); } /** * Constructor CountingByteBuffer * * @param copy */ public CountingByteBuffer(CountingByteBuffer copy) { init(copy.capacity, copy.position, copy.limit); } /** * Method init * * @param capacity * @param position * @param limit */ protected void init(int capacity, int position, int limit) { this.capacity = capacity; this.position = position; this.limit = limit; } /** * Method compact * @return */ public GenericByteBuffer compact() { return this; } /** * Method compareTo * * @param ob * @return */ public int compareTo(Object ob) { return 0; } /** * Method duplicate * @return */ public GenericByteBuffer duplicate() { return new CountingByteBuffer(this); } /** * Method get * @return */ public byte get() { if (position + BYTE_SIZE <= limit) { position += BYTE_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method get * * @param dst * @return */ public GenericByteBuffer get(byte[] dst) { if (position + (dst.length * BYTE_SIZE) <= limit) { position += (dst.length * BYTE_SIZE); return this; } else { throw new BufferUnderflowException(); } } /** * Method get * * @param dst * @param offset * @param length * @return */ public GenericByteBuffer get(byte[] dst, int offset, int length) { if ((length * BYTE_SIZE) < (limit - position)) { throw new BufferUnderflowException(); } if ((dst.length - offset - (length * BYTE_SIZE)) < 0) { throw new IndexOutOfBoundsException(); } position += (length * BYTE_SIZE); return this; } /** * Method get * * @param index * @return */ public byte get(int index) { if ((index + BYTE_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getChar * @return */ public char getChar() { if ((position + CHAR_SIZE) <= limit) { position += CHAR_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getChar * * @param index * @return */ public char getChar(int index) { if ((index + CHAR_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getDouble * @return */ public double getDouble() { if ((position + DOUBLE_SIZE) <= limit) { position += DOUBLE_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getDouble * * @param index * @return */ public double getDouble(int index) { if ((index + DOUBLE_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getFloat * @return */ public float getFloat() { if ((position + FLOAT_SIZE) <= limit) { position += FLOAT_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getFloat * * @param index * @return */ public float getFloat(int index) { if ((index + FLOAT_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getInt * @return */ public int getInt() { if ((position + INT_SIZE) <= limit) { position += INT_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getInt * * @param index * @return */ public int getInt(int index) { if ((index + INT_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getLong * @return */ public long getLong() { if ((position + LONG_SIZE) <= limit) { position += LONG_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getLong * * @param index * @return */ public long getLong(int index) { if ((index + LONG_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getShort * @return */ public short getShort() { if ((position + SHORT_SIZE) <= limit) { position += SHORT_SIZE; return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method getShort * * @param index * @return */ public short getShort(int index) { if ((index + SHORT_SIZE) <= limit) { return 0; } else { throw new IndexOutOfBoundsException(); } } /** * Method isDirect * @return */ public boolean isDirect() { return false; } /** * Method put * * @param b * @return */ public GenericByteBuffer put(byte b) { if ((position + BYTE_SIZE) <= capacity) { position += BYTE_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method put * * @param src * @return */ public GenericByteBuffer put(byte[] src) { if ((position + (src.length * BYTE_SIZE)) <= capacity) { position += (src.length * BYTE_SIZE); } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method put * * @param src * @param offset * @param length * @return */ public GenericByteBuffer put(byte[] src, int offset, int length) { if ((position + (length * BYTE_SIZE)) <= capacity) { position += (length * BYTE_SIZE); } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method put * * @param src * @return */ public GenericByteBuffer put(GenericByteBuffer src) { int length = ((CountingByteBuffer) src).limit - ((CountingByteBuffer) src).position; if ((position + length) <= capacity) { position += length; ((CountingByteBuffer) src).position = ((CountingByteBuffer) src).limit; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method put * * @param index * @param b * @return */ public GenericByteBuffer put(int index, byte b) { if ( !((index + BYTE_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putChar * * @param value * @return */ public GenericByteBuffer putChar(char value) { if ((position + BYTE_SIZE) <= capacity) { position += BYTE_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putChar * * @param index * @param value * @return */ public GenericByteBuffer putChar(int index, char value) { if ( !((index + CHAR_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putDouble * * @param value * @return */ public GenericByteBuffer putDouble(double value) { if ((position + DOUBLE_SIZE) <= capacity) { position += DOUBLE_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putDouble * * @param index * @param value * @return */ public GenericByteBuffer putDouble(int index, double value) { if ( !((index + DOUBLE_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putFloat * * @param value * @return */ public GenericByteBuffer putFloat(float value) { if ((position + FLOAT_SIZE) <= capacity) { position += FLOAT_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putFloat * * @param index * @param value * @return */ public GenericByteBuffer putFloat(int index, float value) { if ( !((index + FLOAT_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putInt * * @param value * @return */ public GenericByteBuffer putInt(int value) { if ((position + INT_SIZE) <= capacity) { position += INT_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putInt * * @param index * @param value * @return */ public GenericByteBuffer putInt(int index, int value) { if ( !((index + INT_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putLong * * @param value * @return */ public GenericByteBuffer putLong(long value) { if ((position + LONG_SIZE) <= capacity) { position += LONG_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putLong * * @param index * @param value * @return */ public GenericByteBuffer putLong(int index, long value) { if ( !((index + LONG_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method putShort * * @param value * @return */ public GenericByteBuffer putShort(short value) { if ((position + SHORT_SIZE) <= capacity) { position += SHORT_SIZE; } else { throw new IndexOutOfBoundsException(); } if (position >= limit) { limit = position; } return this; } /** * Method putShort * * @param index * @param value * @return */ public GenericByteBuffer putShort(int index, short value) { if ( !((index + SHORT_SIZE) <= limit)) { throw new IndexOutOfBoundsException(); } return this; } /** * Method slice * @return */ public GenericByteBuffer slice() { return new CountingByteBuffer(this); } /** * Method isReadOnly * @return */ public boolean isReadOnly() { return false; } /** * Method capacity * @return */ public int capacity() { return capacity; } /** * Method limit * @return */ public int limit() { return limit; } /** * Method limit * * @param newLimit * @return */ public GenericByteBuffer limit(int newLimit) { if (newLimit < capacity) { limit = newLimit; } else { throw new IllegalArgumentException(); } return this; } /** * Method position * @return */ public int position() { return position; } /** * Method position * * @param newPosition * @return */ public GenericByteBuffer position(int newPosition) { if (newPosition <= limit) { position = newPosition; } else { throw new IllegalArgumentException(); } return this; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -