⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringvector.java

📁 J2ME+WebService,采用KSoap框架进行操作。
💻 JAVA
字号:

/**
//文档生成日期:2005.8.14
//
//(1)概述:
//类名称:StringVector
//类说明:
 * Vector主要用来保存各种类型的对象(包括相同类型和不同类型的对象)。
 * 但是在一些情况下使用会给程序带来性能上的影响。这主要是由Vector类的两个特点所决定的。
 * 第一,Vector提供了线程的安全保护功能。即使Vector类中的许多方法同步。
 * 但是如果你已经确认你的应用程序是单线程,这些方法的同步就完全不必要了。
 * 第二,在Vector查找存储的各种对象时,常常要花很多的时间进行类型的匹配。
 * 而当这些对象都是同一类型时,这些匹配就完全不必要了。
 * 因此,有必要设计一个单线程的,保存特定类型对象的类或集合来替代Vector类
* 
//所在子系统:Web Service kSOAP Caller
//
//系统总描述:
	    
http://www.cnblogs.com/zhengyun_ustc/archive/2005/08/27/ksoapmidpwebservice1.html
讲的是
第一小步,Web Service传递String 
http://www.cnblogs.com/zhengyun_ustc/archive/2005/08/27/ksoapmidpwebservice2.html
则给出了
第二小步,Web Service传递较为复杂的类 
下面我们讲述如何在MIDP设备和Web Service之间传递较为复杂的类,比如这个类中不但有String类型成员变量,还有Vector之类的复杂类型。 
从kSoap的FAQ上看,他们推荐使用KvmSerializable以及 ClassMap传递自定义类,但是我一直没有试验成功。 
我还是按照能试验出来的办法讲述一下步骤吧: 
大致思路就是,在服务器端将类实例按照一定规格(一个一个的成员变量写)序列化为byte[],将这个byte[]数组返回给kSOAP。kSOAP收到之后,再反序列化,将byte[]一段一段地读入类实例。


//(2)历史记录:
//创建人: 郑昀(2005.8.14)
//联系我: Google Talk >> zhengyun@gmail.com
//Blogs:    http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc

//(3)版权声明:
//遵照GPL协议的大意开放源代码,您可以自由传播和修改,在遵照GPL协议的约束条件的前提下。

////////////////////////////////////////////////////////////////////*/
package com.ultrapower.tools;

/**
 * @author VictorZheng
 *
 */
public class StringVector 
{
	// 这儿的transient标示这个属性不需要自动序列化
	private transient String[] data; 
	private int count; 
	public int size()
	{
		return data.length;//count;
	}
	public StringVector() 
	{ 
	    // default size is 10
		this(10);   
	}
	public StringVector(int initialSize) 
	{ 
		count=initialSize;
		data = new String[initialSize]; 
	} 
	public void add(String str) 
	{ 
		//	 ignore null strings 
		if(str == null) { return; } 
		ensureCapacity(count + 1); 
		data[count++] = str; 
	} 

	private void ensureCapacity(int minCapacity) 
	{ 
		int oldCapacity = data.length; 
		if (minCapacity > oldCapacity) 
		{ 
			String oldData[] = data; 
			int newCapacity = oldCapacity * 2; 
			data = new String[newCapacity]; 
			System.arraycopy(oldData, 0, data, 0, count); 
		} 
	} 
	public void remove(String str) 
	{ 
		if(str == null) 
		{
			return; // ignore null str   
		}
		for(int i = 0; i < count; i++) 
		{ 
			//	 check for a match 
			if(data[i].equals(str)) 
			{ 
				System.arraycopy(data,i+1,data,i,count-1); // copy data 
				//	 allow previously valid array element be gc'd 
				data[--count] = null; 
				return; 
			} 
		} 
	}
	
	public final String getStringAt(int index) 
	{ 
		if(index < 0) 
		{ return null; } 
		else if(index > count) 
		{ 
			return null; // index is > # strings 
		} 
		else 
		{ 
			return data[index]; // index is good  
		}
	}
	
	public synchronized void writeObject(java.io.DataOutputStream s) 
	throws java.io.IOException  
	{  
		//	 Write out array length 
		s.writeInt(count);  
		//	 Write out all elements in the proper order.   
		for (int i=0; i<count; i++) 
			s.writeUTF(data[i]);  
	} 
	
	public synchronized void readObject(java.io.DataInputStream s) 
	throws java.io.IOException, ClassNotFoundException   
	{
		System.out.println("Enter readObject");
		//	 Read in array length and allocate array   
		int arrayLength = s.readInt(); 
		System.out.println("StringVector count=" + arrayLength);
		data = new String[arrayLength];
		// 同步data的大小
		count = arrayLength;
		//	 Read in all elements in the proper order.  
		for (int i=0; i<arrayLength; i++) 
		{
			data[i] = s.readUTF();
			System.out.println("读入:" + data[i]);
		} 
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -