comparison src/classes/java/io/InputStreamReader.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
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.
17 */
18
19 package java.io;
20
21
22 /**
23 * how hard can it be to transform byte(s) into a char? I hate Unicode
24 */
25 public class InputStreamReader extends Reader {
26
27 static final int BUF_SIZE=128;
28 private static Object lock = new Object(); // our peer has state
29
30 InputStream in;
31 byte[] bbuf = new byte[BUF_SIZE];
32 String charSetName=null;
33
34 public InputStreamReader (InputStream in){
35 this.in = in;
36 }
37
38 public InputStreamReader (InputStream in,String charSetName){
39 this.in = in;
40 this.charSetName = charSetName;
41 }
42
43 @Override
44 public void close () throws IOException {
45 in.close();
46 }
47
48 private native int decode (int b, boolean endOfInput);
49
50 @Override
51 public boolean ready() {
52 try {
53 return (in.available() > 0);
54 } catch (IOException iox){
55 return false;
56 }
57 }
58
59 @Override
60 public int read () throws IOException {
61 synchronized (lock){
62 while (true){
63
64 int b = in.read();
65 if (b < 0){
66 return -1;
67 }
68
69 int c = decode(b, (in.available() == 0));
70 if (c >= 0 ) {
71 return c;
72 }
73 }
74 }
75 }
76
77
78
79 native int decode (byte[] inBuf,int len,
80 char[] outBuf, int off,
81 boolean endOfInput);
82
83
84 @Override
85 public int read (char[] cbuf, int off, int len) throws IOException {
86 int r = 0;
87 boolean available = true;
88
89 synchronized (lock){
90 while (available && r < len){
91 // <2do> - so what if that backtracks? the peer might have
92 // iteration-specific state that gets lost. see native peer comments
93 int b = in.read(bbuf, 0, Math.min(len-r,bbuf.length));
94 if (b < 0){
95 return (r == 0) ? -1 : r;
96 }
97
98 // true if we have not reach the end of the input stream "in"
99 // and there are still more bytes available to be read
100 available = (in.available() > 0);
101
102 r += decode(bbuf,b, cbuf,off+r, !available);
103 }
104 }
105
106 return r;
107 }
108
109 }