forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnpacker.java
More file actions
129 lines (91 loc) · 3.67 KB
/
Copy pathUnpacker.java
File metadata and controls
129 lines (91 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// MessagePack for Java
//
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
//
// 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
//
// 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
// limitations under the License.
//
package org.msgpack.unpacker;
import java.io.InputStream;
import java.io.IOException;
import java.io.EOFException;
import java.nio.ByteBuffer;
import java.math.BigInteger;
import java.util.NoSuchElementException;
import java.lang.Iterable;
import org.msgpack.value.Value;
import org.msgpack.MessagePack;
import org.msgpack.packer.Unconverter;
public abstract class Unpacker implements Iterable<Value> {
protected MessagePack msgpack = new MessagePack(); // TODO initialize
public abstract boolean tryReadNil() throws IOException;
public abstract boolean trySkipNil() throws IOException;
public abstract void readNil() throws IOException;
public abstract boolean readBoolean() throws IOException;
public abstract byte readByte() throws IOException;
public abstract short readShort() throws IOException;
public abstract int readInt() throws IOException;
public abstract long readLong() throws IOException;
public abstract BigInteger readBigInteger() throws IOException;
public abstract float readFloat() throws IOException;
public abstract double readDouble() throws IOException;
public abstract byte[] readByteArray() throws IOException;
public abstract int readArrayBegin() throws IOException;
public abstract void readArrayEnd(boolean check) throws IOException;
public void readArrayEnd() throws IOException {
readArrayEnd(false);
}
public abstract int readMapBegin() throws IOException;
public abstract void readMapEnd(boolean check) throws IOException;
public void readMapEnd() throws IOException {
readMapEnd(false);
}
public String readString() throws IOException {
// TODO encoding exception
return new String(readByteArray(), "UTF-8");
}
public UnpackerIterator iterator() {
return new UnpackerIterator(this);
}
public abstract void skip() throws IOException;
protected abstract void readValue(Unconverter uc) throws IOException;
public Value readValue() throws IOException {
Unconverter uc = new Unconverter();
readValue(uc);
return uc.getResult();
}
public <T> T read(T to) throws IOException {
return (T)msgpack.getTemplate(to.getClass()).read(this, to);
}
public <T> T read(Class<T> klass) throws IOException {
return (T)msgpack.getTemplate(klass).read(this, null);
}
public <T> T readOptional(Class<T> klass, T defaultValue) throws IOException {
if(trySkipNil()) {
return defaultValue;
}
return (T)msgpack.getTemplate(klass).read(this, null);
}
public <T> T readOptional(T to, T defaultValue) throws IOException {
if(trySkipNil()) {
return defaultValue;
}
return (T)msgpack.getTemplate(to.getClass()).read(this, to);
}
public <T> T readOptional(Class<T> klass) throws IOException {
return readOptional(klass, null);
}
public <T> T readOptional(T to) throws IOException {
return readOptional(to, null);
}
}