Skip to content

Commit

Permalink
Made setProofFileSystem static
Browse files Browse the repository at this point in the history
  • Loading branch information
ngengesenior committed Feb 10, 2024
1 parent cd69f05 commit 7b68012
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static File getProofFileSystem() {
*
* @param fileSystem: The file system used for saving proof data
*/
public void setProofFileSystem(File fileSystem) {
public static void setProofFileSystem(File fileSystem) {
ProofMode.proofFileSystem = fileSystem;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.witness.proofmode.data_processor;

import java.io.IOException;

/**
* Interface for writing and reading files.
*
*/
public interface DataWriterReader {
void writeData(byte[] data) throws IOException;
byte[] readData() throws IOException;
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.witness.proofmode.data_processor;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDescriptorWriter implements DataWriterReader {
private final FileDescriptor fileDescriptor;

public FileDescriptorWriter(FileDescriptor fileDescriptor) {
this.fileDescriptor = fileDescriptor;
}

@Override
public void writeData(byte[] data) throws IOException {
try (FileOutputStream fos = new FileOutputStream(fileDescriptor)) {
fos.write(data);
fos.flush();
}
}

@Override
public byte[] readData() throws IOException {
try (FileInputStream fis = new FileInputStream(fileDescriptor)) {
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
return buffer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.witness.proofmode.data_processor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Writing and reading [File]s
*/
public class FileWriterReader implements DataWriterReader {
private final File fileOut;

public FileWriterReader(File fileOut) {
this.fileOut = fileOut;
}

@Override
public void writeData(byte[] data) throws IOException {
try (DataOutputStream os = new DataOutputStream(new FileOutputStream(fileOut, false))) {
os.write(data);
os.flush();
}
}

@Override
public byte[] readData() throws IOException {
try (DataInputStream is = new DataInputStream(new FileInputStream(fileOut))) {
byte[] buffer = new byte[(int) fileOut.length()];
is.readFully(buffer);
return buffer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.witness.proofmode.data_processor;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;

public class MultiDataWriterReader implements DataWriterReader{
private final DataWriterReader dataWriterReader;

public MultiDataWriterReader(File file) {
this.dataWriterReader = new FileWriterReader(file);
}

public MultiDataWriterReader(FileDescriptor fileDescriptor) {
this.dataWriterReader = new FileDescriptorWriter(fileDescriptor);
}
@Override
public void writeData(byte[] data) throws IOException {

}

@Override
public byte[] readData() throws IOException {
return new byte[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.witness.proofmode.ProofMode;
import org.witness.proofmode.crypto.HashUtils;
import org.witness.proofmode.crypto.pgp.PgpUtils;
import org.witness.proofmode.data_processor.FileWriterReader;
import org.witness.proofmode.notarization.NotarizationListener;
import org.witness.proofmode.notarization.NotarizationProvider;
import org.witness.proofmode.util.DeviceInfo;
Expand Down Expand Up @@ -90,6 +91,7 @@ public class MediaWatcher extends BroadcastReceiver implements ProofModeV1Consta
private Context mContext = null;
private String mPassphrase = null;
private ArrayList<NotarizationProvider> mProviders = new ArrayList<>();
private static FileWriterReader fileWriterReader;

private MediaWatcher(Context context) {
if (mPrefs == null)
Expand Down Expand Up @@ -156,11 +158,10 @@ public static File getHashStorageDir(Context context, String hash) {
}

private static synchronized void writeBytesToFile(Context context, File fileOut, byte[] data) {
fileWriterReader = new FileWriterReader(fileOut);

try {
DataOutputStream os = new DataOutputStream(new FileOutputStream(fileOut, false));
os.write(data);
os.flush();
os.close();
fileWriterReader.writeData(data);
} catch (IOException ioe) {
ioe.printStackTrace();
}
Expand Down

0 comments on commit 7b68012

Please sign in to comment.