Merge "Cleanup StubProvider a little."

This commit is contained in:
Tomasz Mikolajewski
2015-12-18 02:41:30 +00:00
committed by Android (Google) Code Review

View File

@@ -492,30 +492,6 @@ public class StubProvider extends DocumentsProvider {
}
}
@VisibleForTesting
public File createFile(String rootId, String path, String mimeType, byte[] content)
throws FileNotFoundException, IOException {
Log.d(TAG, "Creating test file " + rootId + ":" + path);
StubDocument root = mRoots.get(rootId).document;
if (root == null) {
throw new FileNotFoundException("No roots with the ID " + rootId + " were found");
}
final File file = new File(root.file, path.substring(1));
if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
if (!file.mkdirs()) {
throw new FileNotFoundException("Couldn't create directory " + file.getPath());
}
} else {
if (!file.createNewFile()) {
throw new FileNotFoundException("Couldn't create file " + file.getPath());
}
try (final FileOutputStream fout = new FileOutputStream(file)) {
fout.write(content);
}
}
return file;
}
@VisibleForTesting
public Uri createRegularFile(String rootId, String path, String mimeType, byte[] content)
throws FileNotFoundException, IOException {
@@ -560,6 +536,29 @@ public class StubProvider extends DocumentsProvider {
return found.file;
}
private File createFile(String rootId, String path, String mimeType, byte[] content)
throws FileNotFoundException, IOException {
Log.d(TAG, "Creating test file " + rootId + ":" + path);
StubDocument root = mRoots.get(rootId).document;
if (root == null) {
throw new FileNotFoundException("No roots with the ID " + rootId + " were found");
}
final File file = new File(root.file, path.substring(1));
if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
if (!file.mkdirs()) {
throw new FileNotFoundException("Couldn't create directory " + file.getPath());
}
} else {
if (!file.createNewFile()) {
throw new FileNotFoundException("Couldn't create file " + file.getPath());
}
try (final FileOutputStream fout = new FileOutputStream(file)) {
fout.write(content);
}
}
return file;
}
final static class RootInfo {
public final String name;
public final StubDocument document;