11package net .sharksystem .asap .engine ;
22
3+ import net .sharksystem .SharkException ;
34import net .sharksystem .asap .EncounterConnectionType ;
45import net .sharksystem .asap .crypto .*;
56import net .sharksystem .asap .utils .ASAPSerialization ;
7+ import net .sharksystem .utils .ExtraData ;
8+ import net .sharksystem .utils .ExtraDataFS ;
69import net .sharksystem .utils .Utils ;
710import net .sharksystem .asap .ASAP ;
811import net .sharksystem .asap .ASAPException ;
@@ -146,7 +149,7 @@ private ASAPInternalPeerFS(CharSequence owner, CharSequence rootFolderName, long
146149 }
147150 }
148151
149- this .restoreExtraData ();
152+ // this.restoreExtraData();
150153
151154// System.out.println(this.getLogStart() + "SHOULD also set up engine " + FORMAT_UNDECRYPTABLE_MESSAGES);
152155 }
@@ -681,14 +684,24 @@ public ASAPKeyStore getASAPKeyStore() throws ASAPSecurityException {
681684 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
682685 // make extra data persist //
683686 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
687+ private ExtraData extraData = null ;
684688
685- public static final String EXTRA_FILE_NAME = ".extraData" ;
689+ private ExtraData getExtraData () throws SharkException , IOException {
690+ if (this .extraData == null ) {
691+ this .extraData = new ExtraDataFS (this .rootFolderName );
692+ }
693+
694+ return this .extraData ;
695+ }
696+ /*
697+ private static final String EXTRA_FILE_NAME = ".extraData";
686698 private Map<CharSequence, byte[]> extraData = new HashMap<>();
687699
688700 private File getExtraFile() {
689701 String extraFileName = this.rootFolderName + "/" + EXTRA_FILE_NAME;
690702 return new File(extraFileName);
691703 }
704+ */
692705
693706 /*
694707 Here is the catch... There can be - and in Android will - two instances share data over file system. The use
@@ -699,105 +712,11 @@ private File getExtraFile() {
699712
700713 */
701714
702- private long timeStampSyncExtraData = -1 ; // never saved anything
703-
704- private void extraDataSync () throws IOException , ASAPException {
705- InputStream is = null ;
706- try {
707- is = new FileInputStream (this .getExtraFile ());
708- }
709- catch (IOException ioEx ) {
710- // no such file - save it instead - there is no conflict
711- this .saveExtraData ();
712- return ;
713- }
714-
715- // read time stamp
716- long timeStampSaved = ASAPSerialization .readLongParameter (is );
717-
718- if (this .timeStampSyncExtraData < timeStampSaved ) {
719- // there is an external copy that was created after our last sync
720- }
721-
722- // discard local changes and read from file
723- this .restoreExtraData ();
724- }
725-
726- public void saveExtraData () throws IOException {
727- OutputStream os = new FileOutputStream (this .getExtraFile ());
728-
729- // write time stamp
730- this .timeStampSyncExtraData = System .currentTimeMillis ();
731- ASAPSerialization .writeLongParameter (this .timeStampSyncExtraData , os );
732- ASAPSerialization .writeNonNegativeIntegerParameter (this .extraData .size (), os );
733-
734- for (CharSequence key : this .extraData .keySet ()) {
735- // write key
736- ASAPSerialization .writeCharSequenceParameter (key , os );
737- // value
738- ASAPSerialization .writeByteArray (this .extraData .get (key ), os );
739- }
740-
741- os .close ();
742- }
743-
744- /**
745- * Always restore. If there is no such file - write it.
746- * @throws IOException
747- * @throws ASAPException
748- */
749- public void restoreExtraData () throws IOException , ASAPException {
750- InputStream is = null ;
751- try {
752- is = new FileInputStream (this .getExtraFile ());
753- }
754- catch (IOException ioEx ) {
755- // no such file - nothing to do here
756- return ;
757- }
758-
759- long timeStampSaved = ASAPSerialization .readLongParameter (is );
760- if (timeStampSaved == this .timeStampSyncExtraData ) {
761- is .close ();
762- return ;
763- }
764-
765- // something changed - get a fresh copy
766- this .timeStampSyncExtraData = timeStampSaved ;
767-
768- this .extraData = new HashMap <>();
769- int counter = ASAPSerialization .readIntegerParameter (is );
770-
771- while (counter -- > 0 ) {
772- // read key
773- CharSequence key = ASAPSerialization .readCharSequenceParameter (is );
774- // value
775- byte [] value = ASAPSerialization .readByteArray (is );
776-
777- // save in memory
778- this .extraData .put (key , value );
779- }
780- is .close ();
781- }
782-
783- /**
784- * Make a value persistent with key
785- * @param key
786- * @param value
787- */
788- public void putExtra (CharSequence key , byte [] value ) throws IOException , ASAPException {
789- this .extraDataSync ();
790- this .extraData .put (key , value );
791- this .saveExtraData ();
715+ public void putExtra (CharSequence key , byte [] value ) throws IOException , SharkException {
716+ this .getExtraData ().putExtra (key , value );
792717 }
793718
794- /**
795- * Return a value - can be null if null was set as value.
796- * @param key
797- * @throws ASAPException key never used in putExtra
798- */
799- public byte [] getExtra (CharSequence key ) throws IOException , ASAPException {
800- this .restoreExtraData ();
801- return this .extraData .get (key );
719+ public byte [] getExtra (CharSequence key ) throws IOException , SharkException {
720+ return this .getExtraData ().getExtra (key );
802721 }
803722}
0 commit comments