Skip to content

Commit

Permalink
For issue yegor256#594
Browse files Browse the repository at this point in the history
Solving puzzle 558-afc1c081 in
src/main/java/org/takes/rq/RqMultipart.java:131-135
  • Loading branch information
dalifreire committed Mar 3, 2016
1 parent e039016 commit 9f970b1
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,14 @@ final class Base extends RqWrap implements RqMultipart {
* @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.buffer = ByteBuffer.allocate(
// @checkstyle MagicNumberCheck (1 line)
Math.min(8192, stream.available())
);
this.body = Base.body(req);
this.buffer = Base.buffer(req);
this.map = this.requests(req);
}

@Override
public Iterable<Request> part(final CharSequence name) {
final List<Request> values = this.map
Expand Down Expand Up @@ -176,6 +164,32 @@ public Iterable<Request> part(final CharSequence name) {
public Iterable<String> names() {
return this.map.keySet();
}

/**
* Returns the ReadableByteChannel based on request body.
* @param req Original request
* @return The ReadableByteChannel based on request body.
* @throws IOException If fails
*/
private static ReadableByteChannel body(final Request req)
throws IOException {
return Channels.newChannel(new RqLengthAware(req).body());
}

/**
* Returns the ByteBuffer for the request body.
* @param req Original request
* @return The ByteBuffer for the request body.
* @throws IOException If fails
*/
private static ByteBuffer buffer(final Request req)
throws IOException {
return ByteBuffer.allocate(
// @checkstyle MagicNumberCheck (1 line)
Math.min(8192, new RqLengthAware(req).body().available())
);
}

/**
* Build a request for each part of the origin request.
* @param req Origin request
Expand Down

0 comments on commit 9f970b1

Please sign in to comment.