📄 cbarraylist.java
字号:
/*
* Copyright (c) 2000-2004, Rickard C鰏ter, Martin Svensson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of SICS nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package com.mellowtech.disc;
import java.util.ArrayList;
import java.util.Iterator;
import java.nio.*;
/**
* A ByteStorable wrapper for an ArrayList
*
* @author Rickard C鰏ter
* @version 1.0
* @see java.util.ArrayList
*/
public class CBArrayList extends ByteStorable {
/**
* The internal ArrayList.
*
*/
protected ArrayList list = new ArrayList();
/**
* The name of the class type this array contains.
*
*/
protected CBString template = null;
/**
* Creates a new <code>CBArrayList</code> instance.
*
*/
public CBArrayList() {
}
/**
* Creates a new <code>CBArrayList</code> instance.
*
* @param template that indicates what type of objects that is stored
*/
public CBArrayList(ByteStorable template) {
this.template = new CBString(template.getClass().getName());
}
/**
* Set the template
*
* @param template that indicates what type of objects that is stored
*/
public void setTemplate(ByteStorable template) {
this.template = new CBString(template.getClass().getName());
}
/**
* Get the internal ArrayList
*
* @return an <code>ArrayList</code> value
*/
public ArrayList getArrayList() {
return list;
}
public int byteSize() {
int size = 8 + template.byteSize();
for (Iterator iter = list.iterator(); iter.hasNext();)
size += ((ByteStorable) iter.next()).byteSize();
return size;
}
public int byteSize(ByteBuffer bb) {
int size = bb.getInt();
bb.position(bb.position()-4);
return size;
}
public void toBytes(ByteBuffer bb){
bb.putInt(byteSize());
bb.putInt(list.size());
template.toBytes(bb);
for (Iterator iter = list.iterator(); iter.hasNext();) {
ByteStorable bs = (ByteStorable)iter.next();
bs.toBytes(bb);
}
}
public ByteStorable fromBytes(ByteBuffer bb) {
CBArrayList cblist = new CBArrayList();
int size = bb.getInt();
int count = bb.getInt();
cblist.template = new CBString();
cblist.template = (CBString) cblist.template.fromBytes(bb);
//System.out.println("In CBarraylist:frombytes\n" +
// "size = " + size + " count = " + count);
try {
for (int i = 0; i < count; i++) {
ByteStorable bs = (ByteStorable)
Class.forName(cblist.template.toString()).newInstance();
bs = bs.fromBytes(bb);
//System.out.println(bs.toString());
cblist.list.add(bs);
}
}
catch(Exception e) {
e.printStackTrace();
return null;
}
return cblist;
}
public String toString() {
StringBuffer sb = new StringBuffer();
int count = 0;
for (Iterator iter = list.iterator();iter.hasNext();) {
sb.append(" " + iter.next());
count++;
}
return byteSize() + "\t" + count + "\t[" + sb.toString() + "]";
}
/*
public static void main(String[] args) {
CBArrayList cblist1 = new CBArrayList(new CBInt());
ArrayList list = cblist1.getArrayList();
for (int i = 0; i < 10; i++)
list.add(new CBInt(i));
ByteBuffer bb = ByteBuffer.allocate(cblist1.byteSize());
cblist1.toBytes(bb);
bb.flip();
CBArrayList cblist2 = new CBArrayList();
cblist2 = (CBArrayList) cblist2.fromBytes(bb);
System.out.println(cblist1);
System.out.println(cblist2);
bb.flip();
System.out.println(bb);
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -