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

📄 filestack.java

📁 短信开发用于文件交换处理转发的类模块
💻 JAVA
字号:
package com.pub.dataframe;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashMap;

import com.sms.protocol.base.IDataPackage;

/**
 * Class description goes here.
 * 
 * @version 9:34:54 AM Jul 23, 2007
 * @author rocky
 * 多例模式
 */
public class FileStack {

	private static HashMap<String,FileStack> instances = new HashMap<String,FileStack>(10);
	
	private String fileName = "";
	
	
	/**
	 * 私有的构造子保证外界无法直接将此类实例化
	 */
	private FileStack() {
		// do nothing
	}
	
	/**
	 * 私有的构造子保证外界无法直接将此类实例化
	 */
	private FileStack(String fileName) {
		this.fileName = fileName;
	}
	
	/**
	 * 工厂方法,返还一个具有指定的内部状态的实例
	 */
	public synchronized static FileStack getInstance(String fileName) {
		if (instances.containsKey(fileName)) {
			return instances.get(fileName);
		} else {
			FileStack temp = new FileStack(fileName);
			instances.put(fileName, temp);
			return temp;
		}
	}
	

	private static ThreadLocal tlVB = new ThreadLocal() {
		protected Object initialValue() {
			return org.apache.mina.common.ByteBuffer.allocate(512, false)
					.setAutoExpand(true);
		}
	};

	private static org.apache.mina.common.ByteBuffer getBuffer() {
		return (org.apache.mina.common.ByteBuffer) tlVB.get();
	}

	/**
	 * 消息入文件栈
	 * 
	 * @param stack
	 * @return
	 * 
	 */
	public synchronized boolean pushNoWait(IDataPackage message)
			throws Exception {

		FileChannel fc = null;
		try {
			fc = new RandomAccessFile(fileName, "rw").getChannel();
			org.apache.mina.common.ByteBuffer pbuf = getBuffer();
			pbuf.clear();
			message.writePackage(pbuf);
			pbuf.putInt(message.getPackageLength());// 设置消息到小的文件头
			pbuf.flip();
			if (fc.tryLock() != null) {
				fc.position(fc.size());
				fc.write(pbuf.buf());
			} else {
				return false;
			}
		} finally {
			if (fc != null)
				fc.close();
		}
		return true;

	}

	/**
	 * 阻塞插入文件缓冲栈
	 * 
	 * @param filename
	 * @param stack
	 * @return
	 */
	public synchronized boolean push(IDataPackage message)
			throws Exception {
		FileChannel fc = null;
		FileLock lock = null;
		try {
			fc = new RandomAccessFile(fileName, "rw").getChannel();
			lock = fc.lock();
			fc.position(fc.size());
			org.apache.mina.common.ByteBuffer pbuf = getBuffer();
			pbuf.clear();
			message.writePackage(pbuf);
			pbuf.putInt(message.getPackageLength());// 设置消息到小的文件头
			pbuf.flip();
			fc.write(pbuf.buf());
		} finally {
			try {
				if (fc != null)
					fc.close();
			} catch (Exception ex) {

			}
			try {
				if (lock != null)
					lock.release();
			} catch (Exception ex) {

			}
		}
		return true;

	}

	/**
	 * 读消息出文件栈 非阻塞
	 * 
	 * @param file_name
	 * @param pbuf
	 * @return
	 */
	public synchronized boolean popNoWait(IDataPackage message)
			throws Exception {

		FileChannel fc = null;
		try {
			fc = new RandomAccessFile(fileName, "rw").getChannel();

			if (fc.tryLock() != null) {

				if (fc.size() < 4) {
					fc.close();
					return false;// 读取失败
				}
				fc.position(fc.size() - 4);// 前缩4个字符
				ByteBuffer bf = ByteBuffer.allocate(4);// static
				fc.read(bf);
				bf.flip();
				int len = bf.getInt();
				if (fc.size() < (len + 4)) {
					fc.close();
					fc.truncate(fc.size());
					return false;// 读取失败有错误数据丢弃
				}
				fc.position(fc.size() - len - 4);
				bf = ByteBuffer.allocate(len);// static
				fc.read(bf);
				fc.truncate(fc.size() - len - 4);
				bf.flip();
				org.apache.mina.common.ByteBuffer pbuf = org.apache.mina.common.ByteBuffer
						.wrap(bf);
				message.readPackage(pbuf);
				return true;
			} else {
				return false;
			}

		} finally {
			if (fc != null)
				fc.close();
		}

	}

	/**
	 * 阻塞状态下读文件缓冲栈
	 * 
	 * @param stack
	 */
	public synchronized boolean pop(IDataPackage message)
			throws Exception {

		FileChannel fc = null;
		FileLock lock = null;
		try {

			fc = new RandomAccessFile(fileName, "rw").getChannel();
			lock = fc.lock();
			if (fc.size() < 4) {
				fc.close();
				return false;
			}
			fc.position(fc.size() - 4);// 前缩4个字符
			ByteBuffer bf = ByteBuffer.allocate(4);// static
			fc.read(bf);
			bf.flip();
			int len = bf.getInt();
			if (fc.size() < (len + 4)) {
				fc.close();
				fc.truncate(fc.size());
				return false;// 读取失败有错误数据丢弃
			}
			fc.position(fc.size() - len - 4);
			bf = ByteBuffer.allocate(len);// static
			fc.read(bf);
			fc.truncate(fc.size() - len - 4);
			bf.flip();
			org.apache.mina.common.ByteBuffer pbuf = org.apache.mina.common.ByteBuffer
					.wrap(bf);
			message.readPackage(pbuf);

		} finally {
			try {
				if (fc != null)
					fc.close();
			} catch (Exception ex) {

			}
			try {
				if (lock != null)
					lock.release();
			} catch (Exception ex) {

			}
		}
		return true;

	}

}

⌨️ 快捷键说明

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