// TODO Der kommer en EOF exception når der forsøges at readObject. // TODO Brug sharedpref til at gemme og læse tamagotchi. private synchronized void loadTamagotchiFromFile() { FileInputStream fis = null; try { available.acquire(); Log.d(TAMAGOTCHI, "Loading the tamagotchi from: " + fileName); fis = this.openFileInput(fileName); ObjectInputStream is = new ObjectInputStream(fis); // is.reset(); // TODO Bug when reading a tama from filesystem // Steps to reproduce: Kill with the app context menu thingie, start app again. tama = (Tamagotchi) is.readObject(); if (tama == null) return; is.close(); fis.close(); initializePlayingfield(); Log.d(TAMAGOTCHI, "Done loading the tama"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (OptionalDataException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { available.release(); } }
// Helper function to load the day log from a file private boolean loadDayLog() { boolean res = false; String fullfilename = dayLogFileNamePrefix + dayLogFileNameSuffix; try { FileInputStream fis = this.openFileInput(fullfilename); ObjectInputStream is = new ObjectInputStream(fis); daylog = (DayLog) is.readObject(); is.close(); fis.close(); res = true; // Log.e("Main Load", "Existing log for this date found: " + fullfilename); } catch (ClassNotFoundException e) { e.printStackTrace(); this.deleteFile(dayLogFileNamePrefix + dayLogFileNameSuffix); Toast.makeText(MainActivity.this, "Broken file selected, deleting it...", Toast.LENGTH_LONG) .show(); } catch (OptionalDataException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e("MAIN", "Logload, No log for this date found " + fullfilename); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res; }
public static Results initResults() { Results results; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // TODO: MEDIA_READONLY // is exists folders File file = new File(Environment.getExternalStorageDirectory(), "morning-trainer"); if (!file.isDirectory()) Log.e( "mirinda1", file.delete() ? "delete" : "cannot delete file :" + file.getPath()); // TODO and what? if can't delete if (file.mkdir()) Log.i("mirinda1", "create directory: " + file.getName()); appPath = file.getPath(); file = new File(appPath, user); if (!file.isDirectory()) Log.e("mirinda1", file.delete() ? "delete" : "cannot delete file :" + file.getPath()); if (file.mkdir()) Log.i("mirinda1", "create directory: " + file.getName()); userPath = file.getPath(); String[] lst = file.list(); Log.i("mirinda1", Arrays.toString(lst)); // load data try { file = new File(userPath, SERIALIZED); if (!file.exists()) throw new IOException("not exist"); if (!file.isFile()) throw new IOException("not a file"); // TODO my exceptions FileInputStream inputStream = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); results = (Results) objectInputStream.readObject(); Log.i("mirinda1", results.toString()); } catch (ClassNotFoundException e) { e.printStackTrace(); results = null; } catch (OptionalDataException e) { e.printStackTrace(); results = null; } catch (FileNotFoundException e) { Log.e("mirinda1", "File not found"); results = null; } catch (StreamCorruptedException e) { e.printStackTrace(); results = null; } catch (IOException e) { Log.e("mirinda1", "" + e.getMessage()); results = new Results(user); } } else { Log.e("mirinda1", "SD card didn't mounted"); results = null; } return results; }
@SuppressWarnings("unchecked") private Object getObject(String key, String description, Activity callingActivity) { FileInputStream file = null; ObjectInputStream input = null; try { file = callingActivity.openFileInput(key); input = new ObjectInputStream(file); if (key == COUNT) { return (MyCountDownTimerWrapper) input.readObject(); } else if (key == LIST) { return (Stack<Exercise>) input.readObject(); } else if (key == STACK) { return (Stack<Exercise>) input.readObject(); } else if (key == KEYS) { return (ArrayList<String>) input.readObject(); } else { return (ArrayList<Exercise>) input.readObject(); } } catch (StreamCorruptedException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (OptionalDataException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (IOException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (ClassNotFoundException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } finally { try { if (file != null && input != null) { file.close(); input.close(); } } catch (IOException e) { Log.e( MainMenuActivity.LOG_TAG, "Could not close files while getting " + description + " from file"); e.printStackTrace(); } } return null; }
/** * Read a cache of OMGeometries, given a ObjectInputStream. * * @param objstream ObjectInputStream of geometry list. */ public void readGraphics(ObjectInputStream objstream) throws IOException { Debug.message("omgraphics", "OMGeometryList: Reading cached geometries"); try { while (true) { try { OMGeometry omg = (OMGeometry) objstream.readObject(); this.add(omg); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (OptionalDataException ode) { ode.printStackTrace(); } } } catch (EOFException e) { } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_workout); Workout loadedWorkout = null; try { FileInputStream fis = openFileInput("WorkoutPage"); ObjectInputStream is = new ObjectInputStream(fis); loadedWorkout = (Workout) is.readObject(); is.close(); fis.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (OptionalDataException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String s = loadedWorkout.getName(); setTitle(s); /* Intent intent = getIntent(); String message = intent.getStringExtra("asdf"); name = message; //setTitle(name); */ }
/** * Rebuilds the view state from the Base64 included String included with the request. * * @param stateString the Base64 encoded view state * @return the view state reconstructed from <code>stateString</code> */ protected Object doGetState(String stateString) { ObjectInputStream ois = null; InputStream bis = new Base64InputStream(stateString); try { if (guard != null) { byte[] bytes = stateString.getBytes(RIConstants.CHAR_ENCODING); int numRead = bis.read(bytes, 0, bytes.length); byte[] decodedBytes = new byte[numRead]; bis.reset(); bis.read(decodedBytes, 0, decodedBytes.length); bytes = guard.decrypt(decodedBytes); if (bytes == null) return null; bis = new ByteArrayInputStream(bytes); } if (compressViewState) { bis = new GZIPInputStream(bis); } ois = serialProvider.createObjectInputStream(bis); long stateTime = 0; if (stateTimeoutEnabled) { try { stateTime = ois.readLong(); } catch (IOException ioe) { // we've caught an exception trying to read the time // marker. This most likely means a view that has been // around before upgrading to the release that included // this feature. So, no marker, return null now to // cause a ViewExpiredException if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine( "Client state timeout is enabled, but unable to find the " + "time marker in the serialized state. Assuming state " + "to be old and returning null."); } return null; } } Object structure = ois.readObject(); Object state = ois.readObject(); if (stateTime != 0 && hasStateExpired(stateTime)) { // return null if state has expired. This should cause // a ViewExpiredException to be thrown return null; } return new Object[] {structure, state}; } catch (java.io.OptionalDataException ode) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, ode.getMessage(), ode); } throw new FacesException(ode); } catch (ClassNotFoundException cnfe) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, cnfe.getMessage(), cnfe); } throw new FacesException(cnfe); } catch (IOException iox) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, iox.getMessage(), iox); } throw new FacesException(iox); } finally { if (ois != null) { try { ois.close(); } catch (IOException ioe) { // ignore } } } }