0
|
1 //
|
|
2 // MessagePack for Java
|
|
3 //
|
|
4 // Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
|
|
5 //
|
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
|
7 // you may not use this file except in compliance with the License.
|
|
8 // 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 package org.msgpack.io;
|
|
19
|
|
20 import java.io.InputStream;
|
|
21 import java.nio.ByteBuffer;
|
|
22 import java.io.IOException;
|
|
23 import java.io.EOFException;
|
|
24
|
|
25 public class StreamInput extends AbstractInput {
|
|
26 private final InputStream in;
|
|
27
|
|
28 private byte[] castBuffer;
|
|
29 private ByteBuffer castByteBuffer;
|
|
30 private int filled;
|
|
31
|
|
32 public StreamInput(InputStream in) {
|
|
33 this.in = in;
|
|
34 this.castBuffer = new byte[8];
|
|
35 this.castByteBuffer = ByteBuffer.wrap(castBuffer);
|
|
36 this.filled = 0;
|
|
37 }
|
|
38
|
|
39 public int read(byte[] b, int off, int len) throws IOException {
|
|
40 int remain = len;
|
|
41 while (remain > 0) {
|
|
42 int n = in.read(b, off, remain);
|
|
43 if (n <= 0) {
|
|
44 throw new EOFException();
|
|
45 }
|
|
46 incrReadByteCount(n);
|
|
47 remain -= n;
|
|
48 off += n;
|
|
49 }
|
|
50 return len;
|
|
51 }
|
|
52
|
|
53 public boolean tryRefer(BufferReferer ref, int size) throws IOException {
|
|
54 return false;
|
|
55 }
|
|
56
|
|
57 public byte readByte() throws IOException {
|
|
58 int n = in.read();
|
|
59 if (n < 0) {
|
|
60 throw new EOFException();
|
|
61 }
|
|
62 incrReadOneByteCount();
|
|
63 return (byte) n;
|
|
64 }
|
|
65
|
|
66 public void advance() {
|
|
67 incrReadByteCount(filled);
|
|
68 filled = 0;
|
|
69 }
|
|
70
|
|
71 private void require(int len) throws IOException {
|
|
72 while (filled < len) {
|
|
73 int n = in.read(castBuffer, filled, len - filled);
|
|
74 if (n < 0) {
|
|
75 throw new EOFException();
|
|
76 }
|
|
77 filled += n;
|
|
78 }
|
|
79 }
|
|
80
|
|
81 public byte getByte() throws IOException {
|
|
82 require(1);
|
|
83 return castBuffer[0];
|
|
84 }
|
|
85
|
|
86 public short getShort() throws IOException {
|
|
87 require(2);
|
|
88 return castByteBuffer.getShort(0);
|
|
89 }
|
|
90
|
|
91 public int getInt() throws IOException {
|
|
92 require(4);
|
|
93 return castByteBuffer.getInt(0);
|
|
94 }
|
|
95
|
|
96 public long getLong() throws IOException {
|
|
97 require(8);
|
|
98 return castByteBuffer.getLong(0);
|
|
99 }
|
|
100
|
|
101 public float getFloat() throws IOException {
|
|
102 require(4);
|
|
103 return castByteBuffer.getFloat(0);
|
|
104 }
|
|
105
|
|
106 public double getDouble() throws IOException {
|
|
107 require(8);
|
|
108 return castByteBuffer.getDouble(0);
|
|
109 }
|
|
110
|
|
111 public void close() throws IOException {
|
|
112 in.close();
|
|
113 }
|
|
114 }
|