Change out all-in-one deserialization process with object-by-object method to save memory. Still not enough. Looks like I'll need to send each object right into the DB instead of into a list.
This commit is contained in:
@@ -6,9 +6,13 @@ import com.magnatune.eyecreate.companionformagnatune.model.xml.Track;
|
||||
import com.squareup.okhttp.ResponseBody;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.converters.basic.DateConverter;
|
||||
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
|
||||
import com.thoughtworks.xstream.io.xml.KXml2Driver;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.OptionalDataException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import retrofit.Converter;
|
||||
@@ -17,18 +21,26 @@ public class MagnatuneAlbumsConverter implements Converter<ResponseBody,AlbumRes
|
||||
@Override
|
||||
public AlbumResponse convert(ResponseBody value) throws IOException {
|
||||
GZIPInputStream compressedStream = new GZIPInputStream(value.byteStream());
|
||||
XStream parser = new XStream(new Xpp3Driver());
|
||||
XStream parser = new XStream(new KXml2Driver());
|
||||
parser.processAnnotations(Album.class);
|
||||
parser.processAnnotations(Track.class);
|
||||
parser.processAnnotations(AlbumResponse.class);
|
||||
parser.registerConverter(new DateConverter("yyyy-MM-dd",new String[]{}));
|
||||
parser.ignoreUnknownElements();
|
||||
AlbumResponse conversion = null;
|
||||
ObjectInputStream objectStream = parser.createObjectInputStream(compressedStream);
|
||||
boolean processingDone = false;
|
||||
List<Album> albums = new ArrayList<>();
|
||||
while(!processingDone) {
|
||||
try {
|
||||
conversion = (AlbumResponse) parser.fromXML(compressedStream);
|
||||
} catch (Exception e) {
|
||||
albums.add((Album) objectStream.readObject());
|
||||
} catch(OptionalDataException e) {
|
||||
processingDone = true;
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return conversion;
|
||||
}
|
||||
AlbumResponse response = new AlbumResponse();
|
||||
response.albums = albums;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user