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 org.msgpack.MessageTypeException;
|
|
21
|
|
22 public final class UnpackerStack {
|
|
23 private int top;
|
|
24 private byte[] types;
|
|
25 private int[] counts;
|
|
26
|
|
27 public static final int MAX_STACK_SIZE = 128;
|
|
28
|
|
29 private static final byte TYPE_INVALID = 0;
|
|
30 private static final byte TYPE_ARRAY = 1;
|
|
31 private static final byte TYPE_MAP = 2;
|
|
32
|
|
33 public UnpackerStack() {
|
|
34 this.top = 0;
|
|
35 this.types = new byte[MAX_STACK_SIZE];
|
|
36 this.counts = new int[MAX_STACK_SIZE];
|
|
37 this.types[0] = TYPE_INVALID;
|
|
38 }
|
|
39
|
|
40 public void pushArray(int size) {
|
|
41 top++;
|
|
42 types[top] = TYPE_ARRAY;
|
|
43 counts[top] = size;
|
|
44 }
|
|
45
|
|
46 public void pushMap(int size) {
|
|
47 top++;
|
|
48 types[top] = TYPE_MAP;
|
|
49 counts[top] = size * 2;
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * throws MessageTypeException if stack is invalid
|
|
54 */
|
|
55 public void checkCount() {
|
|
56 if (counts[top] > 0) {
|
|
57 return;
|
|
58 }
|
|
59
|
|
60 if (types[top] == TYPE_ARRAY) {
|
|
61 throw new MessageTypeException(
|
|
62 "Array is end but readArrayEnd() is not called");
|
|
63 } else if (types[top] == TYPE_MAP) {
|
|
64 throw new MessageTypeException(
|
|
65 "Map is end but readMapEnd() is not called");
|
|
66 } else { // empty
|
|
67 return;
|
|
68 }
|
|
69 }
|
|
70
|
|
71 public void reduceCount() {
|
|
72 counts[top]--;
|
|
73 }
|
|
74
|
|
75 public void pop() {
|
|
76 top--;
|
|
77 }
|
|
78
|
|
79 public int getDepth() {
|
|
80 return top;
|
|
81 }
|
|
82
|
|
83 public int getTopCount() {
|
|
84 return counts[top];
|
|
85 }
|
|
86
|
|
87 public boolean topIsArray() {
|
|
88 return types[top] == TYPE_ARRAY;
|
|
89 }
|
|
90
|
|
91 public boolean topIsMap() {
|
|
92 return types[top] == TYPE_MAP;
|
|
93 }
|
|
94
|
|
95 public void clear() {
|
|
96 top = 0;
|
|
97 }
|
|
98 }
|