You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jo...@apache.org on 2014/12/20 06:52:30 UTC
[8/8] incubator-nifi git commit: NIFI-189 Collapsed several commons
libs into one
NIFI-189 Collapsed several commons libs into one
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/1357a17b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/1357a17b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/1357a17b
Branch: refs/heads/develop
Commit: 1357a17b6752a78dd87c3de7b0323f22e52731f9
Parents: c0e4618
Author: joewitt <jo...@apache.org>
Authored: Sat Dec 20 00:40:14 2014 -0500
Committer: joewitt <jo...@apache.org>
Committed: Sat Dec 20 00:40:14 2014 -0500
----------------------------------------------------------------------
commons/core-flowfile-attributes/pom.xml | 29 -
.../flowfile/attributes/CoreAttributes.java | 72 ---
.../attributes/FlowFileAttributeKey.java | 21 -
commons/data-provenance-utils/pom.xml | 2 +-
commons/naive-search-ring-buffer/pom.xml | 30 -
.../apache/nifi/util/NaiveSearchRingBuffer.java | 135 ----
.../nifi/util/TestNaiveSearchRingBuffer.java | 72 ---
commons/nifi-file-utils/pom.xml | 35 --
.../java/org/apache/nifi/file/FileUtils.java | 612 ------------------
commons/nifi-stream-utils/.gitignore | 1 -
commons/nifi-stream-utils/pom.xml | 30 -
.../org/apache/nifi/io/BufferedInputStream.java | 37 --
.../apache/nifi/io/BufferedOutputStream.java | 140 -----
.../apache/nifi/io/ByteArrayInputStream.java | 250 --------
.../apache/nifi/io/ByteArrayOutputStream.java | 250 --------
.../apache/nifi/io/ByteCountingInputStream.java | 104 ----
.../nifi/io/ByteCountingOutputStream.java | 63 --
.../org/apache/nifi/io/DataOutputStream.java | 417 -------------
.../org/apache/nifi/io/GZIPOutputStream.java | 41 --
.../nifi/io/LeakyBucketStreamThrottler.java | 324 ----------
.../apache/nifi/io/NonCloseableInputStream.java | 56 --
.../nifi/io/NonCloseableOutputStream.java | 51 --
.../org/apache/nifi/io/NullOutputStream.java | 46 --
.../org/apache/nifi/io/StreamThrottler.java | 33 -
.../java/org/apache/nifi/io/StreamUtils.java | 257 --------
.../org/apache/nifi/io/ZipOutputStream.java | 38 --
.../exception/BytePatternNotFoundException.java | 28 -
.../io/util/NonThreadSafeCircularBuffer.java | 69 --
.../nifi/io/TestLeakyBucketThrottler.java | 144 -----
.../src/test/resources/logback-test.xml | 48 --
commons/nifi-utils/pom.xml | 4 +
.../flowfile/attributes/CoreAttributes.java | 72 +++
.../attributes/FlowFileAttributeKey.java | 21 +
.../apache/nifi/io/CompoundUpdateMonitor.java | 115 ----
.../org/apache/nifi/io/LastModifiedMonitor.java | 30 -
.../java/org/apache/nifi/io/MD5SumMonitor.java | 49 --
.../apache/nifi/io/SynchronousFileWatcher.java | 123 ----
.../java/org/apache/nifi/io/UpdateMonitor.java | 25 -
.../nifi/remote/StandardVersionNegotiator.java | 81 +++
.../apache/nifi/remote/VersionNegotiator.java | 65 ++
.../TransmissionDisabledException.java | 25 +
.../nifi/remote/io/CompressionInputStream.java | 184 ++++++
.../nifi/remote/io/CompressionOutputStream.java | 147 +++++
.../remote/io/InterruptableInputStream.java | 117 ++++
.../remote/io/InterruptableOutputStream.java | 81 +++
.../remote/io/socket/BufferStateManager.java | 111 ++++
.../io/socket/SocketChannelInputStream.java | 157 +++++
.../io/socket/SocketChannelOutputStream.java | 113 ++++
.../remote/io/socket/ssl/SSLSocketChannel.java | 602 ++++++++++++++++++
.../socket/ssl/SSLSocketChannelInputStream.java | 62 ++
.../ssl/SSLSocketChannelOutputStream.java | 53 ++
.../nifi/stream/io/BufferedInputStream.java | 37 ++
.../nifi/stream/io/BufferedOutputStream.java | 140 +++++
.../nifi/stream/io/ByteArrayInputStream.java | 250 ++++++++
.../nifi/stream/io/ByteArrayOutputStream.java | 250 ++++++++
.../nifi/stream/io/ByteCountingInputStream.java | 104 ++++
.../stream/io/ByteCountingOutputStream.java | 63 ++
.../apache/nifi/stream/io/DataOutputStream.java | 417 +++++++++++++
.../apache/nifi/stream/io/GZIPOutputStream.java | 41 ++
.../stream/io/LeakyBucketStreamThrottler.java | 324 ++++++++++
.../nifi/stream/io/NonCloseableInputStream.java | 56 ++
.../stream/io/NonCloseableOutputStream.java | 51 ++
.../apache/nifi/stream/io/NullOutputStream.java | 46 ++
.../apache/nifi/stream/io/StreamThrottler.java | 33 +
.../org/apache/nifi/stream/io/StreamUtils.java | 257 ++++++++
.../apache/nifi/stream/io/ZipOutputStream.java | 38 ++
.../exception/BytePatternNotFoundException.java | 28 +
.../io/util/NonThreadSafeCircularBuffer.java | 69 ++
.../apache/nifi/util/NaiveSearchRingBuffer.java | 135 ++++
.../org/apache/nifi/util/file/FileUtils.java | 623 +++++++++++++++++++
.../file/monitor/CompoundUpdateMonitor.java | 115 ++++
.../util/file/monitor/LastModifiedMonitor.java | 30 +
.../nifi/util/file/monitor/MD5SumMonitor.java | 51 ++
.../file/monitor/SynchronousFileWatcher.java | 123 ++++
.../nifi/util/file/monitor/UpdateMonitor.java | 25 +
.../org/apache/nifi/util/search/Search.java | 57 ++
.../org/apache/nifi/util/search/SearchTerm.java | 141 +++++
.../util/search/ahocorasick/AhoCorasick.java | 155 +++++
.../nifi/util/search/ahocorasick/Node.java | 72 +++
.../util/search/ahocorasick/SearchState.java | 63 ++
.../io/TestCompressionInputOutputStreams.java | 153 +++++
.../stream/io/TestLeakyBucketThrottler.java | 147 +++++
.../nifi/util/TestNaiveSearchRingBuffer.java | 72 +++
.../file/monitor/TestCompoundUpdateMonitor.java | 71 +++
.../monitor/TestSynchronousFileWatcher.java | 61 ++
.../timebuffer/TestCompoundUpdateMonitor.java | 75 ---
.../timebuffer/TestSynchronousFileWatcher.java | 64 --
.../src/test/resources/logback-test.xml | 32 +
commons/pom.xml | 6 -
commons/remote-communications-utils/pom.xml | 29 -
.../nifi/remote/StandardVersionNegotiator.java | 81 ---
.../apache/nifi/remote/VersionNegotiator.java | 65 --
.../TransmissionDisabledException.java | 25 -
.../nifi/remote/io/CompressionInputStream.java | 184 ------
.../nifi/remote/io/CompressionOutputStream.java | 147 -----
.../remote/io/InterruptableInputStream.java | 117 ----
.../remote/io/InterruptableOutputStream.java | 81 ---
.../remote/io/socket/BufferStateManager.java | 111 ----
.../io/socket/SocketChannelInputStream.java | 157 -----
.../io/socket/SocketChannelOutputStream.java | 113 ----
.../remote/io/socket/ssl/SSLSocketChannel.java | 602 ------------------
.../socket/ssl/SSLSocketChannelInputStream.java | 62 --
.../ssl/SSLSocketChannelOutputStream.java | 53 --
.../io/TestCompressionInputOutputStreams.java | 153 -----
commons/search-utils/pom.xml | 30 -
.../org/apache/nifi/util/search/Search.java | 57 --
.../org/apache/nifi/util/search/SearchTerm.java | 141 -----
.../util/search/ahocorasick/AhoCorasick.java | 155 -----
.../nifi/util/search/ahocorasick/Node.java | 72 ---
.../util/search/ahocorasick/SearchState.java | 63 --
commons/wali/pom.xml | 2 +-
.../org/wali/MinimalLockingWriteAheadLog.java | 4 +-
.../framework-bundle/framework/cluster/pom.xml | 2 +-
.../impl/FileBasedClusterNodeFirewall.java | 2 +-
.../nifi/cluster/flow/impl/DataFlowDaoImpl.java | 10 +-
.../impl/FileBasedClusterNodeFirewallTest.java | 3 +-
.../framework-bundle/framework/core-api/pom.xml | 6 +-
.../framework-bundle/framework/core/pom.xml | 8 -
.../nifi/controller/FileSystemSwapManager.java | 2 +-
.../apache/nifi/controller/FlowController.java | 2 +-
.../nifi/controller/FlowUnmarshaller.java | 2 +-
.../apache/nifi/controller/SnippetManager.java | 8 +-
.../nifi/controller/StandardFlowService.java | 2 +-
.../controller/StandardFlowSynchronizer.java | 2 +-
.../apache/nifi/controller/TemplateManager.java | 8 +-
.../repository/FileSystemRepository.java | 4 +-
.../repository/StandardProcessSession.java | 6 +-
.../repository/VolatileContentRepository.java | 4 +-
.../service/ControllerServiceLoader.java | 2 +-
.../StandardXMLFlowConfigurationDAO.java | 2 +-
.../nifi/remote/StandardRemoteProcessGroup.java | 2 +-
.../TestWriteAheadFlowFileRepository.java | 2 +-
.../repository/io/TestLimitedOutputStream.java | 3 +-
.../file-authorization-provider/pom.xml | 2 +-
.../FileAuthorizationProvider.java | 2 +-
.../FileAuthorizationProviderTest.java | 2 +-
.../framework/site-to-site/pom.xml | 12 -
.../remote/codec/StandardFlowFileCodec.java | 2 +-
.../remote/io/socket/SocketChannelInput.java | 4 +-
.../remote/io/socket/SocketChannelOutput.java | 4 +-
.../io/socket/ssl/SSLSocketChannelInput.java | 4 +-
.../io/socket/ssl/SSLSocketChannelOutput.java | 4 +-
.../apache/nifi/web/api/ProvenanceResource.java | 2 +-
.../nifi/web/util/ClientResponseUtils.java | 2 +-
.../hadoop-bundle/hdfs-processors/pom.xml | 6 +-
...lowFileStreamUnpackerSequenceFileWriter.java | 2 +-
.../apache/nifi/processors/hadoop/PutHDFS.java | 4 +-
.../hadoop/SequenceFileWriterImpl.java | 2 +-
.../hadoop/TarUnpackerSequenceFileWriter.java | 2 +-
.../hadoop/ZipUnpackerSequenceFileWriter.java | 2 +-
.../hadoop/util/OutputStreamWritable.java | 4 +-
.../monitor-threshold-bundle/processor/pom.xml | 8 -
.../processors/monitor/MonitorThreshold.java | 2 +-
nar-bundles/monitor-threshold-bundle/ui/pom.xml | 2 +-
.../persistent-provenance-repository/pom.xml | 8 -
.../nifi/provenance/StandardRecordReader.java | 4 +-
.../nifi/provenance/StandardRecordWriter.java | 6 +-
.../provenance/rollover/CompressionAction.java | 4 +-
.../provenance/serialization/RecordReaders.java | 2 +-
.../standard-bundle/jms-processors/pom.xml | 2 +-
.../apache/nifi/processors/jms/JmsConsumer.java | 2 +-
.../org/apache/nifi/processors/jms/PutJMS.java | 2 +-
.../nifi/processors/jms/util/JmsFactory.java | 2 +-
.../standard-bundle/standard-processors/pom.xml | 14 +-
.../processors/standard/CompressContent.java | 6 +-
.../processors/standard/EncryptContent.java | 2 +-
.../standard/EvaluateRegularExpression.java | 2 +-
.../nifi/processors/standard/EvaluateXPath.java | 4 +-
.../processors/standard/EvaluateXQuery.java | 4 +-
.../standard/ExecuteStreamCommand.java | 6 +-
.../nifi/processors/standard/HashContent.java | 4 +-
.../nifi/processors/standard/ListenHTTP.java | 4 +-
.../nifi/processors/standard/MergeContent.java | 8 +-
.../nifi/processors/standard/ModifyBytes.java | 2 +-
.../nifi/processors/standard/PostHTTP.java | 12 +-
.../processors/standard/PutFileTransfer.java | 2 +-
.../nifi/processors/standard/ReplaceText.java | 2 +-
.../standard/ReplaceTextWithMapping.java | 2 +-
.../processors/standard/RouteOnContent.java | 2 +-
.../nifi/processors/standard/ScanAttribute.java | 4 +-
.../nifi/processors/standard/ScanContent.java | 6 +-
.../nifi/processors/standard/SplitContent.java | 2 +-
.../nifi/processors/standard/SplitText.java | 8 +-
.../nifi/processors/standard/SplitXml.java | 2 +-
.../nifi/processors/standard/TransformXml.java | 2 +-
.../nifi/processors/standard/UnpackContent.java | 6 +-
.../standard/servlets/ListenHTTPServlet.java | 4 +-
.../standard/util/UDPStreamConsumer.java | 2 +-
.../processors/standard/TestMergeContent.java | 3 +-
.../processors/standard/TestScanContent.java | 4 +-
.../distributed-cache-client-service/pom.xml | 6 +-
.../DistributedMapCacheClientService.java | 4 +-
.../DistributedSetCacheClientService.java | 4 +-
.../cache/client/SSLCommsSession.java | 4 +-
.../cache/client/StandardCommsSession.java | 4 +-
.../distributed-cache-protocol/pom.xml | 2 +-
.../distributed-cache-server/pom.xml | 6 +-
.../cache/server/AbstractCacheServer.java | 4 +-
.../cache/server/SetCacheServer.java | 2 +-
.../cache/server/map/MapCacheServer.java | 2 +-
.../update-attribute-bundle/processor/pom.xml | 2 +-
nifi-mock/pom.xml | 4 -
pom.xml | 36 +-
203 files changed, 6371 insertions(+), 6667 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/core-flowfile-attributes/pom.xml
----------------------------------------------------------------------
diff --git a/commons/core-flowfile-attributes/pom.xml b/commons/core-flowfile-attributes/pom.xml
deleted file mode 100644
index 5a82ffa..0000000
--- a/commons/core-flowfile-attributes/pom.xml
+++ /dev/null
@@ -1,29 +0,0 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <!--
- 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.
- -->
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.nifi</groupId>
- <artifactId>nifi-commons-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
-
- <artifactId>nifi-core-flowfile-attributes</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>NiFi: Core FlowFile Attributes</name>
-</project>
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/CoreAttributes.java
----------------------------------------------------------------------
diff --git a/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/CoreAttributes.java b/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/CoreAttributes.java
deleted file mode 100644
index 24f43ca..0000000
--- a/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/CoreAttributes.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.flowfile.attributes;
-
-public enum CoreAttributes implements FlowFileAttributeKey {
- /**
- * The flowfile's path indicates the relative directory to which a FlowFile belongs and does not
- * contain the filename
- */
- PATH("path"),
-
- /**
- * The flowfile's absolute path indicates the absolute directory to which a FlowFile belongs and does not
- * contain the filename
- */
- ABSOLUTE_PATH("absolute.path"),
-
- /**
- * The filename of the FlowFile. The filename should not contain any directory structure.
- */
- FILENAME("filename"),
-
- /**
- * A unique UUID assigned to this FlowFile
- */
- UUID("uuid"),
-
- /**
- * A numeric value indicating the FlowFile priority
- */
- PRIORITY("priority"),
-
- /**
- * The MIME Type of this FlowFile
- */
- MIME_TYPE("mime.type"),
-
- /**
- * Specifies the reason that a FlowFile is being discarded
- */
- DISCARD_REASON("discard.reason"),
-
- /**
- * Indicates an identifier other than the FlowFile's UUID that is known to refer to this FlowFile.
- */
- ALTERNATE_IDENTIFIER("alternate.identifier");
-
- private final String key;
- private CoreAttributes(final String key) {
- this.key = key;
- }
-
- @Override
- public String key() {
- return key;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/FlowFileAttributeKey.java
----------------------------------------------------------------------
diff --git a/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/FlowFileAttributeKey.java b/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/FlowFileAttributeKey.java
deleted file mode 100644
index cc6c28e..0000000
--- a/commons/core-flowfile-attributes/src/main/java/org/apache/nifi/flowfile/attributes/FlowFileAttributeKey.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * 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.flowfile.attributes;
-
-public interface FlowFileAttributeKey {
- String key();
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/data-provenance-utils/pom.xml
----------------------------------------------------------------------
diff --git a/commons/data-provenance-utils/pom.xml b/commons/data-provenance-utils/pom.xml
index 162d4f3..0024b70 100644
--- a/commons/data-provenance-utils/pom.xml
+++ b/commons/data-provenance-utils/pom.xml
@@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
- <artifactId>nifi-core-flowfile-attributes</artifactId>
+ <artifactId>nifi-utils</artifactId>
</dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/naive-search-ring-buffer/pom.xml
----------------------------------------------------------------------
diff --git a/commons/naive-search-ring-buffer/pom.xml b/commons/naive-search-ring-buffer/pom.xml
deleted file mode 100644
index c9628f0..0000000
--- a/commons/naive-search-ring-buffer/pom.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <!--
- 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.
- -->
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.nifi</groupId>
- <artifactId>nifi-commons-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
-
- <artifactId>naive-search-ring-buffer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>NiFi Ring Buffer</name>
-
-</project>
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/naive-search-ring-buffer/src/main/java/org/apache/nifi/util/NaiveSearchRingBuffer.java
----------------------------------------------------------------------
diff --git a/commons/naive-search-ring-buffer/src/main/java/org/apache/nifi/util/NaiveSearchRingBuffer.java b/commons/naive-search-ring-buffer/src/main/java/org/apache/nifi/util/NaiveSearchRingBuffer.java
deleted file mode 100644
index 85bfd96..0000000
--- a/commons/naive-search-ring-buffer/src/main/java/org/apache/nifi/util/NaiveSearchRingBuffer.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * 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.util;
-
-import java.util.Arrays;
-
-/**
- * <p>
- * A RingBuffer that can be used to scan byte sequences for subsequences.
- * </p>
- *
- * <p>
- * This class implements an efficient naive search algorithm, which allows the
- * user of the library to identify byte sequences in a stream on-the-fly so that
- * the stream can be segmented without having to buffer the data.
- * </p>
- *
- * <p>
- * The intended usage paradigm is:
- * <code>
- * <pre>
- * final byte[] searchSequence = ...;
- * final CircularBuffer buffer = new CircularBuffer(searchSequence);
- * while ((int nextByte = in.read()) > 0) {
- * if ( buffer.addAndCompare(nextByte) ) {
- * // This byte is the last byte in the given sequence
- * } else {
- * // This byte does not complete the given sequence
- * }
- * }
- * </pre>
- * </code>
- * </p>
- */
-public class NaiveSearchRingBuffer {
-
- private final byte[] lookingFor;
- private final int[] buffer;
- private int insertionPointer = 0;
- private int bufferSize = 0;
-
- public NaiveSearchRingBuffer(final byte[] lookingFor) {
- this.lookingFor = lookingFor;
- this.buffer = new int[lookingFor.length];
- Arrays.fill(buffer, -1);
- }
-
- /**
- * Returns the contents of the internal buffer, which represents the last X
- * bytes added to the buffer, where X is the minimum of the number of bytes
- * added to the buffer or the length of the byte sequence for which we are
- * looking
- *
- * @return
- */
- public byte[] getBufferContents() {
- final int contentLength = Math.min(lookingFor.length, bufferSize);
- final byte[] contents = new byte[contentLength];
- for (int i = 0; i < contentLength; i++) {
- final byte nextByte = (byte) buffer[(insertionPointer + i) % lookingFor.length];
- contents[i] = nextByte;
- }
- return contents;
- }
-
- /**
- * Returns the oldest byte in the buffer
- *
- * @return
- */
- public int getOldestByte() {
- return buffer[insertionPointer];
- }
-
- /**
- * Returns <code>true</code> if the number of bytes that have been added to
- * the buffer is at least equal to the length of the byte sequence for which
- * we are searching
- *
- * @return
- */
- public boolean isFilled() {
- return bufferSize >= buffer.length;
- }
-
- /**
- * Clears the internal buffer so that a new search may begin
- */
- public void clear() {
- Arrays.fill(buffer, -1);
- insertionPointer = 0;
- bufferSize = 0;
- }
-
- /**
- * Add the given byte to the buffer and notify whether or not the byte
- * completes the desired byte sequence.
- *
- * @param data
- * @return <code>true</code> if this byte completes the byte sequence,
- * <code>false</code> otherwise.
- */
- public boolean addAndCompare(final byte data) {
- buffer[insertionPointer] = data;
- insertionPointer = (insertionPointer + 1) % lookingFor.length;
-
- bufferSize++;
- if (bufferSize < lookingFor.length) {
- return false;
- }
-
- for (int i = 0; i < lookingFor.length; i++) {
- final byte compare = (byte) buffer[(insertionPointer + i) % lookingFor.length];
- if (compare != lookingFor[i]) {
- return false;
- }
- }
-
- return true;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/naive-search-ring-buffer/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java
----------------------------------------------------------------------
diff --git a/commons/naive-search-ring-buffer/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java b/commons/naive-search-ring-buffer/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java
deleted file mode 100644
index 0838e96..0000000
--- a/commons/naive-search-ring-buffer/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.util;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-
-public class TestNaiveSearchRingBuffer {
-
- @Test
- public void testAddAndCompare() {
- final byte[] pattern = new byte[]{
- '\r', '0', 38, 48
- };
-
- final byte[] search = new byte[]{
- '\r', '0', 38, 58, 58, 83, 78, '\r', '0', 38, 48, 83, 92, 78, 4, 38
- };
-
- final NaiveSearchRingBuffer circ = new NaiveSearchRingBuffer(pattern);
- int counter = -1;
- for (final byte b : search) {
- counter++;
- final boolean matched = circ.addAndCompare(b);
- if (counter == 10) {
- assertTrue(matched);
- } else {
- assertFalse(matched);
- }
- }
- }
-
- @Test
- public void testGetOldestByte() {
- final byte[] pattern = new byte[]{
- '\r', '0', 38, 48
- };
-
- final byte[] search = new byte[]{
- '\r', '0', 38, 58, 58, 83, 78, (byte) 223, (byte) 227, (byte) 250, '\r', '0', 38, 48, 83, 92, 78, 4, 38
- };
-
- final NaiveSearchRingBuffer circ = new NaiveSearchRingBuffer(pattern);
- int counter = -1;
- for (final byte b : search) {
- counter++;
- final boolean matched = circ.addAndCompare(b);
- if (counter == 13) {
- assertTrue(matched);
- } else {
- assertFalse(matched);
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-file-utils/pom.xml
----------------------------------------------------------------------
diff --git a/commons/nifi-file-utils/pom.xml b/commons/nifi-file-utils/pom.xml
deleted file mode 100644
index 2af4d63..0000000
--- a/commons/nifi-file-utils/pom.xml
+++ /dev/null
@@ -1,35 +0,0 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <!--
- 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.
- -->
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.nifi</groupId>
- <artifactId>nifi-commons-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
-
- <artifactId>nifi-file-utils</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>NiFi File Utils</name>
-
- <dependencies>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- </dependency>
- </dependencies>
-</project>
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-file-utils/src/main/java/org/apache/nifi/file/FileUtils.java
----------------------------------------------------------------------
diff --git a/commons/nifi-file-utils/src/main/java/org/apache/nifi/file/FileUtils.java b/commons/nifi-file-utils/src/main/java/org/apache/nifi/file/FileUtils.java
deleted file mode 100644
index 8920493..0000000
--- a/commons/nifi-file-utils/src/main/java/org/apache/nifi/file/FileUtils.java
+++ /dev/null
@@ -1,612 +0,0 @@
-/*
- * 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.file;
-
-import java.io.BufferedInputStream;
-import java.io.Closeable;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Random;
-import org.apache.commons.codec.digest.DigestUtils;
-
-import org.slf4j.Logger;
-
-/**
- * A utility class containing a few useful static methods to do typical IO
- * operations.
- *
- * @author unattributed
- */
-public class FileUtils {
-
- public static final long TRANSFER_CHUNK_SIZE_BYTES = 1024 * 1024 * 8; //8 MB chunks
- public static final long MILLIS_BETWEEN_ATTEMPTS = 50L;
-
- /**
- * Closes the given closeable quietly - no logging, no exceptions...
- *
- * @param closeable
- */
- public static void closeQuietly(final Closeable closeable) {
- if (null != closeable) {
- try {
- closeable.close();
- } catch (final IOException io) {/*IGNORE*/
-
- }
- }
- }
-
- /**
- * Releases the given lock quietly - no logging, no exception
- *
- * @param lock
- */
- public static void releaseQuietly(final FileLock lock) {
- if (null != lock) {
- try {
- lock.release();
- } catch (final IOException io) {
- /*IGNORE*/
- }
- }
- }
-
- public static void ensureDirectoryExistAndCanAccess(final File dir) throws IOException {
- if (dir.exists() && !dir.isDirectory()) {
- throw new IOException(dir.getAbsolutePath() + " is not a directory");
- } else if (!dir.exists()) {
- final boolean made = dir.mkdirs();
- if (!made) {
- throw new IOException(dir.getAbsolutePath() + " could not be created");
- }
- }
- if (!(dir.canRead() && dir.canWrite())) {
- throw new IOException(dir.getAbsolutePath() + " directory does not have read/write privilege");
- }
- }
-
- /**
- * Deletes the given file. If the given file exists but could not be deleted
- * this will be printed as a warning to the given logger
- *
- * @param file
- * @param logger
- * @return
- */
- public static boolean deleteFile(final File file, final Logger logger) {
- return FileUtils.deleteFile(file, logger, 1);
- }
-
- /**
- * Deletes the given file. If the given file exists but could not be deleted
- * this will be printed as a warning to the given logger
- *
- * @param file
- * @param logger
- * @param attempts indicates how many times an attempt to delete should be
- * made
- * @return true if given file no longer exists
- */
- public static boolean deleteFile(final File file, final Logger logger, final int attempts) {
- if(file == null){
- return false;
- }
- boolean isGone = false;
- try {
- if (file.exists()) {
- final int effectiveAttempts = Math.max(1, attempts);
- for (int i = 0; i < effectiveAttempts && !isGone; i++) {
- isGone = file.delete() || !file.exists();
- if (!isGone && (effectiveAttempts - i) > 1) {
- FileUtils.sleepQuietly(MILLIS_BETWEEN_ATTEMPTS);
- }
- }
- if (!isGone && logger != null) {
- logger.warn("File appears to exist but unable to delete file: " + file.getAbsolutePath());
- }
- }
- } catch (final Throwable t) {
- if (logger != null) {
- logger.warn("Unable to delete file: '" + file.getAbsolutePath() + "' due to " + t);
- }
- }
- return isGone;
- }
-
- /**
- * Deletes all of the given files. If any exist and cannot be deleted that
- * will be printed at warn to the given logger.
- *
- * @param files can be null
- * @param logger can be null
- */
- public static void deleteFile(final List<File> files, final Logger logger) {
- FileUtils.deleteFile(files, logger, 1);
- }
-
- /**
- * Deletes all of the given files. If any exist and cannot be deleted that
- * will be printed at warn to the given logger.
- *
- * @param files can be null
- * @param logger can be null
- * @param attempts indicates how many times an attempt should be made to
- * delete each file
- */
- public static void deleteFile(final List<File> files, final Logger logger, final int attempts) {
- if (null == files || files.isEmpty()) {
- return;
- }
- final int effectiveAttempts = Math.max(1, attempts);
- for (final File file : files) {
- try {
- boolean isGone = false;
- for (int i = 0; i < effectiveAttempts && !isGone; i++) {
- isGone = file.delete() || !file.exists();
- if (!isGone && (effectiveAttempts - i) > 1) {
- FileUtils.sleepQuietly(MILLIS_BETWEEN_ATTEMPTS);
- }
- }
- if (!isGone && logger != null) {
- logger.warn("File appears to exist but unable to delete file: " + file.getAbsolutePath());
- }
- } catch (final Throwable t) {
- if (null != logger) {
- logger.warn("Unable to delete file given from path: '" + file.getPath() + "' due to " + t);
- }
- }
- }
- }
-
- /**
- * Deletes all files (not directories..) in the given directory (non
- * recursive) that match the given filename filter. If any file cannot be
- * deleted then this is printed at warn to the given logger.
- *
- * @param directory
- * @param filter if null then no filter is used
- * @param logger
- */
- public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger) {
- FileUtils.deleteFilesInDir(directory, filter, logger, false);
- }
-
- /**
- * Deletes all files (not directories) in the given directory (recursive)
- * that match the given filename filter. If any file cannot be deleted then
- * this is printed at warn to the given logger.
- *
- * @param directory
- * @param filter if null then no filter is used
- * @param logger
- * @param recurse
- */
- public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse) {
- FileUtils.deleteFilesInDir(directory, filter, logger, recurse, false);
- }
-
- /**
- * Deletes all files (not directories) in the given directory (recursive)
- * that match the given filename filter. If any file cannot be deleted then
- * this is printed at warn to the given logger.
- *
- * @param directory
- * @param filter if null then no filter is used
- * @param logger
- * @param recurse
- * @param deleteEmptyDirectories default is false; if true will delete
- * directories found that are empty
- */
- public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) {
- // ensure the specified directory is actually a directory and that it exists
- if (null != directory && directory.isDirectory()) {
- final File ingestFiles[] = directory.listFiles();
- for (File ingestFile : ingestFiles) {
- boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
- if (ingestFile.isFile() && process) {
- FileUtils.deleteFile(ingestFile, logger, 3);
- }
- if (ingestFile.isDirectory() && recurse) {
- FileUtils.deleteFilesInDir(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
- if (deleteEmptyDirectories && ingestFile.list().length == 0) {
- FileUtils.deleteFile(ingestFile, logger, 3);
- }
- }
- }
- }
- }
-
- /**
- * Deletes given files.
- *
- * @param files
- * @param recurse will recurse
- * @throws IOException
- */
- public static void deleteFiles(final Collection<File> files, final boolean recurse) throws IOException {
- for (final File file : files) {
- FileUtils.deleteFile(file, recurse);
- }
- }
-
- public static void deleteFile(final File file, final boolean recurse) throws IOException {
- if (file.isDirectory() && recurse) {
- FileUtils.deleteFiles(Arrays.asList(file.listFiles()), recurse);
- }
- //now delete the file itself regardless of whether it is plain file or a directory
- if (!FileUtils.deleteFile(file, null, 5)) {
- throw new IOException("Unable to delete " + file.getAbsolutePath());
- }
- }
-
- /**
- * Randomly generates a sequence of bytes and overwrites the contents of the
- * file a number of times. The file is then deleted.
- *
- * @param file File to be overwritten a number of times and, ultimately,
- * deleted
- * @param passes Number of times file should be overwritten
- * @throws IOException if something makes shredding or deleting a problem
- */
- public static void shredFile(final File file, final int passes)
- throws IOException {
- final Random generator = new Random();
- final long fileLength = file.length();
- final int byteArraySize = (int) Math.min(fileLength, 1048576); // 1MB
- final byte[] b = new byte[byteArraySize];
- final long numOfRandomWrites = (fileLength / b.length) + 1;
- final FileOutputStream fos = new FileOutputStream(file);
- try {
- // Over write file contents (passes) times
- final FileChannel channel = fos.getChannel();
- for (int i = 0; i < passes; i++) {
- generator.nextBytes(b);
- for (int j = 0; j <= numOfRandomWrites; j++) {
- fos.write(b);
- }
- fos.flush();
- channel.position(0);
- }
- // Write out "0" for each byte in the file
- Arrays.fill(b, (byte) 0);
- for (int j = 0; j < numOfRandomWrites; j++) {
- fos.write(b);
- }
- fos.flush();
- fos.close();
- // Try to delete the file a few times
- if (!FileUtils.deleteFile(file, null, 5)) {
- throw new IOException("Failed to delete file after shredding");
- }
-
- } finally {
- FileUtils.closeQuietly(fos);
- }
- }
-
- public static long copy(final InputStream in, final OutputStream out) throws IOException {
- final byte[] buffer = new byte[65536];
- long copied = 0L;
- int len;
- while ((len = in.read(buffer)) > 0) {
- out.write(buffer, 0, len);
- copied += len;
- }
-
- return copied;
- }
-
- public static long copyBytes(final byte[] bytes, final File destination, final boolean lockOutputFile) throws FileNotFoundException, IOException {
- FileOutputStream fos = null;
- FileLock outLock = null;
- long fileSize = 0L;
- try {
- fos = new FileOutputStream(destination);
- final FileChannel out = fos.getChannel();
- if (lockOutputFile) {
- outLock = out.tryLock(0, Long.MAX_VALUE, false);
- if (null == outLock) {
- throw new IOException("Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
- }
- }
- fos.write(bytes);
- fos.flush();
- fileSize = bytes.length;
- } finally {
- FileUtils.releaseQuietly(outLock);
- FileUtils.closeQuietly(fos);
- }
- return fileSize;
- }
-
- /**
- * Copies the given source file to the given destination file. The given
- * destination will be overwritten if it already exists.
- *
- * @param source
- * @param destination
- * @param lockInputFile if true will lock input file during copy; if false
- * will not
- * @param lockOutputFile if true will lock output file during copy; if false
- * will not
- * @param move if true will perform what is effectively a move operation
- * rather than a pure copy. This allows for potentially highly efficient
- * movement of the file but if not possible this will revert to a copy then
- * delete behavior. If false, then the file is copied and the source file is
- * retained. If a true rename/move occurs then no lock is held during that
- * time.
- * @param logger if failures occur, they will be logged to this logger if
- * possible. If this logger is null, an IOException will instead be thrown,
- * indicating the problem.
- * @return long number of bytes copied
- * @throws FileNotFoundException if the source file could not be found
- * @throws IOException
- * @throws SecurityException if a security manager denies the needed file
- * operations
- */
- public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final boolean move, final Logger logger) throws FileNotFoundException, IOException {
-
- FileInputStream fis = null;
- FileOutputStream fos = null;
- FileLock inLock = null;
- FileLock outLock = null;
- long fileSize = 0L;
- if (!source.canRead()) {
- throw new IOException("Must at least have read permission");
-
- }
- if (move && source.renameTo(destination)) {
- fileSize = destination.length();
- } else {
- try {
- fis = new FileInputStream(source);
- fos = new FileOutputStream(destination);
- final FileChannel in = fis.getChannel();
- final FileChannel out = fos.getChannel();
- if (lockInputFile) {
- inLock = in.tryLock(0, Long.MAX_VALUE, true);
- if (null == inLock) {
- throw new IOException("Unable to obtain shared file lock for: " + source.getAbsolutePath());
- }
- }
- if (lockOutputFile) {
- outLock = out.tryLock(0, Long.MAX_VALUE, false);
- if (null == outLock) {
- throw new IOException("Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
- }
- }
- long bytesWritten = 0;
- do {
- bytesWritten += out.transferFrom(in, bytesWritten, TRANSFER_CHUNK_SIZE_BYTES);
- fileSize = in.size();
- } while (bytesWritten < fileSize);
- out.force(false);
- FileUtils.closeQuietly(fos);
- FileUtils.closeQuietly(fis);
- fos = null;
- fis = null;
- if (move && !FileUtils.deleteFile(source, null, 5)) {
- if (logger == null) {
- FileUtils.deleteFile(destination, null, 5);
- throw new IOException("Could not remove file " + source.getAbsolutePath());
- } else {
- logger.warn("Configured to delete source file when renaming/move not successful. However, unable to delete file at: " + source.getAbsolutePath());
- }
- }
- } finally {
- FileUtils.releaseQuietly(inLock);
- FileUtils.releaseQuietly(outLock);
- FileUtils.closeQuietly(fos);
- FileUtils.closeQuietly(fis);
- }
- }
- return fileSize;
- }
-
- /**
- * Copies the given source file to the given destination file. The given
- * destination will be overwritten if it already exists.
- *
- * @param source
- * @param destination
- * @param lockInputFile if true will lock input file during copy; if false
- * will not
- * @param lockOutputFile if true will lock output file during copy; if false
- * will not
- * @param logger
- * @return long number of bytes copied
- * @throws FileNotFoundException if the source file could not be found
- * @throws IOException
- * @throws SecurityException if a security manager denies the needed file
- * operations
- */
- public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final Logger logger) throws FileNotFoundException, IOException {
- return FileUtils.copyFile(source, destination, lockInputFile, lockOutputFile, false, logger);
- }
-
- public static long copyFile(final File source, final OutputStream stream, final boolean closeOutputStream, final boolean lockInputFile) throws FileNotFoundException, IOException {
- FileInputStream fis = null;
- FileLock inLock = null;
- long fileSize = 0L;
- try {
- fis = new FileInputStream(source);
- final FileChannel in = fis.getChannel();
- if (lockInputFile) {
- inLock = in.tryLock(0, Long.MAX_VALUE, true);
- if (inLock == null) {
- throw new IOException("Unable to obtain exclusive file lock for: " + source.getAbsolutePath());
- }
-
- }
-
- byte[] buffer = new byte[1 << 18]; //256 KB
- int bytesRead = -1;
- while ((bytesRead = fis.read(buffer)) != -1) {
- stream.write(buffer, 0, bytesRead);
- }
- in.force(false);
- stream.flush();
- fileSize = in.size();
- } finally {
- FileUtils.releaseQuietly(inLock);
- FileUtils.closeQuietly(fis);
- if (closeOutputStream) {
- FileUtils.closeQuietly(stream);
- }
- }
- return fileSize;
- }
-
- public static long copyFile(final InputStream stream, final File destination, final boolean closeInputStream, final boolean lockOutputFile) throws FileNotFoundException, IOException {
- final Path destPath = destination.toPath();
- final long size = Files.copy(stream, destPath);
- if (closeInputStream) {
- stream.close();
- }
- return size;
- }
-
- /**
- * Renames the given file from the source path to the destination path. This
- * handles multiple attempts. This should only be used to rename within a
- * given directory. Renaming across directories might not work well. See the
- * <code>File.renameTo</code> for more information.
- *
- * @param source the file to rename
- * @param destination the file path to rename to
- * @param maxAttempts the max number of attempts to attempt the rename
- * @throws IOException if rename isn't successful
- */
- public static void renameFile(final File source, final File destination, final int maxAttempts) throws IOException {
- FileUtils.renameFile(source, destination, maxAttempts, false);
- }
-
- /**
- * Renames the given file from the source path to the destination path. This
- * handles multiple attempts. This should only be used to rename within a
- * given directory. Renaming across directories might not work well. See the
- * <code>File.renameTo</code> for more information.
- *
- * @param source the file to rename
- * @param destination the file path to rename to
- * @param maxAttempts the max number of attempts to attempt the rename
- * @param replace if true and a rename attempt fails will check if a file is
- * already at the destination path. If so it will delete that file and
- * attempt the rename according the remaining maxAttempts. If false, any
- * conflicting files will be left as they were and the rename attempts will
- * fail if conflicting.
- * @throws IOException if rename isn't successful
- */
- public static void renameFile(final File source, final File destination, final int maxAttempts, final boolean replace) throws IOException {
- final int attempts = (replace || maxAttempts < 1) ? Math.max(2, maxAttempts) : maxAttempts;
- boolean renamed = false;
- for (int i = 0; i < attempts; i++) {
- renamed = source.renameTo(destination);
- if (!renamed) {
- FileUtils.deleteFile(destination, null, 5);
- } else {
- break; //rename has succeeded
- }
- }
- if (!renamed) {
- throw new IOException("Attempted " + maxAttempts + " times but unable to rename from \'" + source.getPath() + "\' to \'" + destination.getPath() + "\'");
-
- }
- }
-
- public static void sleepQuietly(final long millis) {
- try {
- Thread.sleep(millis);
- } catch (final InterruptedException ex) {
- /* do nothing */
- }
- }
-
- /**
- * Syncs a primary copy of a file with the copy in the restore directory. If
- * the restore directory does not have a file and the primary has a file,
- * the the primary's file is copied to the restore directory. Else if the
- * restore directory has a file, but the primary does not, then the
- * restore's file is copied to the primary directory. Else if the primary
- * file is different than the restore file, then an IllegalStateException is
- * thrown. Otherwise, if neither file exists, then no syncing is performed.
- *
- * @param primaryFile the primary file
- * @param restoreFile the restore file
- * @param logger a logger
- * @throws IOException if an I/O problem was encountered during syncing
- * @throws IllegalStateException if the primary and restore copies exist but
- * are different
- */
- public static void syncWithRestore(final File primaryFile, final File restoreFile, final Logger logger)
- throws IOException {
-
- if (primaryFile.exists() && !restoreFile.exists()) {
- // copy primary file to restore
- copyFile(primaryFile, restoreFile, false, false, logger);
- } else if (restoreFile.exists() && !primaryFile.exists()) {
- // copy restore file to primary
- copyFile(restoreFile, primaryFile, false, false, logger);
- } else if (primaryFile.exists() && restoreFile.exists() && !isSame(primaryFile, restoreFile)) {
- throw new IllegalStateException(String.format("Primary file '%s' is different than restore file '%s'",
- primaryFile.getAbsoluteFile(), restoreFile.getAbsolutePath()));
- }
- }
-
- /**
- * Returns true if the given files are the same according to their MD5 hash.
- *
- * @param file1 a file
- * @param file2 a file
- * @return true if the files are the same; false otherwise
- * @throws IOException if the MD5 hash could not be computed
- */
- public static boolean isSame(final File file1, final File file2) throws IOException {
- return Arrays.equals(computeMd5Digest(file1), computeMd5Digest(file2));
- }
-
- /**
- * Returns the MD5 hash of the given file.
- *
- * @param file a file
- * @return the MD5 hash
- * @throws IOException if the MD5 hash could not be computed
- */
- public static byte[] computeMd5Digest(final File file) throws IOException {
- BufferedInputStream bis = null;
- try {
- bis = new BufferedInputStream(new FileInputStream(file));
- return DigestUtils.md5(bis);
- } finally {
- FileUtils.closeQuietly(bis);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/.gitignore
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/.gitignore b/commons/nifi-stream-utils/.gitignore
deleted file mode 100755
index ea8c4bf..0000000
--- a/commons/nifi-stream-utils/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/target
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/pom.xml
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/pom.xml b/commons/nifi-stream-utils/pom.xml
deleted file mode 100644
index ae06436..0000000
--- a/commons/nifi-stream-utils/pom.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?xml version="1.0"?>
-<!--
- 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.
--->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.nifi</groupId>
- <artifactId>nifi-commons-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
-
- <artifactId>nifi-stream-utils</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>NiFi Stream Utils</name>
-
-</project>
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedInputStream.java
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedInputStream.java b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedInputStream.java
deleted file mode 100644
index 57adb8c..0000000
--- a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedInputStream.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.io;
-
-import java.io.InputStream;
-
-/**
- * This class is a slight modification of the BufferedInputStream in the java.io
- * package. The modification is that this implementation does not provide
- * synchronization on method calls, which means that this class is not suitable
- * for use by multiple threads. However, the absence of these synchronized
- * blocks results in potentially much better performance.
- */
-public class BufferedInputStream extends java.io.BufferedInputStream {
-
- public BufferedInputStream(final InputStream in) {
- super(in);
- }
-
- public BufferedInputStream(final InputStream in, final int size) {
- super(in, size);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedOutputStream.java
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedOutputStream.java b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedOutputStream.java
deleted file mode 100644
index 56caf65..0000000
--- a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/BufferedOutputStream.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * 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.io;
-
-import java.io.FilterOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-
-/**
- * This class is a slight modification of the
- * {@link java.io.BufferedOutputStream} class. This implementation differs in
- * that it does not mark methods as synchronized. This means that this class is
- * not suitable for writing by multiple concurrent threads. However, the removal
- * of the synchronized keyword results in potentially much better performance.
- */
-public class BufferedOutputStream extends FilterOutputStream {
-
- /**
- * The internal buffer where data is stored.
- */
- protected byte buf[];
-
- /**
- * The number of valid bytes in the buffer. This value is always in the
- * range <tt>0</tt> through <tt>buf.length</tt>; elements
- * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid byte data.
- */
- protected int count;
-
- /**
- * Creates a new buffered output stream to write data to the specified
- * underlying output stream.
- *
- * @param out the underlying output stream.
- */
- public BufferedOutputStream(OutputStream out) {
- this(out, 8192);
- }
-
- /**
- * Creates a new buffered output stream to write data to the specified
- * underlying output stream with the specified buffer size.
- *
- * @param out the underlying output stream.
- * @param size the buffer size.
- * @exception IllegalArgumentException if size <= 0.
- */
- public BufferedOutputStream(OutputStream out, int size) {
- super(out);
- if (size <= 0) {
- throw new IllegalArgumentException("Buffer size <= 0");
- }
- buf = new byte[size];
- }
-
- /**
- * Flush the internal buffer
- */
- private void flushBuffer() throws IOException {
- if (count > 0) {
- out.write(buf, 0, count);
- count = 0;
- }
- }
-
- /**
- * Writes the specified byte to this buffered output stream.
- *
- * @param b the byte to be written.
- * @exception IOException if an I/O error occurs.
- */
- @Override
- public void write(int b) throws IOException {
- if (count >= buf.length) {
- flushBuffer();
- }
- buf[count++] = (byte) b;
- }
-
- /**
- * Writes <code>len</code> bytes from the specified byte array starting at
- * offset <code>off</code> to this buffered output stream.
- *
- * <p>
- * Ordinarily this method stores bytes from the given array into this
- * stream's buffer, flushing the buffer to the underlying output stream as
- * needed. If the requested length is at least as large as this stream's
- * buffer, however, then this method will flush the buffer and write the
- * bytes directly to the underlying output stream. Thus redundant
- * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
- *
- * @param b the data.
- * @param off the start offset in the data.
- * @param len the number of bytes to write.
- * @exception IOException if an I/O error occurs.
- */
- @Override
- public void write(byte b[], int off, int len) throws IOException {
- if (len >= buf.length) {
- /* If the request length exceeds the size of the output buffer,
- flush the output buffer and then write the data directly.
- In this way buffered streams will cascade harmlessly. */
- flushBuffer();
- out.write(b, off, len);
- return;
- }
- if (len >= buf.length - count) {
- flushBuffer();
- }
- System.arraycopy(b, off, buf, count, len);
- count += len;
- }
-
- /**
- * Flushes this buffered output stream. This forces any buffered output
- * bytes to be written out to the underlying output stream.
- *
- * @exception IOException if an I/O error occurs.
- * @see java.io.FilterOutputStream#out
- */
- @Override
- public void flush() throws IOException {
- flushBuffer();
- out.flush();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayInputStream.java
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayInputStream.java b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayInputStream.java
deleted file mode 100644
index 9defab4..0000000
--- a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayInputStream.java
+++ /dev/null
@@ -1,250 +0,0 @@
-/*
- * 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.io;
-
-import java.io.InputStream;
-
-/**
- * This class performs the same function as java.io.ByteArrayInputStream but
- * does not mark its methods as synchronized
- */
-public class ByteArrayInputStream extends InputStream {
-
- /**
- * An array of bytes that was provided by the creator of the stream.
- * Elements <code>buf[0]</code> through <code>buf[count-1]</code> are the
- * only bytes that can ever be read from the stream; element
- * <code>buf[pos]</code> is the next byte to be read.
- */
- protected byte buf[];
-
- /**
- * The index of the next character to read from the input stream buffer.
- * This value should always be nonnegative and not larger than the value of
- * <code>count</code>. The next byte to be read from the input stream buffer
- * will be <code>buf[pos]</code>.
- */
- protected int pos;
-
- /**
- * The currently marked position in the stream. ByteArrayInputStream objects
- * are marked at position zero by default when constructed. They may be
- * marked at another position within the buffer by the <code>mark()</code>
- * method. The current buffer position is set to this point by the
- * <code>reset()</code> method.
- * <p>
- * If no mark has been set, then the value of mark is the offset passed to
- * the constructor (or 0 if the offset was not supplied).
- *
- * @since JDK1.1
- */
- protected int mark = 0;
-
- /**
- * The index one greater than the last valid character in the input stream
- * buffer. This value should always be nonnegative and not larger than the
- * length of <code>buf</code>. It is one greater than the position of the
- * last byte within <code>buf</code> that can ever be read from the input
- * stream buffer.
- */
- protected int count;
-
- /**
- * Creates a <code>ByteArrayInputStream</code> so that it uses
- * <code>buf</code> as its buffer array. The buffer array is not copied. The
- * initial value of <code>pos</code> is <code>0</code> and the initial value
- * of <code>count</code> is the length of <code>buf</code>.
- *
- * @param buf the input buffer.
- */
- public ByteArrayInputStream(byte buf[]) {
- this.buf = buf;
- this.pos = 0;
- this.count = buf.length;
- }
-
- /**
- * Creates <code>ByteArrayInputStream</code> that uses <code>buf</code> as
- * its buffer array. The initial value of <code>pos</code> is
- * <code>offset</code> and the initial value of <code>count</code> is the
- * minimum of <code>offset+length</code> and <code>buf.length</code>. The
- * buffer array is not copied. The buffer's mark is set to the specified
- * offset.
- *
- * @param buf the input buffer.
- * @param offset the offset in the buffer of the first byte to read.
- * @param length the maximum number of bytes to read from the buffer.
- */
- public ByteArrayInputStream(byte buf[], int offset, int length) {
- this.buf = buf;
- this.pos = offset;
- this.count = Math.min(offset + length, buf.length);
- this.mark = offset;
- }
-
- /**
- * Reads the next byte of data from this input stream. The value byte is
- * returned as an <code>int</code> in the range <code>0</code> to
- * <code>255</code>. If no byte is available because the end of the stream
- * has been reached, the value <code>-1</code> is returned.
- * <p>
- * This <code>read</code> method cannot block.
- *
- * @return the next byte of data, or <code>-1</code> if the end of the
- * stream has been reached.
- */
- @Override
- public int read() {
- return (pos < count) ? (buf[pos++] & 0xff) : -1;
- }
-
- /**
- * Reads up to <code>len</code> bytes of data into an array of bytes from
- * this input stream. If <code>pos</code> equals <code>count</code>, then
- * <code>-1</code> is returned to indicate end of file. Otherwise, the
- * number <code>k</code> of bytes read is equal to the smaller of
- * <code>len</code> and <code>count-pos</code>. If <code>k</code> is
- * positive, then bytes <code>buf[pos]</code> through
- * <code>buf[pos+k-1]</code> are copied into <code>b[off]</code> through
- * <code>b[off+k-1]</code> in the manner performed by
- * <code>System.arraycopy</code>. The value <code>k</code> is added into
- * <code>pos</code> and <code>k</code> is returned.
- * <p>
- * This <code>read</code> method cannot block.
- *
- * @param b the buffer into which the data is read.
- * @param off the start offset in the destination array <code>b</code>
- * @param len the maximum number of bytes read.
- * @return the total number of bytes read into the buffer, or
- * <code>-1</code> if there is no more data because the end of the stream
- * has been reached.
- * @exception NullPointerException If <code>b</code> is <code>null</code>.
- * @exception IndexOutOfBoundsException If <code>off</code> is negative,
- * <code>len</code> is negative, or <code>len</code> is greater than
- * <code>b.length - off</code>
- */
- @Override
- public int read(byte b[], int off, int len) {
- if (b == null) {
- throw new NullPointerException();
- } else if (off < 0 || len < 0 || len > b.length - off) {
- throw new IndexOutOfBoundsException();
- }
-
- if (pos >= count) {
- return -1;
- }
-
- int avail = count - pos;
- if (len > avail) {
- len = avail;
- }
- if (len <= 0) {
- return 0;
- }
- System.arraycopy(buf, pos, b, off, len);
- pos += len;
- return len;
- }
-
- /**
- * Skips <code>n</code> bytes of input from this input stream. Fewer bytes
- * might be skipped if the end of the input stream is reached. The actual
- * number <code>k</code> of bytes to be skipped is equal to the smaller of
- * <code>n</code> and <code>count-pos</code>. The value <code>k</code> is
- * added into <code>pos</code> and <code>k</code> is returned.
- *
- * @param n the number of bytes to be skipped.
- * @return the actual number of bytes skipped.
- */
- @Override
- public long skip(long n) {
- long k = count - pos;
- if (n < k) {
- k = n < 0 ? 0 : n;
- }
-
- pos += k;
- return k;
- }
-
- /**
- * Returns the number of remaining bytes that can be read (or skipped over)
- * from this input stream.
- * <p>
- * The value returned is <code>count - pos</code>, which is the number
- * of bytes remaining to be read from the input buffer.
- *
- * @return the number of remaining bytes that can be read (or skipped over)
- * from this input stream without blocking.
- */
- @Override
- public int available() {
- return count - pos;
- }
-
- /**
- * Tests if this <code>InputStream</code> supports mark/reset. The
- * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
- * always returns <code>true</code>.
- *
- * @since JDK1.1
- */
- @Override
- public boolean markSupported() {
- return true;
- }
-
- /**
- * Set the current marked position in the stream. ByteArrayInputStream
- * objects are marked at position zero by default when constructed. They may
- * be marked at another position within the buffer by this method.
- * <p>
- * If no mark has been set, then the value of the mark is the offset passed
- * to the constructor (or 0 if the offset was not supplied).
- *
- * <p>
- * Note: The <code>readAheadLimit</code> for this class has no meaning.
- *
- * @since JDK1.1
- */
- @Override
- public void mark(int readAheadLimit) {
- mark = pos;
- }
-
- /**
- * Resets the buffer to the marked position. The marked position is 0 unless
- * another position was marked or an offset was specified in the
- * constructor.
- */
- @Override
- public void reset() {
- pos = mark;
- }
-
- /**
- * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
- * this class can be called after the stream has been closed without
- * generating an <tt>IOException</tt>.
- * <p>
- */
- @Override
- public void close() {
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/1357a17b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayOutputStream.java
----------------------------------------------------------------------
diff --git a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayOutputStream.java b/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayOutputStream.java
deleted file mode 100644
index b504c23..0000000
--- a/commons/nifi-stream-utils/src/main/java/org/apache/nifi/io/ByteArrayOutputStream.java
+++ /dev/null
@@ -1,250 +0,0 @@
-/*
- * 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.io;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.util.Arrays;
-
-/**
- * This class provides a more efficient implementation of the
- * java.io.ByteArrayOutputStream. The efficiency is gained in two ways:
- * <ul>
- * <li>The write methods are not synchronized</li>
- * <li>The class provides {@link #getUnderlyingBuffer()} and
- * {@link #getBufferLength()}, which can be used to access the underlying byte
- * array directly, rather than the System.arraycopy that {@link #toByteArray()}
- * uses
- * </ul>
- *
- */
-public class ByteArrayOutputStream extends OutputStream {
-
- /**
- * The buffer where data is stored.
- */
- protected byte buf[];
-
- /**
- * The number of valid bytes in the buffer.
- */
- protected int count;
-
- /**
- * Creates a new byte array output stream. The buffer capacity is initially
- * 32 bytes, though its size increases if necessary.
- */
- public ByteArrayOutputStream() {
- this(32);
- }
-
- /**
- * Creates a new byte array output stream, with a buffer capacity of the
- * specified size, in bytes.
- *
- * @param size the initial size.
- * @exception IllegalArgumentException if size is negative.
- */
- public ByteArrayOutputStream(int size) {
- if (size < 0) {
- throw new IllegalArgumentException("Negative initial size: "
- + size);
- }
- buf = new byte[size];
- }
-
- /**
- * Increases the capacity if necessary to ensure that it can hold at least
- * the number of elements specified by the minimum capacity argument.
- *
- * @param minCapacity the desired minimum capacity
- * @throws OutOfMemoryError if {@code minCapacity < 0}. This is interpreted
- * as a request for the unsatisfiably large capacity
- * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
- */
- private void ensureCapacity(int minCapacity) {
- // overflow-conscious code
- if (minCapacity - buf.length > 0) {
- grow(minCapacity);
- }
- }
-
- /**
- * Increases the capacity to ensure that it can hold at least the number of
- * elements specified by the minimum capacity argument.
- *
- * @param minCapacity the desired minimum capacity
- */
- private void grow(int minCapacity) {
- // overflow-conscious code
- int oldCapacity = buf.length;
- int newCapacity = oldCapacity << 1;
- if (newCapacity - minCapacity < 0) {
- newCapacity = minCapacity;
- }
- if (newCapacity < 0) {
- if (minCapacity < 0) // overflow
- {
- throw new OutOfMemoryError();
- }
- newCapacity = Integer.MAX_VALUE;
- }
- buf = Arrays.copyOf(buf, newCapacity);
- }
-
- /**
- * Writes the specified byte to this byte array output stream.
- *
- * @param b the byte to be written.
- */
- @Override
- public void write(int b) {
- ensureCapacity(count + 1);
- buf[count] = (byte) b;
- count += 1;
- }
-
- /**
- * Writes <code>len</code> bytes from the specified byte array starting at
- * offset <code>off</code> to this byte array output stream.
- *
- * @param b the data.
- * @param off the start offset in the data.
- * @param len the number of bytes to write.
- */
- @Override
- public void write(byte b[], int off, int len) {
- if ((off < 0) || (off > b.length) || (len < 0)
- || ((off + len) - b.length > 0)) {
- throw new IndexOutOfBoundsException();
- }
- ensureCapacity(count + len);
- System.arraycopy(b, off, buf, count, len);
- count += len;
- }
-
- /**
- * Writes the complete contents of this byte array output stream to the
- * specified output stream argument, as if by calling the output stream's
- * write method using <code>out.write(buf, 0, count)</code>.
- *
- * @param out the output stream to which to write the data.
- * @exception IOException if an I/O error occurs.
- */
- public void writeTo(OutputStream out) throws IOException {
- out.write(buf, 0, count);
- }
-
- /**
- * Resets the <code>count</code> field of this byte array output stream to
- * zero, so that all currently accumulated output in the output stream is
- * discarded. The output stream can be used again, reusing the already
- * allocated buffer space.
- *
- * @see java.io.ByteArrayInputStream#count
- */
- public void reset() {
- count = 0;
- }
-
- /**
- * Creates a newly allocated byte array. Its size is the current size of
- * this output stream and the valid contents of the buffer have been copied
- * into it.
- *
- * @return the current contents of this output stream, as a byte array.
- * @see java.io.ByteArrayOutputStream#size()
- */
- public byte toByteArray ()
- [] {
- return Arrays.copyOf(buf, count);
- }
-
- /**
- * Returns the current size of the buffer.
- *
- * @return the value of the <code>count</code> field, which is the number of
- * valid bytes in this output stream.
- * @see java.io.ByteArrayOutputStream#count
- */
- public int size() {
- return count;
- }
-
- /**
- * Converts the buffer's contents into a string decoding bytes using the
- * platform's default character set. The length of the new <tt>String</tt>
- * is a function of the character set, and hence may not be equal to the
- * size of the buffer.
- *
- * <p>
- * This method always replaces malformed-input and unmappable-character
- * sequences with the default replacement string for the platform's default
- * character set. The {@linkplain java.nio.charset.CharsetDecoder} class
- * should be used when more control over the decoding process is required.
- *
- * @return String decoded from the buffer's contents.
- * @since JDK1.1
- */
- @Override
- public String toString() {
- return new String(buf, 0, count);
- }
-
- /**
- * Converts the buffer's contents into a string by decoding the bytes using
- * the specified {@link java.nio.charset.Charset charsetName}. The length of
- * the new <tt>String</tt> is a function of the charset, and hence may not
- * be equal to the length of the byte array.
- *
- * <p>
- * This method always replaces malformed-input and unmappable-character
- * sequences with this charset's default replacement string. The {@link
- * java.nio.charset.CharsetDecoder} class should be used when more control
- * over the decoding process is required.
- *
- * @param charsetName the name of a supported
- * {@linkplain java.nio.charset.Charset <code>charset</code>}
- * @return String decoded from the buffer's contents.
- * @exception UnsupportedEncodingException If the named charset is not
- * supported
- * @since JDK1.1
- */
- public String toString(String charsetName) throws UnsupportedEncodingException {
- return new String(buf, 0, count, charsetName);
- }
-
- /**
- * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
- * this class can be called after the stream has been closed without
- * generating an <tt>IOException</tt>.
- * <p>
- *
- */
- @Override
- public void close() {
- }
-
- public byte[] getUnderlyingBuffer() {
- return buf;
- }
-
- public int getBufferLength() {
- return count;
- }
-}