Advertisement
Guest User

Untitled

a guest
May 30th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. private static String readFile(String path) {
  2.         api = new BoxAPIConnection(DEVELOPER_TOKEN);
  3.         BoxFolder root = changeDirectory(path);
  4.         String[] folders = path.split("/");
  5.         BoxFile res = searchFile(root, folders[folders.length - 1]);
  6.         ByteOutputStream out = new ByteOutputStream();
  7.         res.download(out);
  8.         String contain = out.toString();
  9.         return contain;
  10.     }
  11.     private static BoxFolder changeDirectory(String path) {
  12.         BoxFolder root = BoxFolder.getRootFolder(api);
  13.         String[] folders = path.split("/");
  14.         for (String folder : folders) {
  15.             if (folder.length() == 0) {
  16.                 continue;
  17.             }
  18.             Iterable<BoxItem.Info> res = root.search(folder);
  19.             for (BoxItem.Info info : res) {
  20.                 if (info instanceof BoxFolder.Info && info.getName().equals(folder)) {
  21.                     root = new BoxFolder(api, info.getID());
  22.                     break;
  23.                 }
  24.             }
  25.         }
  26.         return root;
  27.     }
  28.     private static BoxFile searchFile(BoxFolder currentDirectory, String name) {
  29.         BoxFile res = null;
  30.         for (BoxItem.Info info : currentDirectory) {
  31.             if (info instanceof BoxFile.Info && info.getName().equals(name)) {
  32.                 res = (BoxFile)info.getResource();
  33.                 break;
  34.             }
  35.         }
  36.         return res;
  37.     }
  38.     private static void writeFile(String path, String file) {
  39.         api = new BoxAPIConnection(DEVELOPER_TOKEN);
  40.         BoxFolder root = changeDirectory(path);
  41.         String[] folders = path.split("/");
  42.         byte[] bytes = file.getBytes();
  43.         ByteInputStream uploadStream = new ByteInputStream(bytes, bytes.length);
  44.         BoxFile fileThatNeedsToUpload = searchFile(root, folders[folders.length - 1]);
  45.         if (fileThatNeedsToUpload == null) {
  46.             root.uploadFile(uploadStream, folders[folders.length - 1]);
  47.         } else {
  48.             removeFile(path);
  49.             writeFile(path, file);
  50.         }
  51.     }
  52.     private static void removeFile(String path) {
  53.         api = new BoxAPIConnection(DEVELOPER_TOKEN);
  54.         BoxFolder root = changeDirectory(path);
  55.         String[] folders = path.split("/");
  56.         BoxFile fileThatNeedsToRemove = searchFile(root, folders[folders.length - 1]);
  57.         if (fileThatNeedsToRemove != null) {
  58.             fileThatNeedsToRemove.delete();
  59.         }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement