Return-Path: X-Original-To: apmail-accumulo-dev-archive@www.apache.org Delivered-To: apmail-accumulo-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4953319A8E for ; Sat, 19 Mar 2016 19:15:00 +0000 (UTC) Received: (qmail 82270 invoked by uid 500); 19 Mar 2016 19:15:00 -0000 Delivered-To: apmail-accumulo-dev-archive@accumulo.apache.org Received: (qmail 82219 invoked by uid 500); 19 Mar 2016 19:15:00 -0000 Mailing-List: contact dev-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list dev@accumulo.apache.org Received: (qmail 82207 invoked by uid 99); 19 Mar 2016 19:14:59 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Mar 2016 19:14:59 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A074BDFB7D; Sat, 19 Mar 2016 19:14:59 +0000 (UTC) From: joshelser To: dev@accumulo.apache.org Reply-To: dev@accumulo.apache.org References: In-Reply-To: Subject: [GitHub] accumulo pull request: ACCUMULO-4164 Avoid copying rfile index whe... Content-Type: text/plain Message-Id: <20160319191459.A074BDFB7D@git1-us-west.apache.org> Date: Sat, 19 Mar 2016 19:14:59 +0000 (UTC) Github user joshelser commented on a diff in the pull request: https://github.com/apache/accumulo/pull/80#discussion_r56753672 --- Diff: core/src/main/java/org/apache/accumulo/core/file/blockfile/impl/SeekableByteArrayInputStream.java --- @@ -0,0 +1,132 @@ +/* + * 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.accumulo.core.file.blockfile.impl; + +import java.io.IOException; +import java.io.InputStream; + +/** + * This class is like byte array input stream with two differences. It supports seeking and avoids synchronization. + */ +public class SeekableByteArrayInputStream extends InputStream { + + // make this volatile to ensure data set by one thread can be seen by another + private volatile byte buffer[]; + private int cur; + private int max; + + @Override + public int read() { + if (cur < max) { + return buffer[cur++] & 0xff; + } else { + return -1; + } + } + + @Override + public int read(byte b[], int offset, int length) { + if (b == null) { + throw new NullPointerException(); + } + + if (length < 0 || offset < 0 || length > b.length - offset) { + throw new IndexOutOfBoundsException(); + } + + if (length == 0) { + return 0; + } + + int avail = max - cur; + + if (avail <= 0) { + return -1; + } + + if (length > avail) { + length = avail; + } + + System.arraycopy(buffer, cur, b, offset, length); + cur += length; + return length; + } + + @Override + public long skip(long requestedSkip) { + long actualSkip = max - cur; + if (requestedSkip < actualSkip) + if (requestedSkip < 0) + actualSkip = 0; + else + actualSkip = requestedSkip; + + cur += actualSkip; + return actualSkip; + } + + @Override + public int available() { + return max - cur; + } + + @Override + public boolean markSupported() { + return false; + } + + @Override + public void mark(int readAheadLimit) { + throw new UnsupportedOperationException(); + } + + @Override + public void reset() { + throw new UnsupportedOperationException(); + } + + @Override + public void close() throws IOException {} + + public SeekableByteArrayInputStream(byte[] buf) { + this.buffer = buf; + this.cur = 0; + this.max = buf.length; + } + + public SeekableByteArrayInputStream(byte[] buf, int maxOffset) { + this.buffer = buf; + this.cur = 0; + this.max = maxOffset; + } + + public void seek(int position) { + if (position < 0 || position >= max) + throw new IllegalArgumentException("position = " + position + " maxOffset = " + max); + this.cur = position; + } + + public int getPosition() { + return this.cur; + } + + public byte[] getBuffer() { --- End diff -- Javadoc to be explicit that this is the actual array (changes to it will be represented in the InputStream? Do we want this do be `public`? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---