mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-01-19 03:06:26 +00:00
Adding Data Store implementations for Android and normal Java
This commit is contained in:
parent
742c59a7c7
commit
a9307693a6
40
java/src/com/zerotier/one/AndroidFileProvider.java
Normal file
40
java/src/com/zerotier/one/AndroidFileProvider.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.zerotier.one;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
public class AndroidFileProvider implements DataStoreFileProvider {
|
||||||
|
Context _ctx;
|
||||||
|
|
||||||
|
AndroidFileProvider(Context ctx) {
|
||||||
|
this._ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileInputStream getInputFileStream(String name)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return _ctx.openFileInput(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileOutputStream getOutputFileStream(String name)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return _ctx.openFileOutput(name, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteFile(String name) throws IOException {
|
||||||
|
boolean success = _ctx.deleteFile(name);
|
||||||
|
if(!success)
|
||||||
|
{
|
||||||
|
throw new IOException("Unable to delete file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
65
java/src/com/zerotier/one/DataStore.java
Normal file
65
java/src/com/zerotier/one/DataStore.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package com.zerotier.one;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.zerotier.sdk.DataStoreGetListener;
|
||||||
|
import com.zerotier.sdk.DataStorePutListener;
|
||||||
|
|
||||||
|
public class DataStore implements DataStoreGetListener, DataStorePutListener {
|
||||||
|
|
||||||
|
private DataStoreFileProvider _provider;
|
||||||
|
|
||||||
|
DataStore(DataStoreFileProvider provider) {
|
||||||
|
this._provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onDataStorePut(String name, byte[] buffer, boolean secure) {
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = _provider.getOutputFileStream(name);
|
||||||
|
fos.write(buffer);
|
||||||
|
fos.close();
|
||||||
|
return buffer.length;
|
||||||
|
} catch (FileNotFoundException fnf) {
|
||||||
|
|
||||||
|
} catch (IOException io) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onDelete(String name) {
|
||||||
|
try {
|
||||||
|
_provider.deleteFile(name);
|
||||||
|
return 0;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long onDataStoreGet(String name, byte[] out_buffer,
|
||||||
|
long bufferIndex, long[] out_objectSize) {
|
||||||
|
try {
|
||||||
|
FileInputStream fin = _provider.getInputFileStream(name);
|
||||||
|
out_objectSize[0] = fin.getChannel().size();
|
||||||
|
if(bufferIndex > 0)
|
||||||
|
{
|
||||||
|
fin.skip(bufferIndex);
|
||||||
|
}
|
||||||
|
int read = fin.read(out_buffer);
|
||||||
|
fin.close();
|
||||||
|
return read;
|
||||||
|
} catch (FileNotFoundException fnf) {
|
||||||
|
return -1;
|
||||||
|
} catch (IOException io) {
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
12
java/src/com/zerotier/one/DataStoreFileProvider.java
Normal file
12
java/src/com/zerotier/one/DataStoreFileProvider.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.zerotier.one;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface DataStoreFileProvider {
|
||||||
|
FileInputStream getInputFileStream(String name) throws FileNotFoundException;
|
||||||
|
FileOutputStream getOutputFileStream(String name) throws FileNotFoundException;
|
||||||
|
void deleteFile(String name) throws IOException;
|
||||||
|
}
|
38
java/src/com/zerotier/one/JavaFileProvider.java
Normal file
38
java/src/com/zerotier/one/JavaFileProvider.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.zerotier.one;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class JavaFileProvider implements DataStoreFileProvider {
|
||||||
|
private String _path;
|
||||||
|
|
||||||
|
public JavaFileProvider(String path) {
|
||||||
|
this._path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileInputStream getInputFileStream(String name)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
File f = new File(_path + File.pathSeparator + name);
|
||||||
|
return new FileInputStream(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileOutputStream getOutputFileStream(String name)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
File f = new File(_path + File.pathSeparator + name);
|
||||||
|
return new FileOutputStream(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteFile(String name) throws IOException {
|
||||||
|
File f = new File(_path + File.pathSeparator + name);
|
||||||
|
boolean success = f.delete();
|
||||||
|
if(!success) {
|
||||||
|
throw new IOException("Unable to delete file: " + _path + File.pathSeparator + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user