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

📄 rmsdao.java

📁 基于J2ME
💻 JAVA
字号:
package com.yxw.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.rms.*;

import com.yxw.model.PlayList;
public class RMSDao {

	private RecordStore rs;
	public static int cacheSize=100;
	public RMSDao(){
		
	}
	private void openRecordStore(String storeName){
		try {
			rs=RecordStore.openRecordStore(storeName, true);
		} catch (RecordStoreFullException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	private void closeRecordStore(){
		try {
			rs.closeRecordStore();
			rs=null;
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	public int addRecord(String storeName,String fileName,long size,String url){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		byte[] out=null;
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		DataOutputStream dos=new DataOutputStream(outputStream);
		try {
			dos.writeLong(size);
			dos.writeUTF(fileName);
			dos.writeUTF(url);
			outputStream.flush();
			out=outputStream.toByteArray();
			int t=rs.addRecord(out, 0, out.length);
			outputStream.reset();
			outputStream.close();
			dos.close();
			this.closeRecordStore();
			return t;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.closeRecordStore();
		return 0;
	}
	public int deleteRecord(String storeName,int recordId){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		try {
			rs.deleteRecord(recordId);
			return 1;
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.closeRecordStore();
		return 0;
	}
	public int updateRecord(String storeName,int recordId,String fileName,long size,String url){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		byte[] out=null;
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		DataOutputStream dos=new DataOutputStream(outputStream);
		try {
			dos.writeLong(size);
			dos.writeUTF(fileName);
			dos.writeUTF(url);
			outputStream.flush();
			out=outputStream.toByteArray();
			rs.setRecord(recordId, out, 0, out.length);
			outputStream.reset();
			outputStream.close();
			dos.close();
			this.closeRecordStore();
			return 1;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		
		this.closeRecordStore();
	
		return 0;
	}
	public byte[] getRecordById(String storeName,int id){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		byte[] in;
		try {
			in = new byte[rs.getRecordSize(id)];
			ByteArrayInputStream inputStream=new ByteArrayInputStream(in);
			DataInputStream dis=new DataInputStream(inputStream);
			rs.getRecord(id, in, 0);
		
			long size=dis.readLong();
			String fileName=dis.readUTF();
			String url=dis.readUTF();
			//System.out.println(fileName+" "+size+" "+url);
			inputStream.reset();
			inputStream.close();
			dis.close();
			this.closeRecordStore();
			return in;
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.closeRecordStore();
		return null;
	}
	public static void deleteRecordStore(String storeName){
		try {
			RecordStore.deleteRecordStore(storeName);
		} catch (RecordStoreNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	public int saveOrUpdateConfig(String storeName,int cacheSize,boolean isProxy){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		byte[] out=null;
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		DataOutputStream dos=new DataOutputStream(outputStream);
		try {
			dos.writeInt(cacheSize);
			dos.writeBoolean(isProxy);
			outputStream.flush();
			out=outputStream.toByteArray();
			if(this.rs.getNumRecords()==1){
				rs.setRecord(1, out, 0, out.length);
			}else{
				rs.addRecord(out, 0, out.length);
			}
			
			outputStream.reset();
			outputStream.close();
			dos.close();
			this.closeRecordStore();
			return 1;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
			
		}
		
		this.closeRecordStore();
	
		return 0;
	}
	public byte[] getConfig(String storeName){
		
		if(rs==null){
			this.openRecordStore(storeName);
		}
		byte[] in;
		try {
			in = new byte[rs.getRecordSize(1)];
			ByteArrayInputStream inputStream=new ByteArrayInputStream(in);
			DataInputStream dis=new DataInputStream(inputStream);
			rs.getRecord(1, in, 0);
		
			int cacheSize=dis.readInt();
			boolean isProxy=dis.readBoolean();
			System.out.println(cacheSize+" "+isProxy);
			inputStream.reset();
			inputStream.close();
			dis.close();
			this.closeRecordStore();
			return in;
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.closeRecordStore();
		return null;
	}
	public PlayList[] getFileList(String storeName){
		if(rs==null){
			this.openRecordStore(storeName);
		}
		try {
			RecordEnumeration re=rs.enumerateRecords(null, null, false);
			if(re.numRecords()<=0){
				this.closeRecordStore();
				return null;
			}
			System.out.println("all:"+re.numRecords());
			 PlayList[] list=new PlayList[re.numRecords()];
			int i=re.numRecords()-1;
			while(re.hasNextElement()){
				
				ByteArrayInputStream inputStream;
				try {
					
					//System.out.println(re.nextRecordId());
					int id=re.nextRecordId();
					list[i]=new PlayList();
					inputStream = new ByteArrayInputStream(rs.getRecord(id));
					DataInputStream dis=new DataInputStream(inputStream);
					list[i].setId(id);
					list[i].setSize(dis.readLong());
					list[i].setFileName(dis.readUTF());
					list[i].setUrl(dis.readUTF());
					i--;
					inputStream.reset();
					inputStream.close();
					dis.close();
				} catch (InvalidRecordIDException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				} catch (RecordStoreException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				} catch (Exception e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
				
				
			}
			
			this.closeRecordStore();
			return list;
		} catch (RecordStoreNotOpenException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.closeRecordStore();
		return null;
	}
}

⌨️ 快捷键说明

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