eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtils.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2012-2013 Reece H. Dunn
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.reecedunn.espeak;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. public class FileUtils {
  25. public static String read(File file) throws IOException {
  26. return readByteArray(new FileInputStream(file), (int)file.length()).toString();
  27. }
  28. public static String read(InputStream stream) throws IOException {
  29. return readByteArray(stream, stream.available()).toString();
  30. }
  31. public static String read(InputStream stream, int length) throws IOException {
  32. return readByteArray(stream, length).toString();
  33. }
  34. public static byte[] readBinary(File file) throws IOException {
  35. return readByteArray(new FileInputStream(file), (int)file.length()).toByteArray();
  36. }
  37. public static byte[] readBinary(InputStream stream) throws IOException {
  38. return readByteArray(stream, stream.available()).toByteArray();
  39. }
  40. public static byte[] readBinary(InputStream stream, int length) throws IOException {
  41. return readByteArray(stream, length).toByteArray();
  42. }
  43. private static ByteArrayOutputStream readByteArray(InputStream stream, int length) throws IOException {
  44. ByteArrayOutputStream content = new ByteArrayOutputStream(length);
  45. int c = stream.read();
  46. while (c != -1)
  47. {
  48. content.write((byte)c);
  49. c = stream.read();
  50. }
  51. return content;
  52. }
  53. public static void write(File outputFile, String contents) throws IOException {
  54. write(outputFile, contents.getBytes());
  55. }
  56. public static void write(File outputFile, byte[] contents) throws IOException {
  57. FileOutputStream outputStream = new FileOutputStream(outputFile);
  58. try {
  59. outputStream.write(contents, 0, contents.length);
  60. } finally {
  61. outputStream.close();
  62. }
  63. chmod(outputFile);
  64. }
  65. public static void chmod(File file) {
  66. try {
  67. Runtime.getRuntime().exec("/system/bin/chmod 755 " + file.getAbsolutePath());
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. public static void rmdir(File directory) {
  73. if (!directory.exists() || !directory.isDirectory()) {
  74. return;
  75. }
  76. for (File child : directory.listFiles()) {
  77. if (child.isDirectory()) {
  78. rmdir(child);
  79. }
  80. child.delete();
  81. }
  82. }
  83. }