github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgRpc/kmgRpcJava/java/src/com/google/gson/internal/Streams.java (about)

     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    @SuppressWarnings("resource")
    72    public static Writer writerForAppendable(Appendable appendable) {
    73      return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable);
    74    }
    75  
    76    /**
    77     * Adapts an {@link Appendable} so it can be passed anywhere a {@link Writer}
    78     * is used.
    79     */
    80    private static final class AppendableWriter extends Writer {
    81      private final Appendable appendable;
    82      private final CurrentWrite currentWrite = new CurrentWrite();
    83  
    84      private AppendableWriter(Appendable appendable) {
    85        this.appendable = appendable;
    86      }
    87  
    88      @Override public void write(char[] chars, int offset, int length) throws IOException {
    89        currentWrite.chars = chars;
    90        appendable.append(currentWrite, offset, offset + length);
    91      }
    92  
    93      @Override public void write(int i) throws IOException {
    94        appendable.append((char) i);
    95      }
    96  
    97      @Override public void flush() {}
    98      @Override public void close() {}
    99  
   100      /**
   101       * A mutable char sequence pointing at a single char[].
   102       */
   103      static class CurrentWrite implements CharSequence {
   104        char[] chars;
   105        public int length() {
   106          return chars.length;
   107        }
   108        public char charAt(int i) {
   109          return chars[i];
   110        }
   111        public CharSequence subSequence(int start, int end) {
   112          return new String(chars, start, end - start);
   113        }
   114      }
   115    }
   116  
   117  }