2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.vm;
20 import gov.nasa.jpf.annotation.MJI;
21 import gov.nasa.jpf.vm.MJIEnv;
22 import gov.nasa.jpf.vm.NativePeer;
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.charset.Charset;
27 import java.nio.charset.CharsetDecoder;
29 public class JPF_java_io_InputStreamReader extends NativePeer {
31 static final int BUF_SIZE = 128;
33 // <2do> decoder should be stored on a per-reader basis, since Charsets
35 CharsetDecoder decoder;
37 // ..same here - that's a shared resource with state! Only works for now
38 // since all InputStreamReader decoding is protected by the same lock
39 ByteBuffer in = ByteBuffer.allocate(BUF_SIZE);
40 CharBuffer out = CharBuffer.allocate(BUF_SIZE);
42 public JPF_java_io_InputStreamReader() {
43 decoder = Charset.defaultCharset().newDecoder();
47 public int decode___3BI_3CIZ__I (MJIEnv env, int objref,
48 int bref, int len, int cref, int off,
53 if (lim < in.capacity()){ // left-over bytes
59 for (int i=0; i<len; i++){
60 in.put(env.getByteArrayElement(bref,i));
64 decoder.decode(in,out,endOfInput);
66 int n = out.position();
67 for (int i=0, j=off; i<n; i++,j++){
68 env.setCharArrayElement(cref,j, out.get(i));
79 // <2do> - that fails if we have a multi byte char and there is a backtrack
80 // between decode() calls. Granted, that seems strange, but there is an
81 // InputStream.read() in the loop which might just branch into user code
83 public int decode__IZ__I (MJIEnv env, int objref, int b, boolean endOfInput){
87 // this is terrible overhead to get a single char, I must be doing something wrong..
89 if (lim < in.capacity()){ // left-over bytes
99 decoder.decode(in,out,endOfInput);
101 if (out.position() == 1){