/src/main/java/com/redcreen/rpcplus/handler/codec/serialize/fastjson/FastJsonObjectInput.java

https://github.com/redcreen/rpcplus · Java · 135 lines · 97 code · 19 blank · 19 comment · 6 complexity · 2df4bd7a950e6d287576bb7471ffe8e4 MD5 · raw file

  1. /*
  2. * Copyright 1999-2011 Alibaba Group.
  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. package com.redcreen.rpcplus.handler.codec.serialize.fastjson;
  17. import java.io.BufferedReader;
  18. import java.io.EOFException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.Reader;
  23. import java.util.Map;
  24. import com.alibaba.fastjson.JSON;
  25. import com.redcreen.rpcplus.handler.codec.serialize.ObjectInput;
  26. /**
  27. * JsonObjectInput
  28. *
  29. */
  30. public class FastJsonObjectInput implements ObjectInput {
  31. private final BufferedReader reader;
  32. public FastJsonObjectInput(InputStream in){
  33. this(new InputStreamReader(in));
  34. }
  35. public FastJsonObjectInput(Reader reader){
  36. this.reader = new BufferedReader(reader);
  37. }
  38. public boolean readBool() throws IOException {
  39. try {
  40. return readObject(boolean.class);
  41. } catch (ClassNotFoundException e) {
  42. throw new IOException(e.getMessage());
  43. }
  44. }
  45. public byte readByte() throws IOException {
  46. try {
  47. return readObject( byte.class);
  48. } catch (ClassNotFoundException e) {
  49. throw new IOException(e.getMessage());
  50. }
  51. }
  52. public short readShort() throws IOException {
  53. try {
  54. return readObject(short.class);
  55. } catch (ClassNotFoundException e) {
  56. throw new IOException(e.getMessage());
  57. }
  58. }
  59. public int readInt() throws IOException {
  60. try {
  61. return readObject(int.class);
  62. } catch (ClassNotFoundException e) {
  63. throw new IOException(e.getMessage());
  64. }
  65. }
  66. public long readLong() throws IOException {
  67. try {
  68. return readObject(long.class);
  69. } catch (ClassNotFoundException e) {
  70. throw new IOException(e.getMessage());
  71. }
  72. }
  73. public float readFloat() throws IOException {
  74. try {
  75. return readObject(float.class);
  76. } catch (ClassNotFoundException e) {
  77. throw new IOException(e.getMessage());
  78. }
  79. }
  80. public double readDouble() throws IOException {
  81. try {
  82. return readObject(double.class);
  83. } catch (ClassNotFoundException e) {
  84. throw new IOException(e.getMessage());
  85. }
  86. }
  87. public String readUTF() throws IOException {
  88. try {
  89. return readObject(String.class);
  90. } catch (ClassNotFoundException e) {
  91. throw new IOException(e.getMessage());
  92. }
  93. }
  94. public byte[] readBytes() throws IOException {
  95. return readLine().getBytes();
  96. }
  97. @SuppressWarnings("unchecked")
  98. public Object readObject() throws IOException, ClassNotFoundException {
  99. String json = readLine();
  100. if (json.startsWith("{")) {
  101. return JSON.parseObject(json, Map.class);
  102. } else {
  103. json = "{\"value\":" + json + "}";
  104. Map<String, Object> map = JSON.parseObject(json, Map.class);
  105. return map.get("value");
  106. }
  107. }
  108. public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
  109. return JSON.parseObject(readLine(), cls);
  110. }
  111. private String readLine() throws IOException, EOFException {
  112. String line = reader.readLine();
  113. if(line == null || line.trim().length() == 0) throw new EOFException();
  114. return line;
  115. }
  116. }