AbstractCommandLineStoragePoint.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 com.owncloud.android.lib.common.utils.Log_OC;
  23. import java.io.InputStream;
  24. import java.util.Arrays;
  25. /**
  26. * @author Bartosz Przybylski
  27. */
  28. abstract class AbstractCommandLineStoragePoint extends AbstractStoragePointProvider {
  29. private static final String TAG = AbstractCommandLineStoragePoint.class.getSimpleName();
  30. private static final int COMMAND_LINE_OK_RETURN_VALUE = 0;
  31. protected abstract String[] getCommand();
  32. @Override
  33. public boolean canProvideStoragePoints() {
  34. Process process;
  35. try {
  36. process = new ProcessBuilder().command(Arrays.asList(getCommand())).start();
  37. process.waitFor();
  38. } catch (Exception e) {
  39. return false;
  40. }
  41. return process != null && process.exitValue() == COMMAND_LINE_OK_RETURN_VALUE;
  42. }
  43. String getCommandLineResult() {
  44. StringBuilder s = new StringBuilder();
  45. try {
  46. final Process process = new ProcessBuilder().command(getCommand()).redirectErrorStream(true).start();
  47. process.waitFor();
  48. final InputStream is = process.getInputStream();
  49. final byte buffer[] = new byte[1024];
  50. while (is.read(buffer) != -1) {
  51. s.append(new String(buffer, "UTF8"));
  52. }
  53. is.close();
  54. } catch (final Exception e) {
  55. Log_OC.e(TAG, "Error retrieving command line results!", e);
  56. }
  57. return s.toString();
  58. }
  59. }