-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Willi Schönborn
committed
May 13, 2016
1 parent
78456f3
commit 91a6911
Showing
9 changed files
with
122 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.zalando.logbook; | ||
|
||
/* | ||
* #%L | ||
* Logbook: Core | ||
* %% | ||
* Copyright (C) 2015 - 2016 Zalando SE | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public final class Bytes { | ||
|
||
Bytes() { | ||
// package private so we can trick code coverage | ||
} | ||
|
||
public static byte[] toByteArray(final InputStream stream) throws IOException { | ||
final ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
copy(stream, output); | ||
return output.toByteArray(); | ||
} | ||
|
||
public static void copy(final InputStream input, final OutputStream output) throws IOException { | ||
final byte[] buffer = new byte[4096]; | ||
while (true) { | ||
final int read = input.read(buffer); | ||
if (read == -1) { | ||
break; | ||
} | ||
output.write(buffer, 0, read); | ||
} | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
logbook-core/src/test/java/org/zalando/logbook/BytesTest.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,53 @@ | ||
package org.zalando.logbook; | ||
|
||
/* | ||
* #%L | ||
* Logbook: Core | ||
* %% | ||
* Copyright (C) 2015 - 2016 Zalando SE | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public final class BytesTest { | ||
|
||
@Test | ||
public void shouldCopyToByteArray() throws IOException { | ||
final byte[] bytes = "abc".getBytes(UTF_8); | ||
assertThat(Bytes.toByteArray(new ByteArrayInputStream(bytes)), is(bytes)); | ||
} | ||
|
||
@Test | ||
public void shouldCopyStreams() throws IOException { | ||
final byte[] bytes = "abc".getBytes(UTF_8); | ||
|
||
final ByteArrayInputStream input = new ByteArrayInputStream(bytes); | ||
final ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
|
||
Bytes.copy(input, output); | ||
|
||
assertThat(output.toByteArray(), is(bytes)); | ||
} | ||
|
||
} |
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
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
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
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