Skip to content

Commit

Permalink
Removed ByteStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Schönborn committed May 13, 2016
1 parent 78456f3 commit 91a6911
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 13 deletions.
51 changes: 51 additions & 0 deletions logbook-core/src/main/java/org/zalando/logbook/Bytes.java
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 logbook-core/src/test/java/org/zalando/logbook/BytesTest.java
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
@OhNoYouDidnt
public final class EnforceCoverageTest {

@Test
public void shouldUseBytesConstructor() {
new Bytes();
}

@Test
public void shouldUseConditionsConstructor() {
new Conditions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.io.ByteStreams;
import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.zalando.logbook.Bytes;
import org.zalando.logbook.Origin;
import org.zalando.logbook.RawHttpRequest;

Expand Down Expand Up @@ -158,10 +158,10 @@ public byte[] getBody() {
@Override
public org.zalando.logbook.HttpRequest withBody() throws IOException {
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntityEnclosingRequest foo = (HttpEntityEnclosingRequest) request;
final InputStream content = foo.getEntity().getContent();
this.body = ByteStreams.toByteArray(content);
foo.setEntity(new ByteArrayEntity(body));
final HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) this.request;
final InputStream content = request.getEntity().getContent();
this.body = Bytes.toByteArray(content);
request.setEntity(new ByteArrayEntity(body));
} else {
this.body = new byte[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.io.ByteStreams;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.zalando.logbook.Bytes;
import org.zalando.logbook.Origin;
import org.zalando.logbook.RawHttpResponse;

Expand Down Expand Up @@ -104,7 +104,7 @@ public org.zalando.logbook.HttpResponse withBody() throws IOException {
return this;
}

this.body = ByteStreams.toByteArray(entity.getContent());
this.body = Bytes.toByteArray(entity.getContent());
response.setEntity(new ByteArrayEntity(body));

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

import static com.google.common.io.ByteStreams.toByteArray;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.hasSize;
Expand All @@ -54,6 +53,7 @@
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.zalando.logbook.Bytes.toByteArray;

public final class LocalRequestTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

import static com.github.restdriver.clientdriver.RestClientDriver.giveResponse;
import static com.github.restdriver.clientdriver.RestClientDriver.onRequestTo;
import static com.google.common.io.ByteStreams.toByteArray;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -51,6 +50,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.zalando.logbook.Bytes.toByteArray;

public final class LogbookHttpInterceptorsTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

import com.google.common.collect.ListMultimap;
import com.google.common.io.ByteStreams;
import org.zalando.logbook.Bytes;
import org.zalando.logbook.HttpRequest;
import org.zalando.logbook.Origin;
import org.zalando.logbook.RawHttpRequest;
Expand Down Expand Up @@ -116,7 +116,7 @@ public Charset getCharset() {
@Override
public HttpRequest withBody() throws IOException {
final ServletInputStream stream = getInputStream();
final byte[] bytes = ByteStreams.toByteArray(stream);
final byte[] bytes = Bytes.toByteArray(stream);
output.write(bytes);
this.body = bytes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
* #L%
*/

import com.google.common.io.ByteStreams;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zalando.logbook.Bytes;

import javax.annotation.Nullable;
import javax.servlet.ServletInputStream;
Expand Down Expand Up @@ -102,7 +102,7 @@ public void readBytes(final HttpServletRequest request, final HttpServletRespons

@RequestMapping(value = "/stream", produces = MediaType.TEXT_PLAIN_VALUE)
public void stream(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
ByteStreams.copy(request.getInputStream(), response.getOutputStream());
Bytes.copy(request.getInputStream(), response.getOutputStream());
}

@RequestMapping(value = "/reader", produces = MediaType.TEXT_PLAIN_VALUE)
Expand Down

0 comments on commit 91a6911

Please sign in to comment.