Improving reading from stream in serialization

This commit is contained in:
marcin.sciesinski
2021-03-19 08:11:08 -07:00
parent 9b9f7cfacc
commit 87916afc83

View File

@@ -137,7 +137,7 @@ public class CollectionSerializer {
}
Map<String, Object> extraInformation = collection.getExtraInformation();
JSONObject json= new JSONObject();
JSONObject json = new JSONObject();
json.putAll(extraInformation);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
@@ -168,7 +168,7 @@ public class CollectionSerializer {
DefaultCardCollection collection = new DefaultCardCollection();
byte[] packs = new byte[packTypes];
int read = inputStream.read(packs);
int read = readWholeArray(inputStream, packs);
if (read != packTypes)
throw new IllegalStateException("Under-read the packs information");
for (int i = 0; i < packs.length; i++)
@@ -177,7 +177,7 @@ public class CollectionSerializer {
int cardBytes = convertToInt(inputStream.read(), inputStream.read());
byte[] cards = new byte[cardBytes];
read = inputStream.read(cards);
read = readWholeArray(inputStream, cards);
if (read != cardBytes)
throw new IllegalArgumentException("Under-read the cards information");
for (int i = 0; i < cards.length; i++)
@@ -202,7 +202,7 @@ public class CollectionSerializer {
collection.addCurrency(currency);
byte[] packs = new byte[packTypes];
int read = inputStream.read(packs);
int read = readWholeArray(inputStream, packs);
if (read != packTypes)
throw new IllegalStateException("Under-read the packs information");
for (int i = 0; i < packs.length; i++)
@@ -211,7 +211,7 @@ public class CollectionSerializer {
int cardBytes = convertToInt(inputStream.read(), inputStream.read());
byte[] cards = new byte[cardBytes];
read = inputStream.read(cards);
read = readWholeArray(inputStream, cards);
if (read != cardBytes)
throw new IllegalArgumentException("Under-read the cards information");
for (int i = 0; i < cards.length; i++)
@@ -237,7 +237,7 @@ public class CollectionSerializer {
byte[] packs = new byte[packTypes * 2];
int read = inputStream.read(packs);
int read = readWholeArray(inputStream, packs);
if (read != packTypes * 2)
throw new IllegalStateException("Under-read the packs information");
for (int i = 0; i < packTypes; i++) {
@@ -248,7 +248,7 @@ public class CollectionSerializer {
int cardBytes = convertToInt(inputStream.read(), inputStream.read());
byte[] cards = new byte[cardBytes];
read = inputStream.read(cards);
read = readWholeArray(inputStream, cards);
if (read != cardBytes)
throw new IllegalArgumentException("Under-read the cards information");
for (int i = 0; i < cards.length; i++)
@@ -273,7 +273,7 @@ public class CollectionSerializer {
byte[] packs = new byte[packTypes * 2];
int read = inputStream.read(packs);
int read = readWholeArray(inputStream, packs);
if (read != packTypes * 2)
throw new IllegalStateException("Under-read the packs information");
for (int i = 0; i < packTypes; i++) {
@@ -284,7 +284,7 @@ public class CollectionSerializer {
int cardBytes = convertToInt(inputStream.read(), inputStream.read());
byte[] cards = new byte[cardBytes];
read = inputStream.read(cards);
read = readWholeArray(inputStream, cards);
if (read != cardBytes)
throw new IllegalArgumentException("Under-read the cards information");
for (int i = 0; i < cards.length; i++) {
@@ -311,7 +311,7 @@ public class CollectionSerializer {
byte[] packs = new byte[packTypes * 2];
int read = inputStream.read(packs);
int read = readWholeArray(inputStream, packs);
if (read != packTypes * 2)
throw new IllegalStateException("Under-read the packs information");
for (int i = 0; i < packTypes; i++) {
@@ -322,7 +322,7 @@ public class CollectionSerializer {
int cardBytes = convertToInt(inputStream.read(), inputStream.read());
byte[] cards = new byte[cardBytes];
read = inputStream.read(cards);
read = readWholeArray(inputStream, cards);
if (read != cardBytes)
throw new IllegalArgumentException("Under-read the cards information");
for (int i = 0; i < cards.length; i++) {
@@ -361,4 +361,16 @@ public class CollectionSerializer {
for (int i = 0; i < byteCount; i++)
outputStream.write((value >> (8 * (byteCount - i - 1))) & 0x000000ff);
}
private static int readWholeArray(InputStream stream, byte[] array) throws IOException {
int readCount = 0;
while (true) {
int readAmount = stream.read(array, readCount, array.length - readCount);
if (readAmount < 0)
return readCount;
readCount += readAmount;
if (readCount == array.length)
return readCount;
}
}
}