001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2005 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package j2me.nio;
010:
011: /**
012: * Clean-room implementation of Buffer to support
013: * <code>javolution.util.Struct</code> when <code>java.nio</code> is
014: * not available.
015: */
016: public abstract class Buffer {
017: final int _capacity;
018:
019: int _limit;
020:
021: int _position;
022:
023: int _mark;
024:
025: Buffer(int capacity, int limit, int position, int mark) {
026: _capacity = capacity;
027: _limit = limit;
028: _position = position;
029: _mark = mark;
030: }
031:
032: public final int capacity() {
033: return _capacity;
034: }
035:
036: public final Buffer clear() {
037: _limit = _capacity;
038: _position = 0;
039: _mark = -1;
040: return this ;
041: }
042:
043: public final Buffer flip() {
044: _limit = _position;
045: _position = 0;
046: _mark = -1;
047: return this ;
048: }
049:
050: public final boolean hasRemaining() {
051: return _limit - _position > 0;
052: }
053:
054: public boolean isReadOnly() {
055: return false;
056: }
057:
058: public final int limit() {
059: return _limit;
060: }
061:
062: public final Buffer limit(int newLimit) {
063: if ((newLimit < 0) || (newLimit > _capacity)) {
064: throw new IllegalArgumentException();
065: }
066: if (newLimit < _mark) {
067: _mark = -1;
068: }
069: if (_position > newLimit) {
070: _position = newLimit;
071: }
072: _limit = newLimit;
073: return this ;
074: }
075:
076: public final Buffer mark() {
077: _mark = _position;
078: return this ;
079: }
080:
081: public final int position() {
082: return _position;
083: }
084:
085: public final Buffer position(int newPosition) {
086: if ((newPosition < 0) || (newPosition > _limit)) {
087: throw new IllegalArgumentException();
088: }
089:
090: if (newPosition <= _mark) {
091: _mark = -1;
092: }
093: _position = newPosition;
094: return this ;
095: }
096:
097: public final int remaining() {
098: return _limit - _position;
099: }
100:
101: public final Buffer reset() {
102: if (_mark == -1) {
103: throw new InvalidMarkException();
104: }
105: _position = _mark;
106: return this ;
107: }
108:
109: public final Buffer rewind() {
110: _position = 0;
111: _mark = -1;
112: return this;
113: }
114: }
|