Skip to content

Commit

Permalink
Apply patch for #171, to add support for java.nio.Buffer (#409)
Browse files Browse the repository at this point in the history
* Apply patch for #171, to add support for java.nio.Buffer

* Add support for CharBuffer, a unit test for ByteBuffer constructor and refactor the existing ByteBufferTest to BufferTest

* Add CharBuffer to java/nio

---------

Co-authored-by: varad64 <[email protected]>
  • Loading branch information
varad64 and varad64 authored Aug 18, 2023
1 parent 4654787 commit 9efc548
Show file tree
Hide file tree
Showing 9 changed files with 619 additions and 341 deletions.
145 changes: 82 additions & 63 deletions src/classes/modules/java.base/java/nio/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,95 @@
* The Java Pathfinder core (jpf-core) platform is licensed under the
* Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.nio;

public abstract class Buffer {
protected int position;
protected int capacity;
protected int limit;

public final int capacity() {
return capacity;
}

public final Buffer position(int newPosition) {
if ((newPosition<0)||(newPosition>limit)) {
throw new IllegalArgumentException("Illegal buffer position exception: "+newPosition);
}
this.position = newPosition;
return this;
}

public final int position() {
return position;
}

public final int limit() {
return this.limit;
}

public final Buffer limit(int newLimit) {
if ((newLimit<0)||(newLimit>capacity)) {
throw new IllegalArgumentException("Illegal buffer limit exception: "+newLimit);
}
this.limit = newLimit;
return this;
}

public final Buffer clear(){
position = 0;
limit = capacity;
return this;
}

public final Buffer flip() {
limit = position;
position = 0;
return this;
}

public final Buffer rewind() {
position = 0;
return this;
}

public final int remaining() {
return limit-position;
}

public final boolean hasRemaining() {
return remaining()>0;
}

public abstract boolean hasArray();

public abstract Object array();

protected int position = 0;
protected int capacity;
protected int limit;
protected int mark = -1;
long address;


// Todo: There exists other missing methods in this class
// which may throw NoSuchMethodException errors to users
// For example checkBounds has yet to be implemented
Buffer(int mark, int pos, int lim, int cap) {
if (cap < 0)
throw new IllegalArgumentException("Negative capacity: " + cap);
this.capacity = cap;
limit(lim);
position(pos);
if (mark >= 0 && mark <= pos){
this.mark = mark;
}
}

public final int capacity() {
return capacity;
}

public Buffer position(int newPosition) {
if ((newPosition<0)||(newPosition>limit)) {
throw new IllegalArgumentException("Illegal buffer position exception: "+newPosition);
}
this.position = newPosition;
return this;
}

public final int position() {
return position;
}

public int limit() {
return this.limit;
}

public Buffer limit(int newLimit) {
if ((newLimit<0)||(newLimit>capacity)) {
throw new IllegalArgumentException("Illegal buffer limit exception: "+newLimit);
}
this.limit = newLimit;
return this;
}

public final Buffer clear(){
position = 0;
limit = capacity;
mark = -1;
return this;
}

public Buffer flip() {
limit = position;
position = 0;
return this;
}

public final Buffer rewind() {
position = 0;
return this;
}

public final int remaining() {
return limit-position;
}

public final boolean hasRemaining() {
return remaining()>0;
}

public abstract boolean hasArray();

public abstract Object array();
}
Loading

0 comments on commit 9efc548

Please sign in to comment.