-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from ketan/remove-commons-io
Remove dependency on apache-commons-io.
- Loading branch information
Showing
6 changed files
with
248 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<!-- Copyright (C) 2013 ZeroTurnaround <[email protected]> Contains | ||
fragments of code from Apache Commons Exec, rights owned by Apache Software | ||
Foundation (ASF). Licensed 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 | ||
<!-- Copyright (C) 2013 ZeroTurnaround <[email protected]> Contains | ||
fragments of code from Apache Commons Exec, rights owned by Apache Software | ||
Foundation (ASF). Licensed 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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
@@ -42,11 +42,6 @@ | |
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>2.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
|
@@ -71,6 +66,12 @@ | |
<version>3.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>2.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/zeroturnaround/exec/stream/NullOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2019 Ketan Padegaonkar <[email protected]> | ||
* | ||
* Licensed 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.zeroturnaround.exec.stream; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* An OutputStream which ignores everything written to it. | ||
*/ | ||
public class NullOutputStream extends OutputStream { | ||
|
||
/** | ||
* A singleton. | ||
*/ | ||
public static final NullOutputStream NULL_OUTPUT_STREAM = new NullOutputStream(); | ||
|
||
public void write(byte[] b, int off, int len) { | ||
// discard! | ||
} | ||
|
||
public void write(int b) { | ||
// discard! | ||
} | ||
|
||
public void write(byte[] b) throws IOException { | ||
// discard! | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/org/zeroturnaround/exec/stream/TeeOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (C) 2019 Ketan Padegaonkar <[email protected]> | ||
* | ||
* Licensed 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.zeroturnaround.exec.stream; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Splits an OutputStream into two. Named after the unix 'tee' | ||
* command. It allows a stream to be branched off so there | ||
* are now two streams. | ||
*/ | ||
public class TeeOutputStream extends OutputStream { | ||
private final OutputStream left; | ||
private final OutputStream right; | ||
|
||
public TeeOutputStream(OutputStream left, OutputStream right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
/** | ||
* Write a byte array to both output streams. | ||
* | ||
* @param b the data. | ||
* @param off the start offset in the data. | ||
* @param len the number of bytes to write. | ||
* @throws IOException on error. | ||
*/ | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
left.write(b, off, len); | ||
right.write(b, off, len); | ||
} | ||
|
||
|
||
/** | ||
* Write a byte to both output streams. | ||
* | ||
* @param b the byte to write. | ||
* @throws IOException on error. | ||
*/ | ||
public void write(int b) throws IOException { | ||
left.write(b); | ||
right.write(b); | ||
} | ||
|
||
|
||
/** | ||
* Write a byte array to both output streams. | ||
* | ||
* @param b an array of bytes. | ||
* @throws IOException on error. | ||
*/ | ||
public void write(byte[] b) throws IOException { | ||
left.write(b); | ||
right.write(b); | ||
} | ||
|
||
/** | ||
* Closes both output streams | ||
* | ||
* @throws IOException on error. | ||
*/ | ||
@Override | ||
public void close() throws IOException { | ||
try { | ||
left.close(); | ||
} finally { | ||
right.close(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Flush both output streams. | ||
* | ||
* @throws IOException on error | ||
*/ | ||
@Override | ||
public void flush() throws IOException { | ||
left.flush(); | ||
right.flush(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/org/zeroturnaround/exec/stream/TeeOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2019 Ketan Padegaonkar <[email protected]> | ||
* | ||
* Licensed 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.zeroturnaround.exec.stream; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.zeroturnaround.exec.test.RememberCloseOutputStream; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
public class TeeOutputStreamTest { | ||
|
||
public static class ExceptionOnCloseByteArrayOutputStream extends RememberCloseOutputStream { | ||
|
||
public ExceptionOnCloseByteArrayOutputStream(OutputStream out) { | ||
super(out); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
throw new IOException(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldCopyContentsToBothStreams() throws IOException { | ||
ByteArrayOutputStream left = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream right = new ByteArrayOutputStream(); | ||
TeeOutputStream teeOutputStream = new TeeOutputStream(left, right); | ||
|
||
teeOutputStream.write(10); | ||
teeOutputStream.write(new byte[]{1, 2, 3}); | ||
teeOutputStream.write(new byte[]{10, 11, 12, 13, 14, 15, 15, 16}, 2, 3); | ||
|
||
Assert.assertArrayEquals(new byte[]{10, 1, 2, 3, 12, 13, 14}, left.toByteArray()); | ||
Assert.assertArrayEquals(new byte[]{10, 1, 2, 3, 12, 13, 14}, right.toByteArray()); | ||
} | ||
|
||
@Test | ||
public void shouldCloseBothStreamsWhenClosingTee() throws IOException { | ||
RememberCloseOutputStream left = new RememberCloseOutputStream(NullOutputStream.NULL_OUTPUT_STREAM); | ||
RememberCloseOutputStream right = new RememberCloseOutputStream(NullOutputStream.NULL_OUTPUT_STREAM); | ||
TeeOutputStream teeOutputStream = new TeeOutputStream(left, right); | ||
|
||
teeOutputStream.close(); | ||
|
||
Assert.assertTrue(left.isClosed()); | ||
Assert.assertTrue(right.isClosed()); | ||
} | ||
|
||
@Test | ||
public void shouldCloseSecondStreamWhenClosingFirstFails() { | ||
ExceptionOnCloseByteArrayOutputStream left = new ExceptionOnCloseByteArrayOutputStream(NullOutputStream.NULL_OUTPUT_STREAM); | ||
RememberCloseOutputStream right = new RememberCloseOutputStream(NullOutputStream.NULL_OUTPUT_STREAM); | ||
TeeOutputStream teeOutputStream = new TeeOutputStream(left, right); | ||
try { | ||
teeOutputStream.close(); | ||
Assert.fail("Was expecting an exception!"); | ||
} catch (IOException expected) { | ||
|
||
} | ||
|
||
Assert.assertTrue(left.isClosed()); | ||
Assert.assertTrue(right.isClosed()); | ||
} | ||
} |