Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 6, 2016
2 parents 748de47 + d1bec6a commit d9e7bdd
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,21 @@ final class Base extends RqWrap implements RqMultipart {
*/
private final transient ByteBuffer buffer;
/**
* Origin request body.
* InputStream based on request body.
*/
private final transient ReadableByteChannel body;
private final transient InputStream stream;
/**
* Ctor.
* @param req Original request
* @throws IOException If fails
* @checkstyle ExecutableStatementCountCheck (2 lines)
* @todo #558:30min Base ctor. According to new qulice version,
* constructor must contain only variables initialization and other
* constructor calls. Refactor code according to that rule and
* remove `ConstructorOnlyInitializesOrCallOtherConstructors`
* warning suppression.
*/
@SuppressWarnings
(
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
)
public Base(final Request req) throws IOException {
super(req);
final InputStream stream = new RqLengthAware(req).body();
this.body = Channels.newChannel(stream);
this.stream = new RqLengthAware(req).body();
this.buffer = ByteBuffer.allocate(
// @checkstyle MagicNumberCheck (1 line)
Math.min(8192, stream.available())
Math.min(8192, this.stream.available())
);
this.map = this.requests(req);
}
Expand Down Expand Up @@ -210,7 +200,8 @@ private Map<String, List<Request>> requests(
)
);
}
if (this.body.read(this.buffer) < 0) {
final ReadableByteChannel body = Channels.newChannel(this.stream);
if (body.read(this.buffer) < 0) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
"failed to read the request body"
Expand All @@ -228,7 +219,7 @@ private Map<String, List<Request>> requests(
break;
}
this.buffer.position(this.buffer.position() + 1);
requests.add(this.make(boundary));
requests.add(this.make(boundary, body));
}
return RqMultipart.Base.asMap(requests);
}
Expand All @@ -237,14 +228,16 @@ private Map<String, List<Request>> requests(
* Scans the origin request until the boundary reached. Caches
* the content into a temporary file and returns it as a new request.
* @param boundary Boundary
* @param body Origin request body
* @return Request
* @throws IOException If fails
* @todo #254:30min in order to delete temporary files InputStream
* instance on Request.body should be closed. In context of multipart
* requests that means that body of all parts should be closed once
* they are not needed anymore.
*/
private Request make(final byte[] boundary) throws IOException {
private Request make(final byte[] boundary,
final ReadableByteChannel body) throws IOException {
final File file = File.createTempFile(
RqMultipart.class.getName(), ".tmp"
);
Expand All @@ -265,7 +258,7 @@ private Request make(final byte[] boundary) throws IOException {
RqMultipart.Fake.CRLF.getBytes(StandardCharsets.UTF_8)
)
);
this.copy(channel, boundary);
this.copy(channel, boundary, body);
} finally {
channel.close();
}
Expand All @@ -284,11 +277,13 @@ private Request make(final byte[] boundary) throws IOException {
* Copy until boundary reached.
* @param target Output file channel
* @param boundary Boundary
* @param body Origin request body
* @throws IOException If fails
* @checkstyle ExecutableStatementCountCheck (2 lines)
*/
private void copy(final WritableByteChannel target,
final byte[] boundary) throws IOException {
final byte[] boundary, final ReadableByteChannel body)
throws IOException {
int match = 0;
boolean cont = true;
while (cont) {
Expand All @@ -298,7 +293,7 @@ private void copy(final WritableByteChannel target,
this.buffer.put(boundary[idx]);
}
match = 0;
if (this.body.read(this.buffer) == -1) {
if (body.read(this.buffer) == -1) {
break;
}
this.buffer.flip();
Expand Down

0 comments on commit d9e7bdd

Please sign in to comment.