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.CharsetEncoder;
30 * native peer for OutputStreamWriter, to avoid that we have the
31 * char-to-byte conversion in JPF
33 * <2do> this needs to be de-staticed (see model class)
35 public class JPF_java_io_OutputStreamWriter extends NativePeer {
37 static final int BUF_SIZE=128; // needs to be the same as in the model class!
38 static CharsetEncoder encoder;
40 static CharBuffer in = CharBuffer.allocate(BUF_SIZE);
41 static ByteBuffer out = ByteBuffer.allocate(BUF_SIZE*6); // worst case UTF-8
43 public JPF_java_io_OutputStreamWriter() {
44 encoder = Charset.defaultCharset().newEncoder();
48 public int encode___3CII_3B__I (MJIEnv env, int objref,
49 int cref, int off, int len,
51 if (len > BUF_SIZE){ // check for buffer overflow
59 for (int i=off; i<imax; i++){
60 in.put(env.getCharArrayElement(cref, i));
64 encoder.encode(in,out,true);
66 int n = out.position();
67 for (int i=0; i<n; i++){
68 env.setByteArrayElement(bref,i,out.get(i));
75 public int encode__Ljava_lang_String_2II_3B__I (MJIEnv env, int objref,
76 int sref, int off, int len,
78 int cref = env.getReferenceField(sref, "value");
80 return encode___3CII_3B__I(env,objref,cref,off,len,bref);
84 public int encode__C_3B__I (MJIEnv env, int objref, char c, int bufref) {
91 encoder.encode(in,out,true);
93 int n = out.position();
94 for (int i=0; i<n; i++){
95 env.setByteArrayElement(bufref,i,out.get(i));