All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.gemini.engine.page.bmap.GByteArrayOutputStreamWithPos Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.state.gemini.engine.page.bmap;

import org.apache.flink.annotation.Internal;
import org.apache.flink.util.Preconditions;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Un-synchronized stream similar to Java's ByteArrayOutputStream that also exposes the current position.
 */
@Internal
public class GByteArrayOutputStreamWithPos extends OutputStream {

	protected byte[] buffer;
	protected int count;

	public GByteArrayOutputStreamWithPos() {
		this(64);
	}

	public GByteArrayOutputStreamWithPos(int size) {
		Preconditions.checkArgument(size >= 0);
		buffer = new byte[size];
	}

	private void ensureCapacity(int requiredCapacity) {
		if (requiredCapacity - buffer.length > 0) {
			increaseCapacity(requiredCapacity);
		}
	}

	private void increaseCapacity(int requiredCapacity) {
		int oldCapacity = buffer.length;
		int newCapacity = oldCapacity << 1;
		if (newCapacity - requiredCapacity < 0) {
			newCapacity = requiredCapacity;
		}
		if (newCapacity < 0) {
			if (requiredCapacity < 0) {
				throw new OutOfMemoryError();
			}
			newCapacity = Integer.MAX_VALUE;
		}
		buffer = Arrays.copyOf(buffer, newCapacity);
	}

	@Override
	public void write(int b) {
		ensureCapacity(count + 1);
		buffer[count] = (byte) b;
		++count;
	}

	@Override
	public void write(byte[] b, int off, int len) {
		if ((off < 0) || (len < 0) || (off > b.length) || ((off + len) - b.length > 0)) {
			throw new IndexOutOfBoundsException();
		}

		ensureCapacity(count + len);

		System.arraycopy(b, off, buffer, count, len);
		count += len;
	}

	public void reset() {
		count = 0;
	}

	public int size() {
		return count;
	}

	public int getPosition() {
		return count;
	}

	public void setPosition(int position) {
		Preconditions.checkArgument(position >= 0, "Position out of bounds.");
		ensureCapacity(position + 1);
		count = position;
	}

	@Override
	public void close() throws IOException {
	}

	public byte[] getBuf() {
		return buffer;
	}

	public void write(ByteBuffer bb, int off, int len) throws IOException {
		ensureCapacity(count + len);
		ByteBufferUtils.copyFromBufferToArray(bb, buffer, off, count, len);
		count += len;
	}

	public void writeInt(int lastKeyPosition) {
		ensureCapacity(count + 4);
		buffer[count] = (byte) ((lastKeyPosition >>> 24) & 0xFF);
		++count;
		buffer[count] = (byte) ((lastKeyPosition >>> 16) & 0xFF);
		++count;
		buffer[count] = (byte) ((lastKeyPosition >>> 8) & 0xFF);
		++count;
		buffer[count] = (byte) ((lastKeyPosition >>> 0) & 0xFF);
		++count;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy