memoryresourcerandomaccessbuffer.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 110 行

JAVA
110
字号
/*
 * $Id: MemoryResourceRandomAccessBuffer.java,v 1.1 2004/01/20 19:30:19 epr Exp $
 */
package org.jnode.system.util;

import java.io.EOFException;
import java.io.IOException;
import java.util.zip.RandomAccessBuffer;

import org.jnode.system.MemoryResource;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class MemoryResourceRandomAccessBuffer extends RandomAccessBuffer {

	private final MemoryResource mem;
	private final int length;
	private int pos;
	
	/**
	 * Initialize this instance
	 * @param mem
	 */
	public MemoryResourceRandomAccessBuffer(MemoryResource mem) {
		this.mem = mem;
		this.pos = 0;
		this.length = (int)mem.getSize();
	}
	
	/**
	 * @see java.util.zip.RandomAccessBuffer#close()
	 */
	public void close() throws IOException {
		// Nothing to do
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#length()
	 */
	public long length() throws IOException {
		return length;
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#read()
	 */
	public int read() throws IOException {
		if (pos < length) {
			return mem.getByte(pos++) & 0xFF;
		} else {
			return -1;
		}
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#read(byte[], int, int)
	 */
	public int read(byte[] data, int ofs, int len) throws IOException {
		if (pos < length) {
			len = Math.min(length - pos, len);
			mem.getBytes(pos, data, ofs, len);
			pos += len;
			return len;
		} else {
			return -1;
		}
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#readFully(byte[], int, int)
	 */
	public void readFully(byte[] data, int ofs, int len) throws IOException {
		if (pos + len <= this.length) {
			mem.getBytes(pos, data, ofs, len);
			pos += len;
		} else {
			throw new EOFException();
		}
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#readFully(byte[])
	 */
	public void readFully(byte[] data) throws IOException {
		readFully(data, 0, data.length);
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#seek(long)
	 */
	public void seek(long offset) throws IOException {
		pos = (int)offset;
	}

	/**
	 * @see java.util.zip.RandomAccessBuffer#skipBytes(int)
	 */
	public int skipBytes(int count) throws EOFException, IOException {
		if (count > 0) {
			count = Math.min(length - pos, count);
			pos += count;
			return count;
		} else {
			return 0;
		}
	}

}

⌨️ 快捷键说明

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