/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1545 by torben, Wed Jul 6 20:59:19 2011 UTC revision 1546 by torben, Wed Jul 6 21:20:49 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3    import java.io.EOFException;
4  import java.io.File;  import java.io.File;
5  import java.io.IOException;  import java.io.FileInputStream;
6  import java.io.RandomAccessFile;  import java.io.FileOutputStream;
7    import java.io.ObjectInputStream;
8    import java.io.ObjectOutputStream;
9  import java.util.ArrayList;  import java.util.ArrayList;
10  import java.util.Collections;  import java.util.Collections;
11  import java.util.Comparator;  import java.util.Comparator;
# Line 25  public class OfflineStationProvider impl Line 28  public class OfflineStationProvider impl
28    
29    
30          public boolean loadStations(Context context) throws Exception {          public boolean loadStations(Context context) throws Exception {
31                    stations.entries.clear(); //TODO: remove
32                    
33                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
34                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
35                                    
36                  if (!stationsFile.exists())                  if (!stationsFile.exists())
37                          return false;                          return false;
38                                    
39                  int size = (int) stationsFile.length();                  int size = (int) stationsFile.length();
40                  byte data[] = new byte[size];                  /*byte data[] = new byte[size];
41                                    
42                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
43                  raf.readFully(data);                  raf.readFully(data);
44                                    
45                  Serializer serializer = new Persister();                  Serializer serializer = new Persister();
46                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/
47                    
48                    
49                    try {
50                            ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
51                            Object o;
52                            while ( (o=in.readObject()) != null ) {
53                                    stations.entries.add( (StationEntry) o);
54                            }
55                            in.close();
56                    } catch (EOFException e) {
57                            //do nothing;
58                    }
59                                    
60                  Log.e("OFFLINE", "loaded" + stations.entries.size());                  Log.e("OFFLINE", "loaded" + stations.entries.size());
61                                    
62                  return true;                  return true;
63          }                }      
64                    
65          public void downloadStations(Context context) throws IOException {          public void downloadStations(Context context) throws Exception {
66                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
67                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
68    
69                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
70                    Serializer serializer = new Persister();
71                    stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );
72                    
73                                    
74                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw");                  ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
75                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
76                  raf.setLength(0); //truncate                  
77                  raf.write(data);                  for (StationEntry entry : stations.entries) {
78                  raf.close();                                                      out.writeObject(entry);
79                    }
80                    
81                    out.close();            
82          }          }
83                    
84                    

Legend:
Removed from v.1545  
changed lines
  Added in v.1546

  ViewVC Help
Powered by ViewVC 1.1.20