--- android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/07/06 20:59:19 1545 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/07/06 21:20:49 1546 @@ -1,8 +1,11 @@ package dk.thoerup.traininfo.provider; +import java.io.EOFException; import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -25,37 +28,57 @@ public boolean loadStations(Context context) throws Exception { + stations.entries.clear(); //TODO: remove + File parent = context.getFilesDir(); - File stationsFile = new File(parent, "stations.xml"); + File stationsFile = new File(parent, "stations.bin"); if (!stationsFile.exists()) return false; int size = (int) stationsFile.length(); - byte data[] = new byte[size]; + /*byte data[] = new byte[size]; RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); raf.readFully(data); Serializer serializer = new Persister(); - stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); + stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/ + + + try { + ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); + Object o; + while ( (o=in.readObject()) != null ) { + stations.entries.add( (StationEntry) o); + } + in.close(); + } catch (EOFException e) { + //do nothing; + } Log.e("OFFLINE", "loaded" + stations.entries.size()); return true; } - public void downloadStations(Context context) throws IOException { + public void downloadStations(Context context) throws Exception { File parent = context.getFilesDir(); - File stationsFile = new File(parent, "stations.xml"); + File stationsFile = new File(parent, "stations.bin"); byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); + Serializer serializer = new Persister(); + stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); + - RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw"); + ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); Log.e("OFFLINE", "data size" + data.length); - raf.setLength(0); //truncate - raf.write(data); - raf.close(); + + for (StationEntry entry : stations.entries) { + out.writeObject(entry); + } + + out.close(); }