- public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final String filename,final Holder<Path> hp, final Holder<Long> hl) throws CadiException {
- if(hp.get()==null) {
- hp.set(Paths.get(tokenDir,filename));
- }
- return readDisk(df,cred,hp.get(),hl);
- }
-
- public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final Path target, final Holder<Long> hexpired) throws CadiException {
- // Try from Disk
- T t = null;
- if(Files.exists(target)) {
- try {
- final InputStream is = Files.newInputStream(target,StandardOpenOption.READ);
- try {
- // Read Expired unencrypted
- long exp=0;
- for(int i=0;i<Long.SIZE;i+=8) {
- exp |= ((long)is.read()<<i);
- }
- hexpired.set(exp);
-
- t = symm.exec(new Symm.SyncExec<T>() {
- @Override
- public T exec(Encryption enc) throws Exception {
- CipherInputStream dis = enc.inputStream(is,false);
- try {
- int size=0;
- for(int i=0;i<Integer.SIZE;i+=8) {
- size |= ((int)dis.read()<<i);
- }
- if(size>256) {
- throw new CadiException("Invalid size in Token Persistence");
- } else if(cred!=null && size!=cred.length) {
- throw new CadiException(HASH_NO_MATCH);
- }
- if(cred!=null) {
- byte[] array = new byte[size];
- if(dis.read(array)>0) {
- for(int i=0;i<size;++i) {
- if(cred[i]!=array[i]) {
- throw new CadiException(HASH_NO_MATCH);
- }
- }
- }
- }
- return df.newData().load(dis).asObject();
- } finally {
- dis.close();
- }
- }
- });
- } finally {
- is.close();
- }
- } catch (NoSuchFileException e) {
- return t;
- } catch (Exception e) {
- throw new CadiException(e);
- }
- }
- return t;
- }
-
- public long readExpiration(final Path target) throws CadiException {
- long exp=0L;
- if(Files.exists(target)) {
- try {
- final InputStream is = Files.newInputStream(target,StandardOpenOption.READ);
- try {
- for(int i=0;i<Long.SIZE;i+=8) {
- exp |= ((long)is.read()<<i);
- }
- } finally {
- is.close();
- }
- return exp;
- } catch (Exception e) {
- throw new CadiException(e);
- }
- }
- return exp;
- }
+ public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final String filename,final Holder<Path> hp, final Holder<Long> hl) throws CadiException {
+ if (hp.get()==null) {
+ hp.set(Paths.get(tokenDir,filename));
+ }
+ return readDisk(df,cred,hp.get(),hl);
+ }
+
+ public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final Path target, final Holder<Long> hexpired) throws CadiException {
+ // Try from Disk
+ T t = null;
+ if (Files.exists(target)) {
+ try {
+ final InputStream is = Files.newInputStream(target,StandardOpenOption.READ);
+ try {
+ // Read Expired unencrypted
+ long exp=0;
+ for (int i=0;i<Long.SIZE;i+=8) {
+ exp |= ((long)is.read()<<i);
+ }
+ hexpired.set(exp);
+
+ t = symm.exec(new Symm.SyncExec<T>() {
+ @Override
+ public T exec(Encryption enc) throws Exception {
+ CipherInputStream dis = enc.inputStream(is,false);
+ try {
+ int size=0;
+ for (int i=0;i<Integer.SIZE;i+=8) {
+ size |= ((int)dis.read()<<i);
+ }
+ if (size>256) {
+ throw new CadiException("Invalid size in Token Persistence");
+ } else if (cred!=null && size!=cred.length) {
+ throw new CadiException(HASH_NO_MATCH);
+ }
+ if (cred!=null) {
+ byte[] array = new byte[size];
+ if (dis.read(array)>0) {
+ for (int i=0;i<size;++i) {
+ if (cred[i]!=array[i]) {
+ throw new CadiException(HASH_NO_MATCH);
+ }
+ }
+ }
+ }
+ return df.newData().load(dis).asObject();
+ } finally {
+ dis.close();
+ }
+ }
+ });
+ } finally {
+ is.close();
+ }
+ } catch (NoSuchFileException e) {
+ return t;
+ } catch (Exception e) {
+ throw new CadiException(e);
+ }
+ }
+ return t;
+ }
+
+ public long readExpiration(final Path target) throws CadiException {
+ long exp=0L;
+ if (Files.exists(target)) {
+ try {
+ final InputStream is = Files.newInputStream(target,StandardOpenOption.READ);
+ try {
+ for (int i=0;i<Long.SIZE;i+=8) {
+ exp |= ((long)is.read()<<i);
+ }
+ } finally {
+ is.close();
+ }
+ return exp;
+ } catch (Exception e) {
+ throw new CadiException(e);
+ }
+ }
+ return exp;
+ }