-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
improvement: allowing more granular control of reading behaviour for base64 #646
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af79cf0
improvement: allowing more granular control of reading behaviour for …
pavan-kalyan fb36d65
docs: adding @since 2.12 to java docs for non-private methods
pavan-kalyan 5c60aa6
tests: added tests for read behaviour and a few fixes
pavan-kalyan 9cc8d93
tests: fixing error messages and comments for test
pavan-kalyan 74ea8d3
docs: added more relevant java docs
pavan-kalyan f330dd6
docs: adding padding info to each variant and linking method
pavan-kalyan 26f0807
docs: typo
pavan-kalyan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,11 @@ public final class Base64Variant | |
*/ | ||
private final transient boolean _usesPadding; | ||
|
||
/** | ||
* Whether padding characters should be required or not while decoding | ||
*/ | ||
private final transient PaddingReadBehaviour _paddingReadBehaviour; | ||
|
||
/** | ||
* Character used for padding, if any ({@link #PADDING_CHAR_NONE} if not). | ||
*/ | ||
|
@@ -136,6 +141,12 @@ public Base64Variant(String name, String base64Alphabet, boolean usesPadding, ch | |
if (usesPadding) { | ||
_asciiToBase64[(int) paddingChar] = BASE64_VALUE_PADDING; | ||
} | ||
|
||
if (usesPadding) { | ||
this._paddingReadBehaviour = PaddingReadBehaviour.PADDING_REQUIRED; | ||
} else { | ||
this._paddingReadBehaviour = PaddingReadBehaviour.PADDING_FORBIDDEN; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -154,6 +165,11 @@ public Base64Variant(Base64Variant base, String name, int maxLineLength) | |
* line length) differ | ||
*/ | ||
public Base64Variant(Base64Variant base, String name, boolean usesPadding, char paddingChar, int maxLineLength) | ||
{ | ||
cowtowncoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this(base, name, usesPadding, paddingChar, base._paddingReadBehaviour, maxLineLength); | ||
} | ||
|
||
private Base64Variant(Base64Variant base, String name, boolean usesPadding, char paddingChar, PaddingReadBehaviour paddingReadBehaviour, int maxLineLength) | ||
{ | ||
_name = name; | ||
byte[] srcB = base._base64ToAsciiB; | ||
|
@@ -166,6 +182,35 @@ public Base64Variant(Base64Variant base, String name, boolean usesPadding, char | |
_usesPadding = usesPadding; | ||
_paddingChar = paddingChar; | ||
_maxLineLength = maxLineLength; | ||
this._paddingReadBehaviour = paddingReadBehaviour; | ||
} | ||
|
||
private Base64Variant(Base64Variant base, PaddingReadBehaviour paddingReadBehaviour) { | ||
this(base, base._name, base._usesPadding, base._paddingChar, paddingReadBehaviour, base._maxLineLength); | ||
} | ||
|
||
public Base64Variant withPaddingAllowed() { | ||
return new Base64Variant(this, PaddingReadBehaviour.PADDING_ALLOWED); | ||
} | ||
|
||
public Base64Variant withPaddingRequired() { | ||
return new Base64Variant(this, PaddingReadBehaviour.PADDING_REQUIRED); | ||
} | ||
|
||
public Base64Variant withPaddingForbidden() { | ||
return new Base64Variant(this, PaddingReadBehaviour.PADDING_FORBIDDEN); | ||
} | ||
|
||
public Base64Variant withWritePadding(boolean writePadding) { | ||
return new Base64Variant(this, this._name, writePadding, this._paddingChar, this._maxLineLength); | ||
|
||
} | ||
|
||
private enum PaddingReadBehaviour { | ||
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. Since exposed by public accessor, needs to be public type (I can change this post-merge). 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. Changed and added comments and |
||
PADDING_FORBIDDEN, | ||
PADDING_REQUIRED, | ||
PADDING_ALLOWED | ||
; | ||
} | ||
|
||
/* | ||
|
@@ -193,6 +238,7 @@ protected Object readResolve() { | |
public boolean usesPadding() { return _usesPadding; } | ||
public boolean usesPaddingChar(char c) { return c == _paddingChar; } | ||
public boolean usesPaddingChar(int ch) { return ch == (int) _paddingChar; } | ||
public PaddingReadBehaviour paddingReadBehaviour() { return _paddingReadBehaviour; } | ||
public char getPaddingChar() { return _paddingChar; } | ||
public byte getPaddingByte() { return (byte)_paddingChar; } | ||
|
||
|
@@ -275,7 +321,7 @@ public int encodeBase64Partial(int bits, int outputBytes, char[] buffer, int out | |
{ | ||
buffer[outPtr++] = _base64ToAsciiC[(bits >> 18) & 0x3F]; | ||
buffer[outPtr++] = _base64ToAsciiC[(bits >> 12) & 0x3F]; | ||
if (_usesPadding) { | ||
if (usesPadding()) { | ||
buffer[outPtr++] = (outputBytes == 2) ? | ||
_base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar; | ||
buffer[outPtr++] = _paddingChar; | ||
|
@@ -291,7 +337,7 @@ public void encodeBase64Partial(StringBuilder sb, int bits, int outputBytes) | |
{ | ||
sb.append(_base64ToAsciiC[(bits >> 18) & 0x3F]); | ||
sb.append(_base64ToAsciiC[(bits >> 12) & 0x3F]); | ||
if (_usesPadding) { | ||
if (usesPadding()) { | ||
sb.append((outputBytes == 2) ? | ||
_base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar); | ||
sb.append(_paddingChar); | ||
|
@@ -333,7 +379,7 @@ public int encodeBase64Partial(int bits, int outputBytes, byte[] buffer, int out | |
{ | ||
buffer[outPtr++] = _base64ToAsciiB[(bits >> 18) & 0x3F]; | ||
buffer[outPtr++] = _base64ToAsciiB[(bits >> 12) & 0x3F]; | ||
if (_usesPadding) { | ||
if (usesPadding()) { | ||
byte pb = (byte) _paddingChar; | ||
buffer[outPtr++] = (outputBytes == 2) ? | ||
_base64ToAsciiB[(bits >> 6) & 0x3F] : pb; | ||
|
@@ -529,8 +575,8 @@ public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentE | |
decodedData = (decodedData << 6) | bits; | ||
// third base64 char; can be padding, but not ws | ||
if (ptr >= len) { | ||
// but as per [JACKSON-631] can be end-of-input, iff not using padding | ||
if (!usesPadding()) { | ||
// but as per [JACKSON-631] can be end-of-input, iff padding is not required | ||
if (!paddingReadBehaviour().equals(PaddingReadBehaviour.PADDING_REQUIRED)) { | ||
decodedData >>= 4; | ||
builder.append(decodedData); | ||
break; | ||
|
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.
Let's remove
transient
here -- not sure why preceding final field has it, possibly copy-paste issue originally (I will remove it from others too)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.
Removed transient