Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow direct byte array entities in responses
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
core/src/main/java/org/chodavarapu/datamill/http/impl/BytesEntity.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,36 @@ | ||
package org.chodavarapu.datamill.http.impl; | ||
|
||
import org.chodavarapu.datamill.http.Entity; | ||
import org.chodavarapu.datamill.json.JsonObject; | ||
import rx.Observable; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class BytesEntity implements Entity { | ||
private byte[] bytes; | ||
|
||
public BytesEntity(byte[] bytes) { | ||
this.bytes = bytes; | ||
} | ||
|
||
@Override | ||
public Observable<byte[]> asBytes() { | ||
return Observable.just(bytes); | ||
} | ||
|
||
@Override | ||
public Observable<byte[]> asChunks() { | ||
return asBytes(); | ||
} | ||
|
||
@Override | ||
public Observable<JsonObject> asJson() { | ||
return asString().map(json -> new JsonObject(json)); | ||
} | ||
|
||
@Override | ||
public Observable<String> asString() { | ||
return asBytes().map(bytes -> new String(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
32 changes: 32 additions & 0 deletions
32
core/src/test/java/org/chodavarapu/datamill/http/impl/BytesEntityTest.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,32 @@ | ||
package org.chodavarapu.datamill.http.impl; | ||
|
||
import org.apache.commons.io.output.ByteArrayOutputStream; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class BytesEntityTest { | ||
@Test | ||
public void entityTests() { | ||
assertEquals("test", new BytesEntity("test".getBytes()).asString().toBlocking().last()); | ||
assertEquals("test", new String(new BytesEntity("test".getBytes()).asBytes().toBlocking().last())); | ||
assertEquals("test", new String(new BytesEntity("test".getBytes()) | ||
.asChunks() | ||
.collect( | ||
() -> new ByteArrayOutputStream(), | ||
(stream, chunk) -> { | ||
try { | ||
stream.write(chunk); | ||
} catch (IOException e) { | ||
} | ||
}) | ||
.map(stream -> stream.toByteArray()).toBlocking().last())); | ||
assertEquals("value", new BytesEntity("{\"name\":\"value\"}".getBytes()) | ||
.asJson().toBlocking().last().get("name").asString()); | ||
} | ||
} |