BinaryInputArchive.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.yahoo.jute;
  19. import java.io.DataInput;
  20. import java.io.IOException;
  21. import java.io.DataInputStream;
  22. import java.io.InputStream;
  23. /**
  24. *
  25. * @author Milind Bhandarkar
  26. */
  27. public class BinaryInputArchive implements InputArchive {
  28. private DataInput in;
  29. static public BinaryInputArchive getArchive(InputStream strm) {
  30. return new BinaryInputArchive(new DataInputStream(strm));
  31. }
  32. static private class BinaryIndex implements Index {
  33. private int nelems;
  34. BinaryIndex(int nelems) {
  35. this.nelems = nelems;
  36. }
  37. public boolean done() {
  38. return (nelems <= 0);
  39. }
  40. public void incr() {
  41. nelems--;
  42. }
  43. }
  44. /** Creates a new instance of BinaryInputArchive */
  45. public BinaryInputArchive(DataInput in) {
  46. this.in = in;
  47. }
  48. public byte readByte(String tag) throws IOException {
  49. return in.readByte();
  50. }
  51. public boolean readBool(String tag) throws IOException {
  52. return in.readBoolean();
  53. }
  54. public int readInt(String tag) throws IOException {
  55. return in.readInt();
  56. }
  57. public long readLong(String tag) throws IOException {
  58. return in.readLong();
  59. }
  60. public float readFloat(String tag) throws IOException {
  61. return in.readFloat();
  62. }
  63. public double readDouble(String tag) throws IOException {
  64. return in.readDouble();
  65. }
  66. public String readString(String tag) throws IOException {
  67. int len = in.readInt();
  68. if (len == -1) return null;
  69. byte b[] = new byte[len];
  70. in.readFully(b);
  71. return new String(b, "UTF8");
  72. }
  73. static public final int maxBuffer = determineMaxBuffer();
  74. private static int determineMaxBuffer() {
  75. String maxBufferString = System.getProperty("jute.maxbuffer");
  76. try {
  77. return Integer.parseInt(maxBufferString);
  78. } catch(Exception e) {
  79. return 0xfffff;
  80. }
  81. }
  82. public byte[] readBuffer(String tag) throws IOException {
  83. int len = readInt(tag);
  84. if (len == -1) return null;
  85. if (len < 0 || len > maxBuffer) {
  86. throw new RuntimeException("Unreasonable length = " + len);
  87. }
  88. byte[] arr = new byte[len];
  89. in.readFully(arr);
  90. return arr;
  91. }
  92. public void readRecord(Record r, String tag) throws IOException {
  93. r.deserialize(this, tag);
  94. }
  95. public void startRecord(String tag) throws IOException {}
  96. public void endRecord(String tag) throws IOException {}
  97. public Index startVector(String tag) throws IOException {
  98. int len = readInt(tag);
  99. if (len == -1) {
  100. return null;
  101. }
  102. return new BinaryIndex(len);
  103. }
  104. public void endVector(String tag) throws IOException {}
  105. public Index startMap(String tag) throws IOException {
  106. return new BinaryIndex(readInt(tag));
  107. }
  108. public void endMap(String tag) throws IOException {}
  109. }