Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 97C68200C24 for ; Thu, 23 Feb 2017 15:40:34 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 9684C160B50; Thu, 23 Feb 2017 14:40:34 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D0E43160B64 for ; Thu, 23 Feb 2017 15:40:33 +0100 (CET) Received: (qmail 86849 invoked by uid 500); 23 Feb 2017 14:40:33 -0000 Mailing-List: contact issues-help@nifi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nifi.apache.org Delivered-To: mailing list issues@nifi.apache.org Received: (qmail 86782 invoked by uid 99); 23 Feb 2017 14:40:33 -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; Thu, 23 Feb 2017 14:40:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D14E3DFF08; Thu, 23 Feb 2017 14:40:32 +0000 (UTC) From: markap14 To: issues@nifi.apache.org Reply-To: issues@nifi.apache.org References: In-Reply-To: Subject: [GitHub] nifi pull request #1214: NIFI-2876 refactored demarcators into a common abst... Content-Type: text/plain Message-Id: <20170223144032.D14E3DFF08@git1-us-west.apache.org> Date: Thu, 23 Feb 2017 14:40:32 +0000 (UTC) archived-at: Thu, 23 Feb 2017 14:40:34 -0000 Github user markap14 commented on a diff in the pull request: https://github.com/apache/nifi/pull/1214#discussion_r102713215 --- Diff: nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/AbstractDemarcator.java --- @@ -0,0 +1,138 @@ +/* + * 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.nifi.stream.io.util; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.nifi.stream.io.exception.TokenTooLargeException; + +/** + * Base class for implementing streaming demarcators. + *

+ * NOTE: Not intended for multi-thread usage hence not Thread-safe. + *

+ */ +abstract class AbstractDemarcator implements Closeable { + + final static int INIT_BUFFER_SIZE = 8192; + + private final InputStream is; + + private final int initialBufferSize; + + private final int maxDataSize; + + byte[] buffer; + + int index; + + int mark; + + long offset; + + int bufferLength; + + /** + * Constructs an instance of demarcator with provided {@link InputStream} + * and max buffer size. Each demarcated token must fit within max buffer + * size, otherwise the exception will be raised. + */ + AbstractDemarcator(InputStream is, int maxDataSize) { + this(is, maxDataSize, INIT_BUFFER_SIZE); + } + + /** + * Constructs an instance of demarcator with provided {@link InputStream} + * and max buffer size and initial buffer size. Each demarcated token must + * fit within max buffer size, otherwise the exception will be raised. + */ + AbstractDemarcator(InputStream is, int maxDataSize, int initialBufferSize) { + this.validate(is, maxDataSize, initialBufferSize); + this.is = is; + this.initialBufferSize = initialBufferSize; + this.buffer = new byte[initialBufferSize]; + this.maxDataSize = maxDataSize; + } + + @Override + public void close() throws IOException { + // noop + } + + /** + * Will fill the current buffer from current 'index' position, expanding it + * and or shuffling it if necessary. If buffer exceeds max buffer size a + * {@link TokenTooLargeException} will be thrown. + * + * @throws IOException + * if unable to read from the stream + */ + void fill() throws IOException { + if (this.index >= this.buffer.length) { + if (this.mark == 0) { // expand + byte[] newBuff = new byte[this.buffer.length + this.initialBufferSize]; + System.arraycopy(this.buffer, 0, newBuff, 0, this.buffer.length); + this.buffer = newBuff; + } else { // shuffle + int length = this.index - this.mark; + System.arraycopy(this.buffer, this.mark, this.buffer, 0, length); + this.index = length; + this.mark = 0; + } + } + + int bytesRead; + do { + bytesRead = this.is.read(this.buffer, this.index, this.buffer.length - this.index); --- End diff -- I don't believe there's any need for a do/while block here. InputStream either returns -1 indicating that the stream is finished or blocks until data is available, so 0 is never a valid return value. --- 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. ---