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;
024
025import org.apache.hadoop.hbase.io.util.StreamUtils;
026import org.apache.hadoop.hbase.util.ByteBufferUtils;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * When deal with OutputStream which is not ByteBufferWriter type, wrap it with this class. We will
031 * have to write offheap ByteBuffer (DBB) data into the OS. This class is having a temp byte array
032 * to which we can copy the DBB data for writing to the OS.
033 * <br>
034 * This is used while writing Cell data to WAL. In case of AsyncWAL, the OS created there is
035 * ByteBufferWriter. But in case of FSHLog, the OS passed by DFS client, is not of type
036 * ByteBufferWriter. We will need this temp solution until DFS client supports writing ByteBuffer
037 * directly to the OS it creates.
038 * <br>
039 * Note: This class is not thread safe.
040 */
041@InterfaceAudience.Private
042public class ByteBufferWriterOutputStream extends OutputStream
043    implements ByteBufferWriter {
044
045  private static final int DEFAULT_BUFFER_SIZE = 4096;
046
047  private final OutputStream os;
048  private final int bufSize;
049  private byte[] buf;
050
051  public ByteBufferWriterOutputStream(OutputStream os) {
052    this(os, DEFAULT_BUFFER_SIZE);
053  }
054
055  public ByteBufferWriterOutputStream(OutputStream os, int size) {
056    this.os = os;
057    this.bufSize = size;
058    this.buf = null;
059  }
060
061  /**
062   * Writes len bytes from the specified ByteBuffer starting at offset off to this OutputStream. If
063   * off is negative or larger than the ByteBuffer then an ArrayIndexOutOfBoundsException is thrown.
064   * If len is greater than the length of the ByteBuffer, then an ArrayIndexOutOfBoundsException is
065   * thrown. This method does not change the position of the ByteBuffer.
066   * @param b the ByteBuffer
067   * @param off the start offset in the data
068   * @param len the number of bytes to write
069   * @throws IOException if an I/O error occurs. In particular, an IOException is thrown if the
070   *           output stream is closed.
071   * @throws NullPointerException if {@code b} is {@code null}
072   */
073  @Override
074  public void write(ByteBuffer b, int off, int len) throws IOException {
075    Objects.requireNonNull(b);
076
077    // Lazily load in the event that this version of 'write' is not invoked
078    if (this.buf == null) {
079      this.buf = new byte[this.bufSize];
080    }
081    int totalCopied = 0;
082    while (totalCopied < len) {
083      int bytesToCopy = Math.min((len - totalCopied), this.bufSize);
084      ByteBufferUtils.copyFromBufferToArray(this.buf, b, off + totalCopied, 0,
085          bytesToCopy);
086      this.os.write(this.buf, 0, bytesToCopy);
087      totalCopied += bytesToCopy;
088    }
089  }
090
091  @Override
092  public void writeInt(int i) throws IOException {
093    StreamUtils.writeInt(this.os, i);
094  }
095
096  @Override
097  public void write(int b) throws IOException {
098    this.os.write(b);
099  }
100
101  @Override
102  public void write(byte[] b, int off, int len) throws IOException {
103    this.os.write(b, off, len);
104  }
105
106  @Override
107  public void flush() throws IOException {
108    this.os.flush();
109  }
110
111  @Override
112  public void close() throws IOException {
113    this.os.close();
114  }
115}