Code example for RandomAccessFile

Methods: getFilePointer, length, read, seek

0
 
	private boolean mInit = false;
	private void init() throws IOException { 
		if(mRAFile== null || !mInit) {
			mInit = true;
			mRAFile = new RandomAccessFile(mBase, "rw");
		} 
		if(mRAFile == null) {
			throw new IOException();
		} 
	} 
 
	@Override 
	public synchronized void seek(long point) throws IOException {
		//android.util.Log.v("kiyo", "---------seek---------" + point); 
		init(); 
		mSeek = point;
		if(mSeek <0) {
			mSeek = 0;
		} 
		if(mSeek<mRAFile.length()&&point!=mRAFile.getFilePointer()) {
			mRAFile.seek(mSeek);
		} 
	} 
 
	@Override 
	public synchronized long getFilePointer() { 
		return mSeek;
	} 
 
	@Override 
	public synchronized long length() throws IOException { 
		//android.util.Log.v("kiyo", "---------length---------"); 
		init(); 
		seek(mSeek);
		long raLen = mRAFile.length();
		long caLen = mCashStartPointPerFile+mCashLength;
//		android.util.Log.v("kiyo", "----reLen-" + raLen+",caLen="+caLen); 
		if(raLen > caLen) {
			return raLen;
		} else { 
			//android.util.Log.v("kiyo", "---------caLen---------" + caLen); 
			return caLen;
		} 
	} 
	 
	public synchronized void debugLightInfo() { 
//		String str; 
//		try { 
//			str = new String(mWriteCash, 0, mWriteCash.length,"utf8"); 
//			String[] list = str.split("\n"); 
//			android.util.Log.v("kiyo","### ----------------------"+mWriteCash.length); 
//			if(list == null){ 
//				return; 
//			} 
//			for(String s: list) { 
//				android.util.Log.v("kiyo","### "+s.substring(0, 3)); 
//			} 
//			android.util.Log.v("kiyo","### ----------------------"+str); 
//		} catch (UnsupportedEncodingException e) { 
//			// TODO Auto-generated catch block 
//			e.printStackTrace(); 
//		} 
 
	} 
 
	@Override 
	public synchronized int read(byte[] buffer) throws IOException {
		return read(buffer, buffer.length);
	} 
 
	@Override 
	public synchronized int read(byte[] buffer, int buffLen) throws IOException {
 
		init(); 
		seek(mSeek);
 
		long current = getFilePointer();
		long raLen = mRAFile.length();
		long caLen = mCashStartPointPerFile+mCashLength;
 
//		android.util.Log.v("kiyo","cu="+current+"ra="+raLen+",ca="+caLen); 
		if(current >= raLen&& current >= caLen) {
			return -1; 
		} 
		int ret = 0;
		if(mCashStartPointPerFile <= current && current< (mCashStartPointPerFile+mCashLength)) {
			int srcPos = (int)(current-mCashStartPointPerFile);
			int len = mCashLength -srcPos;
//			if(len>buffer.length) { 
//				len = buffer.length; 
//			} 
			if(len>buffLen) {
				len = buffLen;
			} 
 
			System.arraycopy(mWriteCash, srcPos, buffer, 0, len);
//			if((mCashLength-srcPos)<buffer.length&&raLen>caLen) { 
			if((mCashLength-srcPos)<buffLen&&raLen>caLen) {
				mRAFile.seek(current + mCashLength-srcPos);
//				ret = mRAFile.read(buffer, mCashLength-srcPos, buffer.length-(mCashLength-srcPos)); 
				ret = mRAFile.read(buffer, mCashLength-srcPos, buffLen-(mCashLength-srcPos));
			} 
			if(ret == -1) {
				ret = 0;
			} 
			ret = len + ret;
		} else { 
			ret = mRAFile.read(buffer);
			if(ret!=-1&&raLen<caLen) {
				int srcPos = (int)(current-mCashStartPointPerFile+ret);
//				int len = buffer.length-ret; 
				int len = buffLen-ret;
				if(len >mCashLength-srcPos) {