|
| 1 | +// |
| 2 | +// MessagePack for Java |
| 3 | +// |
| 4 | +// Copyright (C) 2009-2011 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.EOFException; |
| 21 | +import java.math.BigInteger; |
| 22 | +import org.msgpack.MessageTypeException; |
| 23 | +import org.msgpack.packer.Unconverter; |
| 24 | +import org.msgpack.value.Value; |
| 25 | +import org.msgpack.value.ArrayValue; |
| 26 | +import org.msgpack.value.MapValue; |
| 27 | + |
| 28 | +public class Converter extends Unpacker { |
| 29 | + private final UnpackerStack stack; |
| 30 | + private Object[] values; |
| 31 | + private Value value; |
| 32 | + |
| 33 | + public Converter(Value value) { |
| 34 | + this.stack = new UnpackerStack(); |
| 35 | + this.values = new Object[UnpackerStack.MAX_STACK_SIZE]; |
| 36 | + this.values[0] = value; |
| 37 | + this.value = value; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void readNil() { |
| 42 | + if(!tryReadNil()) { |
| 43 | + throw new MessageTypeException("Expected nil but got not nil value"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean tryReadNil() { |
| 49 | + if(getTop().isNil()) { |
| 50 | + stack.reduceCount(); |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean readBoolean() { |
| 58 | + boolean v = getTop().asBooleanValue().getBoolean(); |
| 59 | + stack.reduceCount(); |
| 60 | + return v; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public byte readByte() { |
| 65 | + byte v = getTop().asIntegerValue().getByte(); |
| 66 | + stack.reduceCount(); |
| 67 | + return v; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public short readShort() { |
| 72 | + short v = getTop().asIntegerValue().getShort(); |
| 73 | + stack.reduceCount(); |
| 74 | + return v; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int readInt() { |
| 79 | + int v = getTop().asIntegerValue().getInt(); |
| 80 | + stack.reduceCount(); |
| 81 | + return v; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public long readLong() { |
| 86 | + long v = getTop().asIntegerValue().getLong(); |
| 87 | + stack.reduceCount(); |
| 88 | + return v; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public BigInteger readBigInteger() { |
| 93 | + BigInteger v = getTop().asIntegerValue().getBigInteger(); |
| 94 | + stack.reduceCount(); |
| 95 | + return v; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public float readFloat() { |
| 100 | + float v = getTop().asFloatValue().getFloat(); |
| 101 | + stack.reduceCount(); |
| 102 | + return v; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public double readDouble() { |
| 107 | + double v = getTop().asFloatValue().getDouble(); |
| 108 | + stack.reduceCount(); |
| 109 | + return v; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public byte[] readByteArray() { |
| 114 | + byte[] raw = getTop().asRawValue().getByteArray(); |
| 115 | + stack.reduceCount(); |
| 116 | + return raw; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public int readArrayBegin() { |
| 121 | + Value v = getTop(); |
| 122 | + if(!v.isArray()) { |
| 123 | + throw new MessageTypeException("Expected array but got not array value"); |
| 124 | + } |
| 125 | + ArrayValue a = v.asArrayValue(); |
| 126 | + values[stack.getDepth()] = a.getElementArray(); |
| 127 | + stack.pushArray(a.size()); |
| 128 | + return a.size(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void readArrayEnd(boolean check) { |
| 133 | + if(!stack.topIsArray()) { |
| 134 | + throw new MessageTypeException("readArrayEnd() is called but readArrayBegin() is not called"); |
| 135 | + } |
| 136 | + |
| 137 | + int remain = stack.getTopCount(); |
| 138 | + if(remain > 0) { |
| 139 | + if(check) { |
| 140 | + throw new MessageTypeException("readArrayEnd(check=true) is called but the array is not end"); |
| 141 | + } |
| 142 | + for(int i=0; i < remain; i++) { |
| 143 | + skip(); |
| 144 | + } |
| 145 | + } |
| 146 | + stack.pop(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public int readMapBegin() { |
| 151 | + Value v = getTop(); |
| 152 | + if(!v.isArray()) { |
| 153 | + throw new MessageTypeException("Expected array but got not array value"); |
| 154 | + } |
| 155 | + MapValue m = v.asMapValue(); |
| 156 | + values[stack.getDepth()] = m.getKeyValueArray(); |
| 157 | + stack.pushMap(m.size()); |
| 158 | + return m.size(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void readMapEnd(boolean check) { |
| 163 | + if(!stack.topIsMap()) { |
| 164 | + throw new MessageTypeException("readMapEnd() is called but readMapBegin() is not called"); |
| 165 | + } |
| 166 | + |
| 167 | + int remain = stack.getTopCount(); |
| 168 | + if(remain > 0) { |
| 169 | + if(check) { |
| 170 | + throw new MessageTypeException("readMapEnd(check=true) is called but the map is not end"); |
| 171 | + } |
| 172 | + for(int i=0; i < remain; i++) { |
| 173 | + skip(); |
| 174 | + } |
| 175 | + } |
| 176 | + stack.pop(); |
| 177 | + } |
| 178 | + |
| 179 | + private Value getTop() { |
| 180 | + stack.checkCount(); |
| 181 | + if(stack.getDepth() == 0) { |
| 182 | + if(stack.getTopCount() < 0) { |
| 183 | + //throw new EOFException(); // TODO |
| 184 | + throw new RuntimeException(new EOFException()); |
| 185 | + } |
| 186 | + return (Value) values[0]; |
| 187 | + } |
| 188 | + Value[] array = (Value[]) values[stack.getDepth()]; |
| 189 | + return array[stack.getTopCount()]; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + protected void readValue(Unconverter uc) { |
| 194 | + // FIXME |
| 195 | + if(uc.getResult() != null) { |
| 196 | + uc.resetResult(); |
| 197 | + } |
| 198 | + |
| 199 | + Value v = getTop(); |
| 200 | + if(!v.isArray() && !v.isMap()) { |
| 201 | + stack.reduceCount(); |
| 202 | + if(uc.getResult() != null) { |
| 203 | + return; |
| 204 | + } |
| 205 | + v = getTop(); |
| 206 | + } |
| 207 | + |
| 208 | + while(true) { |
| 209 | + if(v.isArray()) { |
| 210 | + ArrayValue a = v.asArrayValue(); |
| 211 | + uc.writeArrayBegin(a.size()); |
| 212 | + values[stack.getDepth()] = a.getElementArray(); |
| 213 | + stack.pushArray(a.size()); |
| 214 | + |
| 215 | + } else if(v.isMap()) { |
| 216 | + MapValue m = v.asMapValue(); |
| 217 | + uc.writeMapBegin(m.size()); |
| 218 | + values[stack.getDepth()] = m.getKeyValueArray(); |
| 219 | + stack.pushMap(m.size()); |
| 220 | + |
| 221 | + } else { |
| 222 | + uc.write(v); |
| 223 | + stack.reduceCount(); |
| 224 | + if(stack.getTopCount() == 0) { |
| 225 | + if(stack.topIsArray()) { |
| 226 | + uc.writeArrayEnd(true); |
| 227 | + stack.pop(); |
| 228 | + } else if(stack.topIsMap()) { |
| 229 | + uc.writeMapEnd(true); |
| 230 | + stack.pop(); |
| 231 | + } else { |
| 232 | + // FIXME error? |
| 233 | + } |
| 234 | + if(uc.getResult() != null) { |
| 235 | + return; |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + v = getTop(); |
| 240 | + } |
| 241 | + |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public void skip() { |
| 246 | + // FIXME |
| 247 | + Value v = getTop(); |
| 248 | + if(!v.isArray() && !v.isMap()) { |
| 249 | + stack.reduceCount(); |
| 250 | + return; |
| 251 | + } |
| 252 | + int targetDepth = stack.getDepth(); |
| 253 | + while(true) { |
| 254 | + if(v.isArray()) { |
| 255 | + ArrayValue a = v.asArrayValue(); |
| 256 | + values[stack.getDepth()] = a.getElementArray(); |
| 257 | + stack.pushArray(a.size()); |
| 258 | + |
| 259 | + } else if(v.isMap()) { |
| 260 | + MapValue m = v.asMapValue(); |
| 261 | + values[stack.getDepth()] = m.getKeyValueArray(); |
| 262 | + stack.pushMap(m.size()); |
| 263 | + |
| 264 | + } else { |
| 265 | + stack.reduceCount(); |
| 266 | + if(stack.getTopCount() == 0) { |
| 267 | + stack.pop(); |
| 268 | + if(stack.getDepth() <= targetDepth) { |
| 269 | + return; |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + v = getTop(); |
| 274 | + } |
| 275 | + } |
| 276 | +} |
| 277 | + |
0 commit comments