PageRenderTime 122ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java

https://gitlab.com/zouxc/dubbo
Java | 138 lines | 98 code | 20 blank | 20 comment | 4 complexity | ca093ed55e0c817c656d90fd706fca67 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.alibaba.dubbo.common.serialize.support.json;
  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.lang.reflect.Type;
  24. import com.alibaba.dubbo.common.serialize.ObjectInput;
  25. import com.alibaba.dubbo.common.utils.PojoUtils;
  26. import com.alibaba.fastjson.JSON;
  27. /**
  28. * JsonObjectInput
  29. *
  30. * @author william.liangf
  31. */
  32. public class FastJsonObjectInput implements ObjectInput {
  33. private final BufferedReader reader;
  34. public FastJsonObjectInput(InputStream in){
  35. this(new InputStreamReader(in));
  36. }
  37. public FastJsonObjectInput(Reader reader){
  38. this.reader = new BufferedReader(reader);
  39. }
  40. public boolean readBool() throws IOException {
  41. try {
  42. return readObject(boolean.class);
  43. } catch (ClassNotFoundException e) {
  44. throw new IOException(e.getMessage());
  45. }
  46. }
  47. public byte readByte() throws IOException {
  48. try {
  49. return readObject( byte.class);
  50. } catch (ClassNotFoundException e) {
  51. throw new IOException(e.getMessage());
  52. }
  53. }
  54. public short readShort() throws IOException {
  55. try {
  56. return readObject(short.class);
  57. } catch (ClassNotFoundException e) {
  58. throw new IOException(e.getMessage());
  59. }
  60. }
  61. public int readInt() throws IOException {
  62. try {
  63. return readObject(int.class);
  64. } catch (ClassNotFoundException e) {
  65. throw new IOException(e.getMessage());
  66. }
  67. }
  68. public long readLong() throws IOException {
  69. try {
  70. return readObject(long.class);
  71. } catch (ClassNotFoundException e) {
  72. throw new IOException(e.getMessage());
  73. }
  74. }
  75. public float readFloat() throws IOException {
  76. try {
  77. return readObject(float.class);
  78. } catch (ClassNotFoundException e) {
  79. throw new IOException(e.getMessage());
  80. }
  81. }
  82. public double readDouble() throws IOException {
  83. try {
  84. return readObject(double.class);
  85. } catch (ClassNotFoundException e) {
  86. throw new IOException(e.getMessage());
  87. }
  88. }
  89. public String readUTF() throws IOException {
  90. try {
  91. return readObject(String.class);
  92. } catch (ClassNotFoundException e) {
  93. throw new IOException(e.getMessage());
  94. }
  95. }
  96. public byte[] readBytes() throws IOException {
  97. return readLine().getBytes();
  98. }
  99. public Object readObject() throws IOException, ClassNotFoundException {
  100. String json = readLine();
  101. return JSON.parse(json);
  102. }
  103. public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
  104. String json = readLine();
  105. return JSON.parseObject(json, cls);
  106. }
  107. @SuppressWarnings("unchecked")
  108. public <T> T readObject(Class<T> cls, Type type) throws IOException,ClassNotFoundException
  109. {
  110. Object value = readObject(cls);
  111. return (T) PojoUtils.realize(value, cls, type);
  112. }
  113. private String readLine() throws IOException, EOFException {
  114. String line = reader.readLine();
  115. if(line == null || line.trim().length() == 0) throw new EOFException();
  116. return line;
  117. }
  118. }