[OLINGO-284] Fixed content length calculation

This commit is contained in:
Michael Bolz 2014-05-26 10:26:00 +02:00
parent 5b5ff8ea23
commit b913717758
2 changed files with 19 additions and 5 deletions

View File

@ -53,11 +53,23 @@ public class EdmBinary extends SingletonPrimitiveType {
}
private static boolean validateMaxLength(final String value, final Integer maxLength) {
return maxLength == null ? true
: // Every three bytes are represented as four base-64 characters.
// Additionally, there could be up to two padding "=" characters
// if the number of bytes is not a multiple of three.
maxLength >= value.length() * 3 / 4 - (value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0);
return maxLength == null ? true :
// Every three bytes are represented as four base-64 characters.
// Additionally, there could be up to two padding "=" characters
// if the number of bytes is not a multiple of three,
// and there could be line feeds, possibly with carriage returns.
maxLength >= (value.length() - lineEndingsLength(value)) * 3 / 4
- (value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0);
}
private static int lineEndingsLength(final String value) {
int result = 0;
int index = 0;
while ((index = value.indexOf('\n', index)) >= 0) {
result += index > 0 && value.charAt(index - 1) == '\r' ? 2 : 1;
index++;
}
return result;
}
@Override

View File

@ -97,6 +97,8 @@ public class EdmBinaryTest extends PrimitiveTypeBaseTest {
byte[].class)));
assertTrue(Arrays.equals(binary, instance.valueOfString("qrvM3e7_", null, Integer.MAX_VALUE, null, null, null,
byte[].class)));
assertTrue(Arrays.equals(binary, instance.valueOfString("\nqrvM\n3e7_\r\n", null, 6, null, null, null,
byte[].class)));
expectFacetsErrorInValueOfString(instance, "qrvM3e7_", null, 3, null, null, null);
expectContentErrorInValueOfString(instance, "@");