FSOutputSummer.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 org.apache.hadoop.fs;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.zip.Checksum;
  22. /**
  23. * This is a generic output stream for generating checksums for
  24. * data before it is written to the underlying stream
  25. */
  26. abstract public class FSOutputSummer extends OutputStream {
  27. // data checksum
  28. private Checksum sum;
  29. // internal buffer for storing data before it is checksumed
  30. private byte buf[];
  31. // internal buffer for storing checksum
  32. private byte checksum[];
  33. // The number of valid bytes in the buffer.
  34. private int count;
  35. protected FSOutputSummer(Checksum sum, int maxChunkSize, int checksumSize) {
  36. this.sum = sum;
  37. this.buf = new byte[maxChunkSize];
  38. this.checksum = new byte[checksumSize];
  39. this.count = 0;
  40. }
  41. /* write the data chunk in <code>b</code> staring at <code>offset</code> with
  42. * a length of <code>len</code>, and its checksum
  43. */
  44. protected abstract void writeChunk(byte[] b, int offset, int len, byte[] checksum)
  45. throws IOException;
  46. /** Write one byte */
  47. public synchronized void write(int b) throws IOException {
  48. sum.update(b);
  49. buf[count++] = (byte)b;
  50. if(count == buf.length) {
  51. flushBuffer();
  52. }
  53. }
  54. /**
  55. * Writes <code>len</code> bytes from the specified byte array
  56. * starting at offset <code>off</code> and generate a checksum for
  57. * each data chunk.
  58. *
  59. * <p> This method stores bytes from the given array into this
  60. * stream's buffer before it gets checksumed. The buffer gets checksumed
  61. * and flushed to the underlying output stream when all data
  62. * in a checksum chunk are in the buffer. If the buffer is empty and
  63. * requested length is at least as large as the size of next checksum chunk
  64. * size, this method will checksum and write the chunk directly
  65. * to the underlying output stream. Thus it avoids uneccessary data copy.
  66. *
  67. * @param b the data.
  68. * @param off the start offset in the data.
  69. * @param len the number of bytes to write.
  70. * @exception IOException if an I/O error occurs.
  71. */
  72. public synchronized void write(byte b[], int off, int len)
  73. throws IOException {
  74. if (off < 0 || len < 0 || off > b.length - len) {
  75. throw new ArrayIndexOutOfBoundsException();
  76. }
  77. for (int n=0;n<len;n+=write1(b, off+n, len-n)) {
  78. }
  79. }
  80. /*
  81. * Write a portion of an array, flushing to the underlying
  82. * stream at most once if necessary.
  83. */
  84. private int write1(byte b[], int off, int len) throws IOException {
  85. if(count==0 && len>=buf.length) {
  86. // local buffer is empty and user data has one chunk
  87. // checksum and output data
  88. sum.update(b, off, buf.length);
  89. writeChecksumChunk(b, off, buf.length, false);
  90. return buf.length;
  91. }
  92. // copy user data to local buffer
  93. int bytesToCopy = buf.length-count;
  94. bytesToCopy = (len<bytesToCopy) ? len : bytesToCopy;
  95. sum.update(b, off, bytesToCopy);
  96. System.arraycopy(b, off, buf, count, bytesToCopy);
  97. count += bytesToCopy;
  98. if (count == buf.length) {
  99. // local buffer is full
  100. flushBuffer();
  101. }
  102. return bytesToCopy;
  103. }
  104. /* Forces any buffered output bytes to be checksumed and written out to
  105. * the underlying output stream.
  106. */
  107. protected synchronized void flushBuffer() throws IOException {
  108. flushBuffer(false);
  109. }
  110. /* Forces any buffered output bytes to be checksumed and written out to
  111. * the underlying output stream. If keep is true, then the state of
  112. * this object remains intact.
  113. */
  114. protected synchronized void flushBuffer(boolean keep) throws IOException {
  115. if (count != 0) {
  116. int chunkLen = count;
  117. count = 0;
  118. writeChecksumChunk(buf, 0, chunkLen, keep);
  119. if (keep) {
  120. count = chunkLen;
  121. }
  122. }
  123. }
  124. /* Generate checksum for the data chunk and output data chunk & checksum
  125. * to the underlying output stream. If keep is true then keep the
  126. * current ckecksum intact, do not reset it.
  127. */
  128. private void writeChecksumChunk(byte b[], int off, int len, boolean keep)
  129. throws IOException {
  130. int tempChecksum = (int)sum.getValue();
  131. if (!keep) {
  132. sum.reset();
  133. }
  134. checksum[0] = (byte)((tempChecksum >>> 24) & 0xFF);
  135. checksum[1] = (byte)((tempChecksum >>> 16) & 0xFF);
  136. checksum[2] = (byte)((tempChecksum >>> 8) & 0xFF);
  137. checksum[3] = (byte)((tempChecksum >>> 0) & 0xFF);
  138. writeChunk(b, off, len, checksum);
  139. }
  140. }