From 8bac901b0ff0a20a92f2b0d57b05fb141efedefc Mon Sep 17 00:00:00 2001 From: James Yuzawa Date: Thu, 25 Jul 2024 23:10:33 -0400 Subject: [PATCH] zero copy split --- .../java/com/iab/gpp/encoder/GppModel.java | 7 +-- .../base64/AbstractBase64UrlEncoder.java | 2 +- ...actEncodableSegmentedBitStringSection.java | 2 +- .../AbstractLazilyEncodableSection.java | 10 ++-- .../gpp/encoder/section/EncodableSection.java | 2 +- .../com/iab/gpp/encoder/section/HeaderV1.java | 14 ++--- .../encoder/section/SlicedCharSequence.java | 52 +++++++++++++++++++ .../com/iab/gpp/encoder/section/TcfCaV1.java | 20 +++---- .../com/iab/gpp/encoder/section/TcfEuV2.java | 22 ++++---- .../com/iab/gpp/encoder/section/UsCaV1.java | 16 +++--- .../com/iab/gpp/encoder/section/UsCoV1.java | 16 +++--- .../com/iab/gpp/encoder/section/UsCtV1.java | 16 +++--- .../com/iab/gpp/encoder/section/UsNatV1.java | 16 +++--- .../com/iab/gpp/encoder/section/UsUtV1.java | 14 ++--- .../com/iab/gpp/encoder/section/UsVaV1.java | 12 ++--- .../com/iab/gpp/encoder/section/UspV1.java | 14 ++--- .../AbstractLazilyEncodableSegment.java | 10 ++-- .../gpp/encoder/segment/EncodableSegment.java | 2 +- .../encoder/segment/HeaderV1CoreSegment.java | 4 +- .../encoder/segment/TcfCaV1CoreSegment.java | 4 +- .../TcfCaV1DisclosedVendorsSegment.java | 4 +- .../TcfCaV1PublisherPurposesSegment.java | 4 +- .../encoder/segment/TcfEuV2CoreSegment.java | 4 +- .../TcfEuV2PublisherPurposesSegment.java | 4 +- .../segment/TcfEuV2VendorsAllowedSegment.java | 4 +- .../TcfEuV2VendorsDisclosedSegment.java | 4 +- .../encoder/segment/UsCaV1CoreSegment.java | 4 +- .../gpp/encoder/segment/UsCaV1GpcSegment.java | 4 +- .../encoder/segment/UsCoV1CoreSegment.java | 4 +- .../gpp/encoder/segment/UsCoV1GpcSegment.java | 4 +- .../encoder/segment/UsCtV1CoreSegment.java | 4 +- .../gpp/encoder/segment/UsCtV1GpcSegment.java | 4 +- .../encoder/segment/UsNatV1CoreSegment.java | 4 +- .../encoder/segment/UsNatV1GpcSegment.java | 4 +- .../encoder/segment/UsUtV1CoreSegment.java | 4 +- .../encoder/segment/UsVaV1CoreSegment.java | 4 +- .../gpp/encoder/segment/UspV1CoreSegment.java | 4 +- 37 files changed, 188 insertions(+), 135 deletions(-) create mode 100644 iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/SlicedCharSequence.java diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/GppModel.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/GppModel.java index efc57ac..fbd7ac1 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/GppModel.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/GppModel.java @@ -13,6 +13,7 @@ import com.iab.gpp.encoder.section.EncodableSection; import com.iab.gpp.encoder.section.HeaderV1; import com.iab.gpp.encoder.section.Sections; +import com.iab.gpp.encoder.section.SlicedCharSequence; import com.iab.gpp.encoder.section.TcfCaV1; import com.iab.gpp.encoder.section.TcfEuV2; import com.iab.gpp.encoder.section.UsCaV1; @@ -285,14 +286,14 @@ protected Map decodeModel(String str) { Map sections = new HashMap<>(); if(str != null && !str.isEmpty()) { - String[] encodedSections = str.split("~"); - HeaderV1 header = new HeaderV1(encodedSections[0]); + List encodedSections = SlicedCharSequence.split(str, '~'); + HeaderV1 header = new HeaderV1(encodedSections.get(0)); sections.put(HeaderV1.NAME, header); @SuppressWarnings("unchecked") List sectionIds = (List) header.getFieldValue("SectionIds"); for (int i = 0; i < sectionIds.size(); i++) { - String section = encodedSections[i + 1]; + CharSequence section = encodedSections.get(i + 1); switch (sectionIds.get(i)) { case TcfEuV2.ID: sections.put(TcfEuV2.NAME, new TcfEuV2(section)); diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/base64/AbstractBase64UrlEncoder.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/base64/AbstractBase64UrlEncoder.java index af0b214..c2f0083 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/base64/AbstractBase64UrlEncoder.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/base64/AbstractBase64UrlEncoder.java @@ -44,7 +44,7 @@ public String encode(String bitString) { return str.toString(); } - public BitString decode(String str) { + public BitString decode(CharSequence str) { int length = str.length(); BitStringBuilder sb = new BitStringBuilder(length * BASE64_BITS); for (int i = 0; i < length; i++) { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractEncodableSegmentedBitStringSection.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractEncodableSegmentedBitStringSection.java index 9ed0337..984538f 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractEncodableSegmentedBitStringSection.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractEncodableSegmentedBitStringSection.java @@ -105,7 +105,7 @@ private void decodeSegmentFromBitString(String[] segment, BitString segmentBitSt public abstract String encode() throws EncodingException; @Override - public abstract void decode(String encodedString) throws DecodingException; + public abstract void decode(CharSequence encodedString) throws DecodingException; @Override public abstract int getId(); diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractLazilyEncodableSection.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractLazilyEncodableSection.java index 5e97e14..3faeacb 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractLazilyEncodableSection.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/AbstractLazilyEncodableSection.java @@ -8,7 +8,7 @@ public abstract class AbstractLazilyEncodableSection implements EncodableSection private List segments; - private String encodedString = null; + private CharSequence encodedString = null; private boolean dirty = false; private boolean decoded = true; @@ -21,7 +21,7 @@ public AbstractLazilyEncodableSection() { protected abstract String encodeSection(List segments); - protected abstract List decodeSection(String encodedString); + protected abstract List decodeSection(CharSequence encodedString); public boolean hasField(String fieldName) { if (!this.decoded) { @@ -73,16 +73,16 @@ public void setFieldValue(String fieldName, Object value) { } public String encode() { - if (this.encodedString == null || this.encodedString.isEmpty() || this.dirty) { + if (this.encodedString == null || this.encodedString.length() == 0 || this.dirty) { this.encodedString = this.encodeSection(this.segments); this.dirty = false; this.decoded = true; } - return this.encodedString; + return this.encodedString.toString(); } - public void decode(String encodedString) { + public void decode(CharSequence encodedString) { this.encodedString = encodedString; this.dirty = false; this.decoded = false; diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/EncodableSection.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/EncodableSection.java index e1f0a85..3f4caad 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/EncodableSection.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/EncodableSection.java @@ -16,5 +16,5 @@ public interface EncodableSection { String encode(); - void decode(String encodedString); + void decode(CharSequence encodedString); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/HeaderV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/HeaderV1.java index c4e1855..8adc459 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/HeaderV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/HeaderV1.java @@ -17,7 +17,7 @@ public HeaderV1() { super(); } - public HeaderV1(String encodedString) { + public HeaderV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -43,15 +43,15 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if(encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - for(int i=0; i i) { - segments.get(i).decode(encodedSegments[i]); + for (int i=0; i i) { + segments.get(i).decode(encodedSegments.get(i)); } } } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/SlicedCharSequence.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/SlicedCharSequence.java new file mode 100644 index 0000000..0842b86 --- /dev/null +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/SlicedCharSequence.java @@ -0,0 +1,52 @@ +package com.iab.gpp.encoder.section; + +import java.util.ArrayList; +import java.util.List; + +public final class SlicedCharSequence implements CharSequence { + + private final CharSequence base; + private final int start; + private final int end; + + private SlicedCharSequence(CharSequence base, int start, int end) { + this.base = base; + this.start = start; + this.end = end; + } + + public static List split(CharSequence charSequence, char splitter) { + List out = new ArrayList<>(1); + int length = charSequence.length(); + int start = 0; + for (int i = 0; i < length; i++) { + if (charSequence.charAt(i) == splitter) { + out.add(new SlicedCharSequence(charSequence, start, i)); + start = i + 1; + } + } + out.add(new SlicedCharSequence(charSequence, start, length)); + return out; + } + + @Override + public int length() { + return end - start; + } + + @Override + public char charAt(int index) { + return base.charAt(start + index); + } + + @Override + public CharSequence subSequence(int newStart, int newEnd) { + return base.subSequence(start + newStart, start + newEnd); + } + + @Override + public String toString() { + return base.subSequence(start, end).toString(); + } + +} diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfCaV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfCaV1.java index add7ab2..c3e7ba4 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfCaV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfCaV1.java @@ -24,7 +24,7 @@ public TcfCaV1() { super(); } - public TcfCaV1(String encodedString) { + public TcfCaV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -50,12 +50,12 @@ protected List initializeSegments() { } @Override - public List decodeSection(String encodedString) { + public List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); - for (int i = 0; i < encodedSegments.length; i++) { + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); + for (int i = 0; i < encodedSegments.size(); i++) { /** * The first 3 bits contain the segment id. Rather than decode the entire string, just check the first character. @@ -68,16 +68,16 @@ public List decodeSection(String encodedString) { * for the encoding version which only coincidentally works here because the version value is less than 8. */ - String encodedSegment = encodedSegments[i]; - if(!encodedSegment.isEmpty()) { + CharSequence encodedSegment = encodedSegments.get(i); + if (encodedSegment.length() > 0) { char firstChar = encodedSegment.charAt(0); if(firstChar >= 'A' && firstChar <= 'H') { - segments.get(0).decode(encodedSegments[i]); + segments.get(0).decode(encodedSegment); } else if(firstChar >= 'I' && firstChar <= 'P') { - segments.get(2).decode(encodedSegments[i]); + segments.get(2).decode(encodedSegment); } else if((firstChar >= 'Y' && firstChar <= 'Z') || (firstChar >= 'a' && firstChar <= 'f')) { - segments.get(1).decode(encodedSegments[i]); + segments.get(1).decode(encodedSegment); } else { throw new DecodingException("Invalid segment '" + encodedSegment + "'"); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfEuV2.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfEuV2.java index c2bf402..f0dd66e 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfEuV2.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/TcfEuV2.java @@ -25,7 +25,7 @@ public TcfEuV2() { super(); } - public TcfEuV2(String encodedString) { + public TcfEuV2(CharSequence encodedString) { super(); decode(encodedString); } @@ -51,12 +51,12 @@ protected List initializeSegments() { } @Override - public List decodeSection(String encodedString) { + public List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); - for (int i = 0; i < encodedSegments.length; i++) { + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); + for (int i = 0; i < encodedSegments.size(); i++) { /** * The first 3 bits contain the segment id. Rather than decode the entire string, just check the first character. @@ -70,19 +70,19 @@ public List decodeSection(String encodedString) { * for the encoding version which only coincidentally works here because the version value is less than 8. */ - String encodedSegment = encodedSegments[i]; - if(!encodedSegment.isEmpty()) { + CharSequence encodedSegment = encodedSegments.get(i); + if (encodedSegment.length() > 0) { char firstChar = encodedSegment.charAt(0); // unfortunately, the segment ordering doesn't match the segment ids if(firstChar >= 'A' && firstChar <= 'H') { - segments.get(0).decode(encodedSegments[i]); + segments.get(0).decode(encodedSegment); } else if(firstChar >= 'I' && firstChar <= 'P') { - segments.get(3).decode(encodedSegments[i]); + segments.get(3).decode(encodedSegment); } else if(firstChar >= 'Q' && firstChar <= 'X') { - segments.get(2).decode(encodedSegments[i]); + segments.get(2).decode(encodedSegment); } else if((firstChar >= 'Y' && firstChar <= 'Z') || (firstChar >= 'a' && firstChar <= 'f')) { - segments.get(1).decode(encodedSegments[i]); + segments.get(1).decode(encodedSegment); } else { throw new DecodingException("Invalid segment '" + encodedSegment + "'"); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCaV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCaV1.java index 98e98f4..3b49643 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCaV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCaV1.java @@ -18,7 +18,7 @@ public UsCaV1() { super(); } - public UsCaV1(String encodedString) { + public UsCaV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -44,19 +44,19 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - if(encodedSegments.length > 0) { - segments.get(0).decode(encodedSegments[0]); + if (encodedSegments.size() > 0) { + segments.get(0).decode(encodedSegments.get(0)); } - if(encodedSegments.length > 1) { + if (encodedSegments.size() > 1) { segments.get(1).setFieldValue(UsCaV1Field.GPC_SEGMENT_INCLUDED, true); - segments.get(1).decode(encodedSegments[1]); + segments.get(1).decode(encodedSegments.get(1)); } else { segments.get(1).setFieldValue(UsCaV1Field.GPC_SEGMENT_INCLUDED, false); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCoV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCoV1.java index cd17b02..e22547d 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCoV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCoV1.java @@ -18,7 +18,7 @@ public UsCoV1() { super(); } - public UsCoV1(String encodedString) { + public UsCoV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -44,19 +44,19 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - if(encodedSegments.length > 0) { - segments.get(0).decode(encodedSegments[0]); + if (encodedSegments.size() > 0) { + segments.get(0).decode(encodedSegments.get(0)); } - if(encodedSegments.length > 1) { + if (encodedSegments.size() > 1) { segments.get(1).setFieldValue(UsCoV1Field.GPC_SEGMENT_INCLUDED, true); - segments.get(1).decode(encodedSegments[1]); + segments.get(1).decode(encodedSegments.get(1)); } else { segments.get(1).setFieldValue(UsCoV1Field.GPC_SEGMENT_INCLUDED, false); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCtV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCtV1.java index 9558b76..3d3f0b8 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCtV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsCtV1.java @@ -18,7 +18,7 @@ public UsCtV1() { super(); } - public UsCtV1(String encodedString) { + public UsCtV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -44,19 +44,19 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - if(encodedSegments.length > 0) { - segments.get(0).decode(encodedSegments[0]); + if (encodedSegments.size() > 0) { + segments.get(0).decode(encodedSegments.get(0)); } - if(encodedSegments.length > 1) { + if (encodedSegments.size() > 1) { segments.get(1).setFieldValue(UsCtV1Field.GPC_SEGMENT_INCLUDED, true); - segments.get(1).decode(encodedSegments[1]); + segments.get(1).decode(encodedSegments.get(1)); } else { segments.get(1).setFieldValue(UsCtV1Field.GPC_SEGMENT_INCLUDED, false); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsNatV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsNatV1.java index bf1bc67..3d955cb 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsNatV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsNatV1.java @@ -18,7 +18,7 @@ public UsNatV1() { super(); } - public UsNatV1(String encodedString) { + public UsNatV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -44,19 +44,19 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - if(encodedSegments.length > 0) { - segments.get(0).decode(encodedSegments[0]); + if (encodedSegments.size() > 0) { + segments.get(0).decode(encodedSegments.get(0)); } - if(encodedSegments.length > 1) { + if (encodedSegments.size() > 1) { segments.get(1).setFieldValue(UsNatV1Field.GPC_SEGMENT_INCLUDED, true); - segments.get(1).decode(encodedSegments[1]); + segments.get(1).decode(encodedSegments.get(1)); } else { segments.get(1).setFieldValue(UsNatV1Field.GPC_SEGMENT_INCLUDED, false); } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsUtV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsUtV1.java index 905d96a..27bae1a 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsUtV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsUtV1.java @@ -17,7 +17,7 @@ public UsUtV1() { super(); } - public UsUtV1(String encodedString) { + public UsUtV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -43,15 +43,15 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - for(int i=0; i i) { - segments.get(i).decode(encodedSegments[i]); + for (int i = 0; i < segments.size(); i++) { + if (encodedSegments.size() > i) { + segments.get(i).decode(encodedSegments.get(i)); } } } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsVaV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsVaV1.java index a543a4f..20b7bc2 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsVaV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UsVaV1.java @@ -17,7 +17,7 @@ public UsVaV1() { super(); } - public UsVaV1(String encodedString) { + public UsVaV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -43,15 +43,15 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); for (int i = 0; i < segments.size(); i++) { - if (encodedSegments.length > i) { - segments.get(i).decode(encodedSegments[i]); + if (encodedSegments.size() > i) { + segments.get(i).decode(encodedSegments.get(i)); } } } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UspV1.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UspV1.java index 52a2124..eb7d8eb 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UspV1.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/section/UspV1.java @@ -17,7 +17,7 @@ public UspV1() { super(); } - public UspV1(String encodedString) { + public UspV1(CharSequence encodedString) { super(); decode(encodedString); } @@ -43,15 +43,15 @@ protected List initializeSegments() { } @Override - protected List decodeSection(String encodedString) { + protected List decodeSection(CharSequence encodedString) { List segments = initializeSegments(); - if(encodedString != null && !encodedString.isEmpty()) { - String[] encodedSegments = encodedString.split("\\."); + if (encodedString != null && encodedString.length() > 0) { + List encodedSegments = SlicedCharSequence.split(encodedString, '.'); - for(int i=0; i i) { - segments.get(i).decode(encodedSegments[i]); + for (int i=0; i < segments.size(); i++) { + if (encodedSegments.size() > i) { + segments.get(i).decode(encodedSegments.get(i)); } } } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/AbstractLazilyEncodableSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/AbstractLazilyEncodableSegment.java index ed3af42..efa895d 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/AbstractLazilyEncodableSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/AbstractLazilyEncodableSegment.java @@ -8,7 +8,7 @@ public abstract class AbstractLazilyEncodableSegment> implem protected T fields; - private String encodedString = null; + private CharSequence encodedString = null; private boolean dirty = false; private boolean decoded = true; @@ -21,7 +21,7 @@ public AbstractLazilyEncodableSegment() { protected abstract String encodeSegment(T fields); - protected abstract void decodeSegment(String encodedString, T Fields); + protected abstract void decodeSegment(CharSequence encodedString, T Fields); public boolean hasField(String fieldName) { return this.fields.containsKey(fieldName); @@ -59,17 +59,17 @@ public void setFieldValue(String fieldName, Object value) { } public String encode() { - if (this.encodedString == null || this.encodedString.isEmpty() || this.dirty) { + if (this.encodedString == null || this.encodedString.length() == 0 || this.dirty) { this.validate(); this.encodedString = encodeSegment(this.fields); this.dirty = false; this.decoded = true; } - return this.encodedString; + return this.encodedString.toString(); } - public void decode(String encodedString) { + public void decode(CharSequence encodedString) { this.encodedString = encodedString; this.dirty = false; this.decoded = false; diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/EncodableSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/EncodableSegment.java index d2363a8..f4b4a35 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/EncodableSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/EncodableSegment.java @@ -14,7 +14,7 @@ public interface EncodableSegment { String encode(); - void decode(String encodedString); + void decode(CharSequence encodedString); default void validate() {}; } diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/HeaderV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/HeaderV1CoreSegment.java index 29b88d2..5b7ed00 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/HeaderV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/HeaderV1CoreSegment.java @@ -49,8 +49,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1CoreSegment.java index aef3236..ab1a160 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1CoreSegment.java @@ -70,8 +70,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1DisclosedVendorsSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1DisclosedVendorsSegment.java index 2a04052..0f7c6f3 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1DisclosedVendorsSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1DisclosedVendorsSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1PublisherPurposesSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1PublisherPurposesSegment.java index 7a9cc0b..2fe380c 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1PublisherPurposesSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfCaV1PublisherPurposesSegment.java @@ -68,8 +68,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2CoreSegment.java index 2c6a147..69e1561 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2CoreSegment.java @@ -74,8 +74,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2PublisherPurposesSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2PublisherPurposesSegment.java index cf09ab5..01b2551 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2PublisherPurposesSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2PublisherPurposesSegment.java @@ -68,8 +68,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsAllowedSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsAllowedSegment.java index cbdce7c..3dc7ac7 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsAllowedSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsAllowedSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsDisclosedSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsDisclosedSegment.java index b76bf89..ef98046 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsDisclosedSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/TcfEuV2VendorsDisclosedSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1CoreSegment.java index 0c2c4a1..917f59b 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1CoreSegment.java @@ -83,8 +83,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1GpcSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1GpcSegment.java index 78843de..416d301 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1GpcSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCaV1GpcSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1CoreSegment.java index 01766c6..185274b 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1CoreSegment.java @@ -81,8 +81,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1GpcSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1GpcSegment.java index b93a9ce..ff5e3a3 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1GpcSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCoV1GpcSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1CoreSegment.java index 6e9688a..fd92597 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1CoreSegment.java @@ -81,8 +81,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1GpcSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1GpcSegment.java index fdd8891..4ca83b7 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1GpcSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsCtV1GpcSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1CoreSegment.java index 777e2e3..d16774a 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1CoreSegment.java @@ -91,8 +91,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1GpcSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1GpcSegment.java index e3b0a08..06a0c00 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1GpcSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsNatV1GpcSegment.java @@ -47,8 +47,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if(encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if(encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsUtV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsUtV1CoreSegment.java index cc926da..3000f9b 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsUtV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsUtV1CoreSegment.java @@ -83,8 +83,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsVaV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsVaV1CoreSegment.java index 5d00f87..9cffe4c 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsVaV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UsVaV1CoreSegment.java @@ -81,8 +81,8 @@ protected String encodeSegment(EncodableBitStringFields fields) { } @Override - protected void decodeSegment(String encodedString, EncodableBitStringFields fields) { - if (encodedString == null || encodedString.isEmpty()) { + protected void decodeSegment(CharSequence encodedString, EncodableBitStringFields fields) { + if (encodedString == null || encodedString.length() == 0) { this.fields.reset(fields); } try { diff --git a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UspV1CoreSegment.java b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UspV1CoreSegment.java index 2513615..68f354d 100644 --- a/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UspV1CoreSegment.java +++ b/iabgpp-encoder/src/main/java/com/iab/gpp/encoder/segment/UspV1CoreSegment.java @@ -45,13 +45,13 @@ protected String encodeSegment(GenericFields fields) { } @Override - protected void decodeSegment(String encodedString, GenericFields fields) { + protected void decodeSegment(CharSequence encodedString, GenericFields fields) { if (encodedString == null || encodedString.length() != 4) { throw new DecodingException("Invalid uspv1 string: '" + encodedString + "'"); } try { - fields.get(UspV1Field.VERSION).setValue(Integer.parseInt(encodedString.substring(0, 1))); + fields.get(UspV1Field.VERSION).setValue((int)(encodedString.charAt(0) - '0')); fields.get(UspV1Field.NOTICE).setValue(encodedString.charAt(1)); fields.get(UspV1Field.OPT_OUT_SALE).setValue(encodedString.charAt(2)); fields.get(UspV1Field.LSPA_COVERED).setValue(encodedString.charAt(3));