· 7 years ago · Apr 24, 2018, 10:18 PM
1package crypto;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.ObjectInputStream;
8import java.io.ObjectOutputStream;
9import java.security.InvalidAlgorithmParameterException;
10import java.security.InvalidKeyException;
11import java.security.NoSuchAlgorithmException;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Optional;
18
19import javax.crypto.Cipher;
20import javax.crypto.CipherInputStream;
21import javax.crypto.CipherOutputStream;
22import javax.crypto.KeyGenerator;
23import javax.crypto.NoSuchPaddingException;
24import javax.crypto.SecretKey;
25import javax.crypto.spec.IvParameterSpec;
26
27import controller.io.Logger;
28
29/**
30 * It Is an utility class for storing and loading informations an a file.
31 *
32 */
33public final class SaveOntoFile {
34
35 private static final String NAME = ".conway";
36 private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
37 private static File f = new File(SaveOntoFile.NAME);
38 private static List<Integer> settings = new ArrayList<>();
39 private static Cipher cip;
40 private static SecretKey key;
41
42 private SaveOntoFile() {
43 }
44
45 /**
46 * Method which stores the progress made, for example the level reached.
47 *
48 * @param level
49 * the number representing the level reached
50 */
51 public static void saveProgress(final int level) {
52 SaveOntoFile.createfile();
53 final List<Integer> completeList = SaveOntoFile.loadList();
54 try (ObjectOutputStream oStream = new ObjectOutputStream(
55 // new CipherOutputStream(
56 new FileOutputStream(SaveOntoFile.f)/* , SaveOntoFile.cip) */)) {
57 completeList.set(0, level);
58 oStream.writeObject(completeList);
59 oStream.flush();
60 } catch (IOException e) {
61 Logger.logThrowable(e);
62 }
63 }
64
65 /**
66 * Method which gives reads from file the number representing the level reached.
67 *
68 * @return an Optional containing the level reached so far.
69 */
70 public static int loadProgress() {
71 final List<Integer> saved = SaveOntoFile.loadList();
72 return saved.get(0);
73 }
74
75 /**
76 * Method which stores the setting of the game on a file via ArrayList.
77 *
78 * @param toSave
79 * is the List of integers to be stored
80 */
81 public static void saveSettings(final List<Integer> toSave) {
82 SaveOntoFile.createfile();
83 final List<Integer> completeList = SaveOntoFile.loadList();
84 try (ObjectOutputStream oStream = new ObjectOutputStream(
85 // new CipherOutputStream(
86 new FileOutputStream(SaveOntoFile.f)/* , SaveOntoFile.cip) */)) {
87 final List<Integer> list = new LinkedList<>(completeList.subList(0, 1));
88 System.out.println(completeList);
89 list.addAll(toSave);
90 oStream.writeObject(list);
91 oStream.flush();
92 } catch (IOException e) {
93 Logger.logThrowable(e);
94 }
95 }
96
97 /**
98 * Method which loads the settings of the game already stored on the file.
99 *
100 * @return the Optional containing the list of integers describing the settings
101 */
102 public static List<Integer> loadSettings() {
103 final List<Integer> list = SaveOntoFile.loadList();
104 list.remove(0);
105 System.err.println("rs" + list);
106 return list;
107 }
108
109 private static List<Integer> loadList() {
110 if (SaveOntoFile.f.exists()) {
111 try (ObjectInputStream oStream2 = new ObjectInputStream(
112 // new CipherInputStream(
113 new FileInputStream(SaveOntoFile.f)/* , SaveOntoFile.cip) */)) {
114 List<Integer> list = (LinkedList<Integer>) oStream2.readObject();
115 return list != null && !list.isEmpty() ? list : new LinkedList<>(Arrays.asList(0));
116 } catch (IOException | ClassNotFoundException e) {
117 Logger.logThrowable(e);
118 }
119 }
120 return new LinkedList<>(Arrays.asList(0));
121 }
122
123 private static void createfile() {
124 if (!SaveOntoFile.f.exists()) {
125 try {
126 SaveOntoFile.f.createNewFile();
127 } catch (Exception e) {
128 Logger.logThrowable(e);
129 }
130 }
131 }
132}