1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the terms of either the GNU
|
---|
7 | * General Public License Version 2 only ("GPL") or the Common Development
|
---|
8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
---|
9 | * may not use this file except in compliance with the License. You can
|
---|
10 | * obtain a copy of the License at
|
---|
11 | * https://oss.oracle.com/licenses/CDDL+GPL-1.1
|
---|
12 | * or LICENSE.txt. See the License for the specific
|
---|
13 | * language governing permissions and limitations under the License.
|
---|
14 | *
|
---|
15 | * When distributing the software, include this License Header Notice in each
|
---|
16 | * file and include the License file at LICENSE.txt.
|
---|
17 | *
|
---|
18 | * GPL Classpath Exception:
|
---|
19 | * Oracle designates this particular file as subject to the "Classpath"
|
---|
20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
---|
21 | * file that accompanied this code.
|
---|
22 | *
|
---|
23 | * Modifications:
|
---|
24 | * If applicable, add the following below the License Header, with the fields
|
---|
25 | * enclosed by brackets [] replaced by your own identifying information:
|
---|
26 | * "Portions Copyright [year] [name of copyright owner]"
|
---|
27 | *
|
---|
28 | * Contributor(s):
|
---|
29 | * If you wish your version of this file to be governed by only the CDDL or
|
---|
30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
---|
31 | * elects to include this software in this distribution under the [CDDL or GPL
|
---|
32 | * Version 2] license." If you don't indicate a single choice of license, a
|
---|
33 | * recipient has the option to distribute your version of this file under
|
---|
34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
---|
35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
---|
36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
---|
37 | * only if the new code is made subject to such option by the copyright
|
---|
38 | * holder.
|
---|
39 | */
|
---|
40 |
|
---|
41 | package org.glassfish.json;
|
---|
42 |
|
---|
43 | import org.glassfish.json.api.BufferPool;
|
---|
44 |
|
---|
45 | import java.io.InputStream;
|
---|
46 | import java.io.Reader;
|
---|
47 | import java.nio.charset.Charset;
|
---|
48 | import javax.json.JsonArray;
|
---|
49 | import javax.json.JsonException;
|
---|
50 | import javax.json.JsonObject;
|
---|
51 | import javax.json.JsonReader;
|
---|
52 | import javax.json.JsonStructure;
|
---|
53 | import javax.json.JsonValue;
|
---|
54 | import javax.json.stream.JsonParser;
|
---|
55 | import javax.json.stream.JsonParsingException;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * JsonReader impl using parser and builders.
|
---|
59 | *
|
---|
60 | * @author Jitendra Kotamraju
|
---|
61 | */
|
---|
62 | class JsonReaderImpl implements JsonReader {
|
---|
63 | private final JsonParserImpl parser;
|
---|
64 | private boolean readDone;
|
---|
65 | private final BufferPool bufferPool;
|
---|
66 |
|
---|
67 | JsonReaderImpl(Reader reader, BufferPool bufferPool) {
|
---|
68 | parser = new JsonParserImpl(reader, bufferPool);
|
---|
69 | this.bufferPool = bufferPool;
|
---|
70 | }
|
---|
71 |
|
---|
72 | JsonReaderImpl(InputStream in, BufferPool bufferPool) {
|
---|
73 | parser = new JsonParserImpl(in, bufferPool);
|
---|
74 | this.bufferPool = bufferPool;
|
---|
75 | }
|
---|
76 |
|
---|
77 | JsonReaderImpl(InputStream in, Charset charset, BufferPool bufferPool) {
|
---|
78 | parser = new JsonParserImpl(in, charset, bufferPool);
|
---|
79 | this.bufferPool = bufferPool;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public JsonStructure read() {
|
---|
84 | if (readDone) {
|
---|
85 | throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
|
---|
86 | }
|
---|
87 | readDone = true;
|
---|
88 | if (parser.hasNext()) {
|
---|
89 | try {
|
---|
90 | JsonParser.Event e = parser.next();
|
---|
91 | if (e == JsonParser.Event.START_ARRAY) {
|
---|
92 | return parser.getArray();
|
---|
93 | } else if (e == JsonParser.Event.START_OBJECT) {
|
---|
94 | return parser.getObject();
|
---|
95 | }
|
---|
96 | } catch (IllegalStateException ise) {
|
---|
97 | throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
|
---|
98 | }
|
---|
99 | }
|
---|
100 | throw new JsonException(JsonMessages.INTERNAL_ERROR());
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public JsonObject readObject() {
|
---|
105 | if (readDone) {
|
---|
106 | throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
|
---|
107 | }
|
---|
108 | readDone = true;
|
---|
109 | if (parser.hasNext()) {
|
---|
110 | try {
|
---|
111 | parser.next();
|
---|
112 | return parser.getObject();
|
---|
113 | } catch (IllegalStateException ise) {
|
---|
114 | throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
|
---|
115 | }
|
---|
116 | }
|
---|
117 | throw new JsonException(JsonMessages.INTERNAL_ERROR());
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public JsonArray readArray() {
|
---|
122 | if (readDone) {
|
---|
123 | throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
|
---|
124 | }
|
---|
125 | readDone = true;
|
---|
126 | if (parser.hasNext()) {
|
---|
127 | try {
|
---|
128 | parser.next();
|
---|
129 | return parser.getArray();
|
---|
130 | } catch (IllegalStateException ise) {
|
---|
131 | throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
|
---|
132 | }
|
---|
133 | }
|
---|
134 | throw new JsonException(JsonMessages.INTERNAL_ERROR());
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public JsonValue readValue() {
|
---|
139 | if (readDone) {
|
---|
140 | throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
|
---|
141 | }
|
---|
142 | readDone = true;
|
---|
143 | if (parser.hasNext()) {
|
---|
144 | try {
|
---|
145 | parser.next();
|
---|
146 | return parser.getValue();
|
---|
147 | } catch (IllegalStateException ise) {
|
---|
148 | throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
|
---|
149 | }
|
---|
150 | }
|
---|
151 | throw new JsonException(JsonMessages.INTERNAL_ERROR());
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public void close() {
|
---|
156 | readDone = true;
|
---|
157 | parser.close();
|
---|
158 | }
|
---|
159 | }
|
---|