BinaryOutputArchive.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.IOException;
  20. import java.io.ByteArrayOutputStream;
  21. import java.util.List;
  22. import java.util.TreeMap;
  23. import java.util.ArrayList;
  24. import java.io.DataOutput;
  25. import java.io.DataOutputStream;
  26. import java.io.OutputStream;
  27. import java.nio.ByteBuffer;
  28. /**
  29. *
  30. * @author Milind Bhandarkar
  31. */
  32. public class BinaryOutputArchive implements OutputArchive {
  33. private ByteBuffer bb = ByteBuffer.allocate(1024);
  34. private DataOutput out;
  35. public static BinaryOutputArchive getArchive(OutputStream strm) {
  36. return new BinaryOutputArchive(new DataOutputStream(strm));
  37. }
  38. /** Creates a new instance of BinaryOutputArchive */
  39. public BinaryOutputArchive(DataOutput out) {
  40. this.out = out;
  41. }
  42. public void writeByte(byte b, String tag) throws IOException {
  43. out.writeByte(b);
  44. }
  45. public void writeBool(boolean b, String tag) throws IOException {
  46. out.writeBoolean(b);
  47. }
  48. public void writeInt(int i, String tag) throws IOException {
  49. out.writeInt(i);
  50. }
  51. public void writeLong(long l, String tag) throws IOException {
  52. out.writeLong(l);
  53. }
  54. public void writeFloat(float f, String tag) throws IOException {
  55. out.writeFloat(f);
  56. }
  57. public void writeDouble(double d, String tag) throws IOException {
  58. out.writeDouble(d);
  59. }
  60. /**
  61. * create our own char encoder to utf8. This is faster
  62. * then string.getbytes(UTF8).
  63. * @param s the string to encode into utf8
  64. * @return utf8 byte sequence.
  65. */
  66. final private ByteBuffer stringToByteBuffer(CharSequence s) {
  67. bb.clear();
  68. final int len = s.length();
  69. for (int i = 0; i < len; i++) {
  70. if (bb.remaining() < 3) {
  71. ByteBuffer n = ByteBuffer.allocate(bb.capacity() << 1);
  72. bb.flip();
  73. n.put(bb);
  74. bb = n;
  75. }
  76. char c = s.charAt(i);
  77. if (c < 0x80) {
  78. bb.put((byte) c);
  79. } else if (c < 0x800) {
  80. bb.put((byte) (0xc0 | (c >> 6)));
  81. bb.put((byte) (0x80 | (c & 0x3f)));
  82. } else {
  83. bb.put((byte) (0xe0 | (c >> 12)));
  84. bb.put((byte) (0x80 | ((c >> 6) & 0x3f)));
  85. bb.put((byte) (0x80 | (c & 0x3f)));
  86. }
  87. }
  88. bb.flip();
  89. return bb;
  90. }
  91. public void writeString(String s, String tag) throws IOException {
  92. if (s == null) {
  93. writeInt(-1, "len");
  94. return;
  95. }
  96. ByteBuffer bb = stringToByteBuffer(s);
  97. writeInt(bb.remaining(), "len");
  98. out.write(bb.array(), bb.position(), bb.limit());
  99. }
  100. public void writeBuffer(byte barr[], String tag)
  101. throws IOException {
  102. if (barr == null) {
  103. out.writeInt(-1);
  104. return;
  105. }
  106. out.writeInt(barr.length);
  107. out.write(barr);
  108. }
  109. public void writeRecord(Record r, String tag) throws IOException {
  110. r.serialize(this, tag);
  111. }
  112. public void startRecord(Record r, String tag) throws IOException {}
  113. public void endRecord(Record r, String tag) throws IOException {}
  114. public void startVector(List v, String tag) throws IOException {
  115. if (v == null) {
  116. writeInt(-1, tag);
  117. return;
  118. }
  119. writeInt(v.size(), tag);
  120. }
  121. public void endVector(List v, String tag) throws IOException {}
  122. public void startMap(TreeMap v, String tag) throws IOException {
  123. writeInt(v.size(), tag);
  124. }
  125. public void endMap(TreeMap v, String tag) throws IOException {}
  126. }