use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.
the class CompressionUtils method decompressAndBase64Decode_old.
public static List<MantisServerSentEvent> decompressAndBase64Decode_old(String encodedString, boolean isCompressedBinary) {
encodedString = encodedString.trim();
// System.out.println("Inside client decompress Current thread -->" + Thread.currentThread().getName());
if (!encodedString.isEmpty() && isCompressedBinary && !encodedString.startsWith("ping") && !encodedString.startsWith("{")) {
if (logger.isDebugEnabled()) {
logger.debug("decoding " + encodedString);
}
byte[] decoded = Base64.getDecoder().decode(encodedString);
GZIPInputStream gis;
try {
gis = new GZIPInputStream(new ByteArrayInputStream(decoded));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String outStr = "";
String line;
while ((line = bf.readLine()) != null) {
outStr += line;
}
String[] toks = outStr.split("\\$\\$\\$");
List<MantisServerSentEvent> msseList = new ArrayList<>();
for (String tok : toks) {
msseList.add(new MantisServerSentEvent(tok));
}
// return Arrays.asList(new MantisServerSentEvent(toks));
return msseList;
} catch (IOException e) {
logger.error(e.getMessage());
}
return new ArrayList<MantisServerSentEvent>();
} else {
List<MantisServerSentEvent> s = new ArrayList<MantisServerSentEvent>();
s.add(new MantisServerSentEvent(encodedString));
return s;
}
}
use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.
the class CompressionUtils method tokenize_2.
static List<MantisServerSentEvent> tokenize_2(BufferedReader bf) throws IOException {
StringBuilder sb = new StringBuilder();
String line;
List<MantisServerSentEvent> msseList = new ArrayList<>();
String outStr = "";
while ((line = bf.readLine()) != null) {
sb.append(line);
}
outStr = sb.toString();
String[] toks = outStr.split("\\$\\$\\$");
for (String tok : toks) {
msseList.add(new MantisServerSentEvent(tok));
}
return msseList;
}
use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.
the class CompressionUtils method decompressAndBase64Decode.
public static List<MantisServerSentEvent> decompressAndBase64Decode(String encodedString, boolean isCompressedBinary, boolean useSnappy, String delimiter) {
encodedString = encodedString.trim();
// System.out.println("Inside client decompress Current thread -->" + Thread.currentThread().getName());
if (!encodedString.isEmpty() && isCompressedBinary && !encodedString.startsWith("ping") && !encodedString.startsWith("{")) {
if (logger.isDebugEnabled()) {
logger.debug("decoding " + encodedString);
}
byte[] decoded = Base64.getDecoder().decode(encodedString);
try {
if (useSnappy) {
return delimiter == null ? tokenize(snappyDecompress(decoded)) : tokenize(snappyDecompress(decoded), delimiter);
} else {
return delimiter == null ? tokenize(gzipDecompress(decoded)) : tokenize(gzipDecompress(decoded), delimiter);
}
} catch (IOException e) {
logger.error(e.getMessage());
}
return new ArrayList<MantisServerSentEvent>();
} else {
List<MantisServerSentEvent> s = new ArrayList<MantisServerSentEvent>();
s.add(new MantisServerSentEvent(encodedString));
return s;
}
}
use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.
the class CompressionUtils method tokenize.
static List<MantisServerSentEvent> tokenize(BufferedReader bf, String delimiter) throws IOException {
StringBuilder sb = new StringBuilder();
String line;
List<MantisServerSentEvent> msseList = new ArrayList<>();
final int delimiterLength = delimiter.length();
char[] delimiterArray = delimiter.toCharArray();
int delimiterCount = 0;
while ((line = bf.readLine()) != null) {
// Consider replacing this whole thing with just String.indexOf
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) != delimiterArray[delimiterCount]) {
if (delimiterCount > 0) {
for (int j = 0; j < delimiterCount; ++j) {
sb.append(delimiterArray[j]);
}
delimiterCount = 0;
}
if (line.charAt(i) != delimiterArray[delimiterCount]) {
sb.append(line.charAt(i));
} else {
delimiterCount++;
}
} else {
delimiterCount++;
}
if (delimiterCount == delimiterLength) {
msseList.add(new MantisServerSentEvent(sb.toString()));
delimiterCount = 0;
sb = new StringBuilder();
}
}
}
// We have a trailing event.
if (sb.length() > 0) {
// We had a partial delimiter match which was not in the builder.
if (delimiterCount > 0) {
for (int j = 0; j < delimiterCount; ++j) {
sb.append(delimiter.charAt(j));
}
}
msseList.add(new MantisServerSentEvent(sb.toString()));
}
return msseList;
}
use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.
the class CompressionUtilsTest method shouldTokenizeWithEventsContainingPartialDelimiterMatches.
@Test
public void shouldTokenizeWithEventsContainingPartialDelimiterMatches() {
String testInput = "ab$cdef$$$ghi$jkl$$$lmno$$pqrst$";
try (BufferedReader reader = new BufferedReader(new StringReader(testInput))) {
List<MantisServerSentEvent> result = CompressionUtils.tokenize(reader);
assertEquals(result.size(), 3);
assertEquals(result.get(0).getEventAsString(), "ab$cdef");
assertEquals(result.get(1).getEventAsString(), "ghi$jkl");
assertEquals(result.get(2).getEventAsString(), "lmno$$pqrst$");
} catch (IOException ex) {
Assert.fail("Tokenization threw an IO exception that was unexpected");
}
}
Aggregations