| 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.gson.internal; |
| 18 | |
| 19 | import com.google.gson.JsonElement; |
| 20 | import com.google.gson.JsonIOException; |
| 21 | import com.google.gson.JsonNull; |
| 22 | import com.google.gson.JsonParseException; |
| 23 | import com.google.gson.JsonSyntaxException; |
| 24 | import com.google.gson.internal.bind.TypeAdapters; |
| 25 | import com.google.gson.stream.JsonReader; |
| 26 | import com.google.gson.stream.JsonWriter; |
| 27 | import com.google.gson.stream.MalformedJsonException; |
| 28 | import java.io.EOFException; |
| 29 | import java.io.IOException; |
| 30 | import java.io.Writer; |
| 31 | |
| 32 | /** |
| 33 | * Reads and writes GSON parse trees over streams. |
| 34 | */ |
| 35 | public final class Streams { |
| 36 | /** |
| 37 | * Takes a reader in any state and returns the next value as a JsonElement. |
| 38 | */ |
| 39 | public static JsonElement parse(JsonReader reader) throws JsonParseException { |
| 40 | boolean isEmpty = true; |
| 41 | try { |
| 42 | reader.peek(); |
| 43 | isEmpty = false; |
| 44 | return TypeAdapters.JSON_ELEMENT.read(reader); |
| 45 | } catch (EOFException e) { |
| 46 | /* |
| 47 | * For compatibility with JSON 1.5 and earlier, we return a JsonNull for |
| 48 | * empty documents instead of throwing. |
| 49 | */ |
| 50 | if (isEmpty) { |
| 51 | return JsonNull.INSTANCE; |
| 52 | } |
| 53 | // The stream ended prematurely so it is likely a syntax error. |
| 54 | throw new JsonSyntaxException(e); |
| 55 | } catch (MalformedJsonException e) { |
| 56 | throw new JsonSyntaxException(e); |
| 57 | } catch (IOException e) { |
| 58 | throw new JsonIOException(e); |
| 59 | } catch (NumberFormatException e) { |
| 60 | throw new JsonSyntaxException(e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Writes the JSON element to the writer, recursively. |
| 66 | */ |
| 67 | public static void write(JsonElement element, JsonWriter writer) throws IOException { |
| 68 | TypeAdapters.JSON_ELEMENT.write(writer, element); |
| 69 | } |
| 70 | |
| 71 | public static Writer writerForAppendable(Appendable appendable) { |
| 72 | return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Adapts an {@link Appendable} so it can be passed anywhere a {@link Writer} |
| 77 | * is used. |
| 78 | */ |
| 79 | private static final class AppendableWriter extends Writer { |
| 80 | private final Appendable appendable; |
| 81 | private final CurrentWrite currentWrite = new CurrentWrite(); |
| 82 | |
| 83 | private AppendableWriter(Appendable appendable) { |
| 84 | this.appendable = appendable; |
| 85 | } |
| 86 | |
| 87 | @Override public void write(char[] chars, int offset, int length) throws IOException { |
| 88 | currentWrite.chars = chars; |
| 89 | appendable.append(currentWrite, offset, offset + length); |
| 90 | } |
| 91 | |
| 92 | @Override public void write(int i) throws IOException { |
| 93 | appendable.append((char) i); |
| 94 | } |
| 95 | |
| 96 | @Override public void flush() {} |
| 97 | @Override public void close() {} |
| 98 | |
| 99 | /** |
| 100 | * A mutable char sequence pointing at a single char[]. |
| 101 | */ |
| 102 | static class CurrentWrite implements CharSequence { |
| 103 | char[] chars; |
| 104 | public int length() { |
| 105 | return chars.length; |
| 106 | } |
| 107 | public char charAt(int i) { |
| 108 | return chars[i]; |
| 109 | } |
| 110 | public CharSequence subSequence(int start, int end) { |
| 111 | return new String(chars, start, end - start); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |