AbstractCommandLineStoragePoint.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Bartosz Przybylski
  5. * Copyright (C) 2016 Nextcloud
  6. * Copyright (C) 2016 Bartosz Przybylski
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.datastorage.providers;
  22. import java.io.InputStream;
  23. import java.util.Arrays;
  24. /**
  25. * @author Bartosz Przybylski
  26. */
  27. abstract public class AbstractCommandLineStoragePoint extends AbstractStoragePointProvider {
  28. static protected final int sCommandLineOKReturnValue = 0;
  29. protected abstract String[] getCommand();
  30. @Override
  31. public boolean canProvideStoragePoints() {
  32. Process process;
  33. try {
  34. process = new ProcessBuilder().command(Arrays.asList(getCommand())).start();
  35. process.waitFor();
  36. } catch (Exception e) {
  37. return false;
  38. }
  39. return process != null && process.exitValue() == sCommandLineOKReturnValue;
  40. }
  41. protected String getCommandLineResult() {
  42. String s = "";
  43. try {
  44. final Process process = new ProcessBuilder().command(getCommand())
  45. .redirectErrorStream(true).start();
  46. process.waitFor();
  47. final InputStream is = process.getInputStream();
  48. final byte buffer[] = new byte[1024];
  49. while (is.read(buffer) != -1)
  50. s += new String(buffer);
  51. is.close();
  52. } catch (final Exception e) { }
  53. return s;
  54. }
  55. }