DataStore now works

This commit is contained in:
Grant Limberg 2015-05-02 18:22:56 -07:00
parent a9307693a6
commit ad6ec22857
2 changed files with 23 additions and 7 deletions

View File

@ -18,25 +18,29 @@ public class DataStore implements DataStoreGetListener, DataStorePutListener {
@Override
public int onDataStorePut(String name, byte[] buffer, boolean secure) {
System.out.println("Writing File: " + name);
try {
FileOutputStream fos = _provider.getOutputFileStream(name);
fos.write(buffer);
fos.close();
return buffer.length;
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
return -1;
} catch (IOException io) {
io.printStackTrace();
return -2;
}
return 0;
}
@Override
public int onDelete(String name) {
System.out.println("Deleting File: " + name);
try {
_provider.deleteFile(name);
return 0;
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
}
@ -44,6 +48,7 @@ public class DataStore implements DataStoreGetListener, DataStorePutListener {
@Override
public long onDataStoreGet(String name, byte[] out_buffer,
long bufferIndex, long[] out_objectSize) {
System.out.println("Reading File: " + name);
try {
FileInputStream fin = _provider.getInputFileStream(name);
out_objectSize[0] = fin.getChannel().size();
@ -55,8 +60,11 @@ public class DataStore implements DataStoreGetListener, DataStorePutListener {
fin.close();
return read;
} catch (FileNotFoundException fnf) {
return -1;
// Can't read a file that doesn't exist!
out_objectSize[0] = 0;
return 0;
} catch (IOException io) {
io.printStackTrace();
return -2;
}
}

View File

@ -16,20 +16,28 @@ public class JavaFileProvider implements DataStoreFileProvider {
@Override
public FileInputStream getInputFileStream(String name)
throws FileNotFoundException {
File f = new File(_path + File.pathSeparator + name);
File f = new File(_path + File.separator + name);
return new FileInputStream(f);
}
@Override
public FileOutputStream getOutputFileStream(String name)
throws FileNotFoundException {
File f = new File(_path + File.pathSeparator + name);
File f = new File(_path + File.separator + name);
if(!f.exists())
{
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return new FileOutputStream(f);
}
@Override
public void deleteFile(String name) throws IOException {
File f = new File(_path + File.pathSeparator + name);
File f = new File(_path + File.separator + name);
boolean success = f.delete();
if(!success) {
throw new IOException("Unable to delete file: " + _path + File.pathSeparator + name);