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.unpacker;
|
|
19
|
|
20 import java.io.IOException;
|
|
21 import java.nio.ByteBuffer;
|
|
22 import org.msgpack.type.Value;
|
|
23 import org.msgpack.MessagePack;
|
|
24 import org.msgpack.template.Template;
|
|
25 import org.msgpack.packer.Unconverter;
|
|
26
|
|
27 public abstract class AbstractUnpacker implements Unpacker {
|
|
28 protected MessagePack msgpack;
|
|
29
|
|
30 protected int rawSizeLimit = 134217728;
|
|
31
|
|
32 protected int arraySizeLimit = 4194304;
|
|
33
|
|
34 protected int mapSizeLimit = 2097152;
|
|
35
|
|
36 protected AbstractUnpacker(MessagePack msgpack) {
|
|
37 this.msgpack = msgpack;
|
|
38 }
|
|
39
|
|
40 @Override
|
|
41 public ByteBuffer readByteBuffer() throws IOException {
|
|
42 return ByteBuffer.wrap(readByteArray());
|
|
43 }
|
|
44
|
|
45 @Override
|
|
46 public void readArrayEnd() throws IOException {
|
|
47 readArrayEnd(false);
|
|
48 }
|
|
49
|
|
50 @Override
|
|
51 public void readMapEnd() throws IOException {
|
|
52 readMapEnd(false);
|
|
53 }
|
|
54
|
|
55 @Override
|
|
56 public UnpackerIterator iterator() {
|
|
57 return new UnpackerIterator(this);
|
|
58 }
|
|
59
|
|
60 protected abstract void readValue(Unconverter uc) throws IOException;
|
|
61
|
|
62 @Override
|
|
63 public Value readValue() throws IOException {
|
|
64 Unconverter uc = new Unconverter(msgpack);
|
|
65 readValue(uc);
|
|
66 return uc.getResult();
|
|
67 }
|
|
68
|
|
69 protected abstract boolean tryReadNil() throws IOException;
|
|
70
|
|
71 @Override
|
|
72 public <T> T read(Class<T> klass) throws IOException {
|
|
73 if (tryReadNil()) {
|
|
74 return null;
|
|
75 }
|
|
76 Template<T> tmpl = msgpack.lookup(klass);
|
|
77 return tmpl.read(this, null);
|
|
78 }
|
|
79
|
|
80 @SuppressWarnings("unchecked")
|
|
81 @Override
|
|
82 public <T> T read(T to) throws IOException {
|
|
83 if (tryReadNil()) {
|
|
84 return null;
|
|
85 }
|
|
86 Template<T> tmpl = msgpack.lookup((Class<T>) to.getClass());
|
|
87 return tmpl.read(this, to);
|
|
88 }
|
|
89
|
|
90 @Override
|
|
91 public <T> T read(Template<T> tmpl) throws IOException {
|
|
92 if (tryReadNil()) {
|
|
93 return null;
|
|
94 }
|
|
95 return (T) tmpl.read(this, null);
|
|
96 }
|
|
97
|
|
98 @Override
|
|
99 public <T> T read(T to, Template<T> tmpl) throws IOException {
|
|
100 if (tryReadNil()) {
|
|
101 return null;
|
|
102 }
|
|
103 return (T) tmpl.read(this, to);
|
|
104 }
|
|
105
|
|
106 public int getReadByteCount() {
|
|
107 throw new UnsupportedOperationException("Not implemented");
|
|
108 }
|
|
109
|
|
110 public void resetReadByteCount() {
|
|
111 throw new UnsupportedOperationException("Not implemented");
|
|
112 }
|
|
113
|
|
114 public void setRawSizeLimit(int size) {
|
|
115 if (size < 32) {
|
|
116 rawSizeLimit = 32;
|
|
117 } else {
|
|
118 rawSizeLimit = size;
|
|
119 }
|
|
120 }
|
|
121
|
|
122 public void setArraySizeLimit(int size) {
|
|
123 if (size < 16) {
|
|
124 arraySizeLimit = 16;
|
|
125 } else {
|
|
126 arraySizeLimit = size;
|
|
127 }
|
|
128 }
|
|
129
|
|
130 public void setMapSizeLimit(int size) {
|
|
131 if (size < 16) {
|
|
132 mapSizeLimit = 16;
|
|
133 } else {
|
|
134 mapSizeLimit = size;
|
|
135 }
|
|
136 }
|
|
137 }
|