Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static String readFile(String path) {
- api = new BoxAPIConnection(DEVELOPER_TOKEN);
- BoxFolder root = changeDirectory(path);
- String[] folders = path.split("/");
- BoxFile res = searchFile(root, folders[folders.length - 1]);
- ByteOutputStream out = new ByteOutputStream();
- res.download(out);
- String contain = out.toString();
- return contain;
- }
- private static BoxFolder changeDirectory(String path) {
- BoxFolder root = BoxFolder.getRootFolder(api);
- String[] folders = path.split("/");
- for (String folder : folders) {
- if (folder.length() == 0) {
- continue;
- }
- Iterable<BoxItem.Info> res = root.search(folder);
- for (BoxItem.Info info : res) {
- if (info instanceof BoxFolder.Info && info.getName().equals(folder)) {
- root = new BoxFolder(api, info.getID());
- break;
- }
- }
- }
- return root;
- }
- private static BoxFile searchFile(BoxFolder currentDirectory, String name) {
- BoxFile res = null;
- for (BoxItem.Info info : currentDirectory) {
- if (info instanceof BoxFile.Info && info.getName().equals(name)) {
- res = (BoxFile)info.getResource();
- break;
- }
- }
- return res;
- }
- private static void writeFile(String path, String file) {
- api = new BoxAPIConnection(DEVELOPER_TOKEN);
- BoxFolder root = changeDirectory(path);
- String[] folders = path.split("/");
- byte[] bytes = file.getBytes();
- ByteInputStream uploadStream = new ByteInputStream(bytes, bytes.length);
- BoxFile fileThatNeedsToUpload = searchFile(root, folders[folders.length - 1]);
- if (fileThatNeedsToUpload == null) {
- root.uploadFile(uploadStream, folders[folders.length - 1]);
- } else {
- removeFile(path);
- writeFile(path, file);
- }
- }
- private static void removeFile(String path) {
- api = new BoxAPIConnection(DEVELOPER_TOKEN);
- BoxFolder root = changeDirectory(path);
- String[] folders = path.split("/");
- BoxFile fileThatNeedsToRemove = searchFile(root, folders[folders.length - 1]);
- if (fileThatNeedsToRemove != null) {
- fileThatNeedsToRemove.delete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement