hy00nc commented on a change in pull request #213: [NEMO-383] Implement DirectByteBufferOutputStream for Off-heap SerializedMemoryStore URL: https://github.com/apache/incubator-nemo/pull/213#discussion_r275231672
########## File path: common/src/main/java/org/apache/nemo/common/DirectByteBufferOutputStream.java ########## @@ -0,0 +1,180 @@ +/* + * 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.nemo.common; + +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.List; + +/** + * This class is a customized output stream implementation backed by + * {@link ByteBuffer}, which utilizes off heap memory when writing the data. + * Memory is allocated when needed by the specified {@code pageSize}. + */ +public final class DirectByteBufferOutputStream extends OutputStream { + + private LinkedList<ByteBuffer> dataList = new LinkedList<>(); + private static final int DEFAULT_PAGE_SIZE = 4096; + private int pageSize; + private ByteBuffer currentBuf; + + + /** + * Default constructor. + * Sets the {@code pageSize} as default size of 4096 bytes. + */ + public DirectByteBufferOutputStream() { + this(DEFAULT_PAGE_SIZE); + } + + /** + * Constructor specifying the {@code size}. + * Sets the {@code pageSize} as {@code size}. + * + * @param size should be a power of 2 and greater than or equal to 4096. + */ + public DirectByteBufferOutputStream(final int size) { + if (size < DEFAULT_PAGE_SIZE || (size & (size - 1)) != 0) { + throw new IllegalArgumentException("Invalid pageSize"); + } + pageSize = size; + } + + /** + * Allocates new {@link ByteBuffer} with the capacity equal to {@code pageSize}. + */ + private void newLastBuffer() { + dataList.addLast(ByteBuffer.allocateDirect(pageSize)); + } + + /** + * Writes the specified byte to this output stream. + * + * @param b the byte to be written. + */ + @Override + public void write(final int b) { + currentBuf = (dataList.isEmpty() ? null : dataList.getLast()); Review comment: Made changes :) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
