1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
|
---|
12 | * or packager/legal/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 packager/legal/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 javax.json.*;
|
---|
46 | import java.io.FilterOutputStream;
|
---|
47 | import java.io.IOException;
|
---|
48 | import java.io.OutputStream;
|
---|
49 | import java.io.Writer;
|
---|
50 | import java.nio.charset.Charset;
|
---|
51 | import java.util.Map;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * JsonWriter impl using generator.
|
---|
55 | *
|
---|
56 | * @author Jitendra Kotamraju
|
---|
57 | */
|
---|
58 | class JsonWriterImpl implements JsonWriter {
|
---|
59 | private static final Charset UTF_8 = Charset.forName("UTF-8");
|
---|
60 |
|
---|
61 | private final JsonGeneratorImpl generator;
|
---|
62 | private boolean writeDone;
|
---|
63 | private final NoFlushOutputStream os;
|
---|
64 |
|
---|
65 | JsonWriterImpl(Writer writer, BufferPool bufferPool) {
|
---|
66 | this(writer, false, bufferPool);
|
---|
67 | }
|
---|
68 |
|
---|
69 | JsonWriterImpl(Writer writer, boolean prettyPrinting, BufferPool bufferPool) {
|
---|
70 | generator = prettyPrinting
|
---|
71 | ? new JsonPrettyGeneratorImpl(writer, bufferPool)
|
---|
72 | : new JsonGeneratorImpl(writer, bufferPool);
|
---|
73 | os = null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | JsonWriterImpl(OutputStream out, BufferPool bufferPool) {
|
---|
77 | this(out, UTF_8, false, bufferPool);
|
---|
78 | }
|
---|
79 |
|
---|
80 | JsonWriterImpl(OutputStream out, boolean prettyPrinting, BufferPool bufferPool) {
|
---|
81 | this(out, UTF_8, prettyPrinting, bufferPool);
|
---|
82 | }
|
---|
83 |
|
---|
84 | JsonWriterImpl(OutputStream out, Charset charset,
|
---|
85 | boolean prettyPrinting, BufferPool bufferPool) {
|
---|
86 | // Decorating the given stream, so that buffered contents can be
|
---|
87 | // written without actually flushing the stream.
|
---|
88 | this.os = new NoFlushOutputStream(out);
|
---|
89 | generator = prettyPrinting
|
---|
90 | ? new JsonPrettyGeneratorImpl(os, charset, bufferPool)
|
---|
91 | : new JsonGeneratorImpl(os, charset, bufferPool);
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public void writeArray(JsonArray array) {
|
---|
96 | if (writeDone) {
|
---|
97 | throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
|
---|
98 | }
|
---|
99 | writeDone = true;
|
---|
100 | generator.writeStartArray();
|
---|
101 | for(JsonValue value : array) {
|
---|
102 | generator.write(value);
|
---|
103 | }
|
---|
104 | generator.writeEnd();
|
---|
105 | // Flush the generator's buffered contents. This won't work for byte
|
---|
106 | // streams as intermediary OutputStreamWriter buffers.
|
---|
107 | generator.flushBuffer();
|
---|
108 | // Flush buffered contents but not the byte stream. generator.flush()
|
---|
109 | // does OutputStreamWriter#flushBuffer (package private) and underlying
|
---|
110 | // byte stream#flush(). Here underlying stream's flush() is no-op.
|
---|
111 | if (os != null) {
|
---|
112 | generator.flush();
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public void writeObject(JsonObject object) {
|
---|
118 | if (writeDone) {
|
---|
119 | throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
|
---|
120 | }
|
---|
121 | writeDone = true;
|
---|
122 | generator.writeStartObject();
|
---|
123 | for(Map.Entry<String, JsonValue> e : object.entrySet()) {
|
---|
124 | generator.write(e.getKey(), e.getValue());
|
---|
125 | }
|
---|
126 | generator.writeEnd();
|
---|
127 | // Flush the generator's buffered contents. This won't work for byte
|
---|
128 | // streams as intermediary OutputStreamWriter buffers.
|
---|
129 | generator.flushBuffer();
|
---|
130 | // Flush buffered contents but not the byte stream. generator.flush()
|
---|
131 | // does OutputStreamWriter#flushBuffer (package private) and underlying
|
---|
132 | // byte stream#flush(). Here underlying stream's flush() is no-op.
|
---|
133 | if (os != null) {
|
---|
134 | generator.flush();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void write(JsonStructure value) {
|
---|
140 | if (value instanceof JsonArray) {
|
---|
141 | writeArray((JsonArray)value);
|
---|
142 | } else {
|
---|
143 | writeObject((JsonObject)value);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public void close() {
|
---|
149 | writeDone = true;
|
---|
150 | generator.close();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private static final class NoFlushOutputStream extends FilterOutputStream {
|
---|
154 | public NoFlushOutputStream(OutputStream out) {
|
---|
155 | super(out);
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void write(byte b[], int off, int len) throws IOException {
|
---|
160 | out.write(b, off ,len);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public void flush() {
|
---|
165 | // no-op
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|