Skip to content

Commit

Permalink
Added: Support for reading and writing serialized objects to files an…
Browse files Browse the repository at this point in the history
…d deleting files older than x days in FileUtils
  • Loading branch information
agnostic-apollo committed Aug 20, 2021
1 parent 7de0613 commit 35ea19d
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 23 deletions.
21 changes: 21 additions & 0 deletions termux-shared/src/main/java/com/termux/shared/data/DataUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import androidx.annotation.Nullable;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class DataUtils {

public static final int TRANSACTION_SIZE_LIMIT_IN_BYTES = 100 * 1024; // 100KB
Expand Down Expand Up @@ -172,4 +176,21 @@ public static boolean isNullOrEmpty(String string) {
return string == null || string.isEmpty();
}



/** Get size of a serializable object. */
public static long getSerializedSize(Serializable object) {
if (object == null) return 0;
try {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
objectOutputStream.close();
return byteOutputStream.toByteArray().length;
} catch (Exception e) {
return -1;
}
}

}
Loading

0 comments on commit 35ea19d

Please sign in to comment.