001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.nio.ByteBuffer;
023import java.util.Objects;
024import org.apache.hadoop.hbase.io.util.StreamUtils;
025import org.apache.hadoop.hbase.util.ByteBufferUtils;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * When deal with OutputStream which is not ByteBufferWriter type, wrap it with this class. We will
030 * have to write offheap ByteBuffer (DBB) data into the OS. This class is having a temp byte array
031 * to which we can copy the DBB data for writing to the OS. <br>
032 * This is used while writing Cell data to WAL. In case of AsyncWAL, the OS created there is
033 * ByteBufferWriter. But in case of FSHLog, the OS passed by DFS client, is not of type
034 * ByteBufferWriter. We will need this temp solution until DFS client supports writing ByteBuffer
035 * directly to the OS it creates. <br>
036 * Note: This class is not thread safe.
037 */
038@InterfaceAudience.Private
039public class ByteBufferWriterOutputStream extends OutputStream implements ByteBufferWriter {
040
041  private static final int DEFAULT_BUFFER_SIZE = 4096;
042
043  private final OutputStream os;
044  private final int bufSize;
045  private byte[] buf;
046
047  public ByteBufferWriterOutputStream(OutputStream os) {
048    this(os, DEFAULT_BUFFER_SIZE);
049  }
050
051  public ByteBufferWriterOutputStream(OutputStream os, int size) {
052    this.os = os;
053    this.bufSize = size;
054    this.buf = null;
055  }
056
057  /**
058   * Writes len bytes from the specified ByteBuffer starting at offset off to this OutputStream. If
059   * off is negative or larger than the ByteBuffer then an ArrayIndexOutOfBoundsException is thrown.
060   * If len is greater than the length of the ByteBuffer, then an ArrayIndexOutOfBoundsException is
061   * thrown. This method does not change the position of the ByteBuffer.
062   * @param b   the ByteBuffer
063   * @param off the start offset in the data
064   * @param len the number of bytes to write
065   * @throws IOException          if an I/O error occurs. In particular, an IOException is thrown if
066   *                              the output stream is closed.
067   * @throws NullPointerException if {@code b} is {@code null}
068   */
069  @Override
070  public void write(ByteBuffer b, int off, int len) throws IOException {
071    Objects.requireNonNull(b);
072
073    // Lazily load in the event that this version of 'write' is not invoked
074    if (this.buf == null) {
075      this.buf = new byte[this.bufSize];
076    }
077    int totalCopied = 0;
078    while (totalCopied < len) {
079      int bytesToCopy = Math.min((len - totalCopied), this.bufSize);
080      ByteBufferUtils.copyFromBufferToArray(this.buf, b, off + totalCopied, 0, bytesToCopy);
081      this.os.write(this.buf, 0, bytesToCopy);
082      totalCopied += bytesToCopy;
083    }
084  }
085
086  @Override
087  public void writeInt(int i) throws IOException {
088    StreamUtils.writeInt(this.os, i);
089  }
090
091  @Override
092  public void write(int b) throws IOException {
093    this.os.write(b);
094  }
095
096  @Override
097  public void write(byte[] b, int off, int len) throws IOException {
098    this.os.write(b, off, len);
099  }
100
101  @Override
102  public void flush() throws IOException {
103    this.os.flush();
104  }
105
106  @Override
107  public void close() throws IOException {
108    this.os.close();
109  }
110}