Skip to content

Commit 2423a33

Browse files
author
ortis
committed
thread safe Safe
1 parent e94128c commit 2423a33

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed

src/main/java/org/ortis/jsafebox/Safe.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,9 @@ public Safe(final File file, final SecretKey key, final int bufferSize) throws E
172172
this.original.readLong();// data length 0
173173

174174
// init cipher
175-
176175
final Cipher cipher = getCipher();
177176

178177
// read private properties
179-
180178
this.original.read(buffer, 0, this.ivLength);// read properties iv
181179
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(buffer, this.ivLength));
182180
cipher.init(Cipher.DECRYPT_MODE, this.encryptionKey, iv);
@@ -197,7 +195,6 @@ public Safe(final File file, final SecretKey key, final int bufferSize) throws E
197195
final int decrypted = cipher.update(buffer, 0, read, outBuffer);
198196

199197
baos.write(outBuffer, 0, decrypted);
200-
// baos.write(buffer, 0, read);
201198
length -= read;
202199

203200
}
@@ -289,7 +286,7 @@ public Safe(final File file, final SecretKey key, final int bufferSize) throws E
289286
* @return
290287
* @throws Exception
291288
*/
292-
public Block add(final Map<String, String> properties, final InputStream data, TaskProbe probe) throws Exception
289+
public synchronized Block add(final Map<String, String> properties, final InputStream data, TaskProbe probe) throws Exception
293290
{
294291
if (probe == null)
295292
probe = TaskProbe.DULL_PROBE;
@@ -398,7 +395,7 @@ public Block add(final Map<String, String> properties, final InputStream data, T
398395
* @param path:
399396
* path of the data to delete
400397
*/
401-
public void delete(final String path)
398+
public synchronized void delete(final String path)
402399
{
403400

404401
final String comparablePath = path.toUpperCase(Environment.getLocale());
@@ -444,7 +441,7 @@ public void extract(final Block block, final OutputStream outputStream) throws E
444441
* destination of extracted block
445442
* @throws Exception
446443
*/
447-
public void extract(String path, final OutputStream outputStream) throws Exception
444+
public synchronized void extract(String path, final OutputStream outputStream) throws Exception
448445
{
449446

450447
path = path.toUpperCase(Environment.getLocale());
@@ -485,7 +482,7 @@ public void extract(String path, final OutputStream outputStream) throws Excepti
485482
* @return
486483
* @throws Exception
487484
*/
488-
public Map<String, String> readMetadata(final Block block) throws Exception
485+
public synchronized Map<String, String> readMetadata(final Block block) throws Exception
489486
{
490487

491488
this.original.seek(block.getOffset());
@@ -511,7 +508,7 @@ public Map<String, String> readMetadata(final Block block) throws Exception
511508
/**
512509
* Discard pending modification
513510
*/
514-
public void discardChanges() throws Exception
511+
public synchronized void discardChanges() throws Exception
515512
{
516513

517514
for (final Map.Entry<String, Block> temp : this.tempBlocks.entrySet())
@@ -543,7 +540,7 @@ public Safe save() throws Exception
543540
return save(null);
544541
}
545542

546-
public Safe save(TaskProbe probe) throws Exception
543+
public synchronized Safe save(TaskProbe probe) throws Exception
547544
{
548545

549546
if (probe == null)
@@ -704,7 +701,7 @@ public Safe save(TaskProbe probe) throws Exception
704701
* @return
705702
* @throws Exception
706703
*/
707-
public byte [] computeHash(final TaskProbe probe) throws Exception
704+
public synchronized byte [] computeHash(final TaskProbe probe) throws Exception
708705
{
709706
final byte [] hash = computeHash(this.original, getCipher(), this.ivLength, this.encryptionKey, this.bufferSize, probe);
710707
return hash;
@@ -733,7 +730,7 @@ private Cipher getCipher() throws Exception
733730
}
734731

735732
@Override
736-
public void close() throws IOException
733+
public synchronized void close() throws IOException
737734
{
738735
this.original.close();
739736

@@ -858,18 +855,7 @@ public File getTempFile()
858855
* @return
859856
*/
860857
public RandomAccessFile getTemp() throws IOException
861-
{/*
862-
if (this.temp == null)
863-
{
864-
865-
if (this.tempFile.exists())
866-
throw new IOException("File " + this.tempFile + " already exist");
867-
868-
this.temp = new RandomAccessFile(this.tempFile, "rw");
869-
this.tempFile.deleteOnExit();
870-
871-
}
872-
*/
858+
{
873859
return this.temp;
874860
}
875861

@@ -878,10 +864,7 @@ private static long encrypt(final InputStream data, final Cipher cipher, final R
878864

879865
final byte [] buffer = new byte[bufferSize];
880866
final byte [] bufferOut = new byte[bufferSize];
881-
// ByteBuffer in;
882-
883-
// final ByteBuffer out = ByteBuffer.allocateDirect(buffer.length);
884-
867+
885868
long total = 0;
886869
int read;
887870
while ((read = data.read(buffer)) > -1)
@@ -1064,7 +1047,6 @@ public static Map<String, String> readHeader(final File file, final int bufferSi
10641047
final byte [] buffer = new byte[bufferSize];
10651048
final byte [] bufferOut = new byte[bufferSize];
10661049

1067-
// final ByteArrayOutputStream baos = new ByteArrayOutputStream();
10681050

10691051
safeFile.seek(HASHER.getHashLength());
10701052
final ByteBuffer byteBuffer = ByteBuffer.allocate((int) (safeFile.length() - safeFile.getFilePointer()));

0 commit comments

Comments
 (0)