-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(#1095) Remove code from ctor #1098
Open
baudoliver7
wants to merge
4
commits into
yegor256:master
Choose a base branch
from
baudoliver7:remove_code_from_ctor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -43,6 +43,8 @@ | |
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.scalar.Sticky; | ||
import org.cactoos.scalar.Unchecked; | ||
import org.cactoos.text.FormattedText; | ||
import org.cactoos.text.Lowered; | ||
|
@@ -102,19 +104,19 @@ public final class RqMtBase implements RqMultipart { | |
private static final String CRLF = "\r\n"; | ||
|
||
/** | ||
* Map of params and values. | ||
* Scalar of Map of params and values. | ||
*/ | ||
private final Map<String, List<Request>> map; | ||
private final Scalar<Map<String, List<Request>>> smap; | ||
|
||
/** | ||
* Internal buffer. | ||
* Scalar of Internal buffer. | ||
*/ | ||
private final ByteBuffer buffer; | ||
private final Scalar<ByteBuffer> sbuffer; | ||
|
||
/** | ||
* InputStream based on request body. | ||
* Scalar of InputStream based on request body. | ||
*/ | ||
private final InputStream stream; | ||
private final Scalar<InputStream> sstream; | ||
|
||
/** | ||
* Original request. | ||
|
@@ -125,27 +127,24 @@ public final class RqMtBase implements RqMultipart { | |
* Ctor. | ||
* @param req Original request | ||
* @throws IOException If fails | ||
* @todo #950:30m Remove code from this ctor, leaving only | ||
* initialization. Currently this constructor access body | ||
* of the request and triggers its evaluation. This breaks | ||
* composition of multiple request, as it can be seen in | ||
* {@link RqMtFake}. When this task is done, remove | ||
* explicit lazy evaluation for RqMtFake. | ||
* @checkstyle ExecutableStatementCountCheck (2 lines) | ||
*/ | ||
public RqMtBase(final Request req) throws IOException { | ||
this.origin = req; | ||
this.stream = new RqLengthAware(req).body(); | ||
this.buffer = ByteBuffer.allocate( | ||
// @checkstyle MagicNumberCheck (1 line) | ||
Math.min(8192, this.stream.available()) | ||
); | ||
this.map = this.requests(req); | ||
this.sstream = new Sticky<>(() -> new RqLengthAware(req).body()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baudoliver7 Is it possible to use |
||
this.sbuffer = new Sticky<>(() -> { | ||
return ByteBuffer.allocate( | ||
// @checkstyle MagicNumberCheck (1 line) | ||
Math.min(8192, RqMtBase.this.sstream.value().available()) | ||
); | ||
}); | ||
this.smap = new Sticky<>(() -> RqMtBase.this.requests(req)); | ||
} | ||
|
||
@Override | ||
public Iterable<Request> part(final CharSequence name) { | ||
final List<Request> values = this.map.getOrDefault( | ||
final Map<String, List<Request>> map = new Unchecked<>(this.smap).value(); | ||
final List<Request> values = map.getOrDefault( | ||
new UncheckedText( | ||
new Lowered(name.toString()) | ||
).asString(), | ||
|
@@ -157,7 +156,7 @@ public Iterable<Request> part(final CharSequence name) { | |
Collections.emptyList(), | ||
new FormattedText( | ||
"there are no parts by name \"%s\" among %d others: %s", | ||
name, this.map.size(), this.map.keySet() | ||
name, map.size(), map.keySet() | ||
).toString() | ||
); | ||
} else { | ||
|
@@ -174,7 +173,7 @@ public Iterable<Request> part(final CharSequence name) { | |
|
||
@Override | ||
public Iterable<String> names() { | ||
return this.map.keySet(); | ||
return new Unchecked<>(this.smap).value().keySet(); | ||
} | ||
|
||
@Override | ||
|
@@ -222,8 +221,10 @@ private Map<String, List<Request>> requests( | |
) | ||
); | ||
} | ||
final ReadableByteChannel body = Channels.newChannel(this.stream); | ||
if (body.read(this.buffer) < 0) { | ||
final ByteBuffer buffer = new Unchecked<>(this.sbuffer).value(); | ||
final InputStream stream = new Unchecked<>(this.sstream).value(); | ||
final ReadableByteChannel body = Channels.newChannel(stream); | ||
if (body.read(buffer) < 0) { | ||
throw new HttpException( | ||
HttpURLConnection.HTTP_BAD_REQUEST, | ||
"failed to read the request body" | ||
|
@@ -232,15 +233,15 @@ private Map<String, List<Request>> requests( | |
final byte[] boundary = String.format( | ||
"%s--%s", RqMtBase.CRLF, matcher.group(1) | ||
).getBytes(RqMtBase.ENCODING); | ||
this.buffer.flip(); | ||
this.buffer.position(boundary.length - 2); | ||
buffer.flip(); | ||
buffer.position(boundary.length - 2); | ||
final Collection<Request> requests = new LinkedList<>(); | ||
while (this.buffer.hasRemaining()) { | ||
final byte data = this.buffer.get(); | ||
while (buffer.hasRemaining()) { | ||
final byte data = buffer.get(); | ||
if (data == '-') { | ||
break; | ||
} | ||
this.buffer.position(this.buffer.position() + 1); | ||
buffer.position(buffer.position() + 1); | ||
requests.add(this.make(boundary, body)); | ||
} | ||
return RqMtBase.asMap(requests); | ||
|
@@ -292,30 +293,31 @@ private void copy(final WritableByteChannel target, | |
throws IOException { | ||
int match = 0; | ||
boolean cont = true; | ||
final ByteBuffer buffer = new Unchecked<>(this.sbuffer).value(); | ||
while (cont) { | ||
if (!this.buffer.hasRemaining()) { | ||
this.buffer.clear(); | ||
if (!buffer.hasRemaining()) { | ||
buffer.clear(); | ||
for (int idx = 0; idx < match; ++idx) { | ||
this.buffer.put(boundary[idx]); | ||
buffer.put(boundary[idx]); | ||
} | ||
match = 0; | ||
if (body.read(this.buffer) == -1) { | ||
if (body.read(buffer) == -1) { | ||
break; | ||
} | ||
this.buffer.flip(); | ||
buffer.flip(); | ||
} | ||
final ByteBuffer btarget = this.buffer.slice(); | ||
final int offset = this.buffer.position(); | ||
final ByteBuffer btarget = buffer.slice(); | ||
final int offset = buffer.position(); | ||
btarget.limit(0); | ||
while (this.buffer.hasRemaining()) { | ||
final byte data = this.buffer.get(); | ||
while (buffer.hasRemaining()) { | ||
final byte data = buffer.get(); | ||
if (data == boundary[match]) { | ||
++match; | ||
} else if (data == boundary[0]) { | ||
match = 1; | ||
} else { | ||
match = 0; | ||
btarget.limit(this.buffer.position() - offset); | ||
btarget.limit(buffer.position() - offset); | ||
} | ||
if (match == boundary.length) { | ||
cont = false; | ||
|
@@ -378,7 +380,7 @@ public void close() throws IOException { | |
super.close(); | ||
} finally { | ||
for (final List<Request> requests | ||
: RqMtBase.this.map.values()) { | ||
: new Unchecked<>(RqMtBase.this.smap).value().values()) { | ||
for (final Request request : requests) { | ||
request.body().close(); | ||
} | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baudoliver7 I suggest reverting this part, if xcop doesn't complain than xml formating is fine