KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > j2me > nio > Buffer


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package j2me.nio;
10
11 /**
12  * Clean-room implementation of Buffer to support
13  * <code>javolution.util.Struct</code> when <code>java.nio</code> is
14  * not available.
15  */

16 public abstract class Buffer {
17     final int _capacity;
18
19     int _limit;
20
21     int _position;
22
23     int _mark;
24
25     Buffer(int capacity, int limit, int position, int mark) {
26         _capacity = capacity;
27         _limit = limit;
28         _position = position;
29         _mark = mark;
30     }
31
32     public final int capacity() {
33         return _capacity;
34     }
35
36     public final Buffer clear() {
37         _limit = _capacity;
38         _position = 0;
39         _mark = -1;
40         return this;
41     }
42
43     public final Buffer flip() {
44         _limit = _position;
45         _position = 0;
46         _mark = -1;
47         return this;
48     }
49
50     public final boolean hasRemaining() {
51         return _limit - _position > 0;
52     }
53
54     public boolean isReadOnly() {
55         return false;
56     }
57
58     public final int limit() {
59         return _limit;
60     }
61
62     public final Buffer limit(int newLimit) {
63         if ((newLimit < 0) || (newLimit > _capacity)) {
64             throw new IllegalArgumentException JavaDoc();
65         }
66         if (newLimit < _mark) {
67             _mark = -1;
68         }
69         if (_position > newLimit) {
70             _position = newLimit;
71         }
72         _limit = newLimit;
73         return this;
74     }
75
76     public final Buffer mark() {
77         _mark = _position;
78         return this;
79     }
80
81     public final int position() {
82         return _position;
83     }
84
85     public final Buffer position(int newPosition) {
86         if ((newPosition < 0) || (newPosition > _limit)) {
87             throw new IllegalArgumentException JavaDoc();
88         }
89
90         if (newPosition <= _mark) {
91             _mark = -1;
92         }
93         _position = newPosition;
94         return this;
95     }
96
97     public final int remaining() {
98         return _limit - _position;
99     }
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 }
Popular Tags