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