1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.calculator2; 18 19 import java.io.InputStream; 20 import java.io.OutputStream; 21 import java.io.IOException; 22 import java.io.FileNotFoundException; 23 import java.io.BufferedInputStream; 24 import java.io.BufferedOutputStream; 25 import java.io.DataInputStream; 26 import java.io.DataOutputStream; 27 28 import android.content.Context; 29 30 class Persist { 31 private static final int LAST_VERSION = 1; 32 private static final String FILE_NAME = "calculator.data"; 33 private Context mContext; 34 35 History history = new History(); 36 37 Persist(Context context) { 38 this.mContext = context; 39 load(); 40 } 41 42 private void load() { 43 try { 44 InputStream is = new BufferedInputStream(mContext.openFileInput(FILE_NAME), 8192); 45 DataInputStream in = new DataInputStream(is); 46 int version = in.readInt(); 47 if (version > LAST_VERSION) { 48 throw new IOException("data version " + version + "; expected " + LAST_VERSION); 49 } 50 history = new History(version, in); 51 in.close(); 52 } catch (FileNotFoundException e) { 53 Calculator.log("" + e); 54 } catch (IOException e) { 55 Calculator.log("" + e); 56 } 57 } 58 59 void save() { 60 try { 61 OutputStream os = new BufferedOutputStream(mContext.openFileOutput(FILE_NAME, 0), 8192); 62 DataOutputStream out = new DataOutputStream(os); 63 out.writeInt(LAST_VERSION); 64 history.write(out); 65 out.close(); 66 } catch (IOException e) { 67 Calculator.log("" + e); 68 } 69 } 70 } 71