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

📄 segmentreader.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 02/24/2004
 *
 * SegmentReader.java - A reader for javax.swing.text.Segment
 *                       objects.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.io;

import java.io.Reader;
import javax.swing.text.Segment;


/**
 * A <code>Reader</code> for <code>javax.swing.text.Segment</code> objects.
 *
 * @author Robert Futrell
 * @version 0.5
 */
public class SegmentReader extends Reader {

	/**
	 * The stream's position in the document.
	 */
	private int position;
	
	/**
	 * A remembered position in the document.
	 */
	private int mark;
	
	/**
	 * The segment we're working on.
	 */
	private Segment segment;
	

/*****************************************************************************/


	/**
	 * Constructor.
	 *
	 * @param segment The segment we're 'reading'.
	 */
	public SegmentReader(Segment segment) {
		setSegment(segment);
	}


/*****************************************************************************/


	/**
	 * This currently does nothing...
	 */
	public void close() {
	}


/*****************************************************************************/


	/**
	 * Returns the segment.
	 *
	 * @return The segment.
	 */
	public Segment getSegment() {
		return segment;
	}


/*****************************************************************************/


	/**
	 * Marks the present position in the stream.  Subsequent calls to
	 * <code>reset()</code> will reposition the stream to this point.
	 *
	 * @param readAheadLimit Ignored.
	 */
	public void mark(int readAheadLimit) {
		mark = position;
	}


/*****************************************************************************/


	/**
	 * Tells whether this reader supports the <code>mark</code> operation.
	 * This always returns <code>true</code> for <code>SegmentReader</code>.
	 */
	public boolean markSupported() {
		return true;
	}


/*****************************************************************************/


	/**
	 * Reads a single character at the current position in the segment.
	 */
	public int read() {
		if(position >= (long)(segment.offset+segment.count))
			return -1;      // Read past end of document.
		return segment.array[position];
	}


/*****************************************************************************/


	/**
	 * Read <code>array.length</code> characters from the beginning
	 * of the document into <code>array</code>.
	 *
	 * @param array The array to read characters into.
	 * @return The number of characters read.
	 */
	public int read(char array[]) {
		return read(array, 0, array.length);
	}


/*****************************************************************************/


	/**
	 * Reads characters into a portion of an array.
	 *
	 * @param cbuf The destination buffer.
	 * @param off Offset at which to start storing characters.
	 * @param len Maximum number of characters to read.
	 * @return The number of characters read, or <code>-1</code> if the
	 *         end of the stream (segment) has been reached.
	 */
	public int read(char cbuf[], int off, int len) {
		int k;
		int endBefore = segment.offset+segment.count;
		if(position >= (long)endBefore)
			return -1;      // Read past end of document.
		k = len;
		if(position + (long)k >= (long)endBefore)
			k = endBefore - (int)position;
		if(off + k >= cbuf.length)
			k = cbuf.length - off;
		System.arraycopy(segment.array,position,
						cbuf,off,
						k);
		position += k;
		return k;
	}


/*****************************************************************************/


	/**
	 * Tells whether this reader is ready to be read without
	 * blocking for input.  <code>SegmentReader</code> will
	 * always return true.
	 *
	 * @return <code>true</code> if the next read operation will
	 *         return without blocking.
	 */
	public boolean ready() {
		return true;
	}


/*****************************************************************************/


	/**
	 * Resets the stream.  If the stream has been marked, then attempt to
	 * reposition it at the mark.  If the stream has not been marked, then
	 * move it to the beginning of the document.
	 */
	public void reset() {
		if(mark == -1)
			position = 0;
		else
        		position = mark;
		mark = -1;
	}


/*****************************************************************************/


	/**
	 * Reinitializes this reader with a new segment.
	 *
	 * @param segment The new segment to read.
	 */
	public void setSegment(Segment segment) {
		mark = -1;
		this.segment = segment;
		position = segment.offset;
	}


/*****************************************************************************/


	/**
	 * Skips characters.  This will not 'skip' past the end of the segment.
	 *
	 * @param n The number of characters to skip.
	 * @return The number of characters actually skipped.
	 */
	public long skip(long n) {
		int endBefore = segment.offset + segment.count;
		if (position + n <= (long)endBefore) {
			position += n;
			return n;
		}
		else {
			long temp = position;
			position = endBefore;
			return (long)endBefore - temp;
		}
	}


/*****************************************************************************/


	/**
	 * Move to the specified position in the document.  If <code>pos</code>
	 * is greater than the document's length, the stream's position is moved
	 * to the end of the document.
	 *
	 * @param pos The position in the document to move to.
	 */
	public void seek(long pos) {
		int endBefore = segment.offset + segment.count;
		if(pos <= (long)endBefore)
			position = (int)pos;
		else
			position = endBefore;
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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