diff --git a/marytts-common/src/main/java/com/twmacinta/io/NullOutputStream.java b/marytts-common/src/main/java/com/twmacinta/io/NullOutputStream.java index 5ea2c685..6ca049e2 100644 --- a/marytts-common/src/main/java/com/twmacinta/io/NullOutputStream.java +++ b/marytts-common/src/main/java/com/twmacinta/io/NullOutputStream.java @@ -4,56 +4,56 @@ import java.io.IOException; import java.io.OutputStream; /** - * Copyright (c) 2001, 2002 by Pensamos Digital, Inc., All Rights Reserved.

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * Copyright (c) 2001, 2002 by Pensamos Digital, Inc., All Rights Reserved. *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. + * + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. *

- * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. + *

+ * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *

* This OutputStream discards all data written to it. - * + * * @author Tim Macinta (twm@alum.mit.edu) **/ public class NullOutputStream extends OutputStream { - private boolean closed = false; + private boolean closed = false; - public NullOutputStream() { - } + public NullOutputStream() { + } - public void close() { - this.closed = true; - } + public void close() { + this.closed = true; + } - public void flush() throws IOException { - if (this.closed) _throwClosed(); - } + public void flush() throws IOException { + if (this.closed) + _throwClosed(); + } - private void _throwClosed() throws IOException { - throw new IOException("This OutputStream has been closed"); - } + private void _throwClosed() throws IOException { + throw new IOException("This OutputStream has been closed"); + } - public void write(byte[] b) throws IOException { - if (this.closed) _throwClosed(); - } + public void write(byte[] b) throws IOException { + if (this.closed) + _throwClosed(); + } - public void write(byte[] b, int offset, int len) throws IOException { - if (this.closed) _throwClosed(); - } + public void write(byte[] b, int offset, int len) throws IOException { + if (this.closed) + _throwClosed(); + } - public void write(int b) throws IOException { - if (this.closed) _throwClosed(); - } + public void write(int b) throws IOException { + if (this.closed) + _throwClosed(); + } } diff --git a/marytts-common/src/main/java/com/twmacinta/util/MD5.java b/marytts-common/src/main/java/com/twmacinta/util/MD5.java index 2895289f..6c551e3d 100644 --- a/marytts-common/src/main/java/com/twmacinta/util/MD5.java +++ b/marytts-common/src/main/java/com/twmacinta/util/MD5.java @@ -13,889 +13,804 @@ import java.io.InputStream; * (c) Santeri Paavolainen, Helsinki Finland 1996
* Many changes Copyright (c) 2002 - 2010 Timothy W Macinta
*

- * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. *

- * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free + * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *

- * See http://www.twmacinta.com/myjava/fast_md5.php for more information - * on this file and the related files. + * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file and the related files. *

- * This was originally a rather straight re-implementation of the - * reference implementation given in RFC1321 by RSA. It passes the MD5 - * test suite as defined in RFC1321. + * This was originally a rather straight re-implementation of the reference implementation given in RFC1321 by RSA. It passes the + * MD5 test suite as defined in RFC1321. *

- * Many optimizations made by Timothy W Macinta. Reduced time to checksum a - * test file in Java alone to roughly half the time taken compared with - * java.security.MessageDigest (within an intepretter). Also added an - * optional native method to reduce the time even further. - * See http://www.twmacinta.com/myjava/fast_md5.php for further information - * on the time improvements achieved. + * Many optimizations made by Timothy W Macinta. Reduced time to checksum a test file in Java alone to roughly half the time taken + * compared with java.security.MessageDigest (within an intepretter). Also added an optional native method to reduce the time even + * further. See http://www.twmacinta.com/myjava/fast_md5.php for further information on the time improvements achieved. *

* Some bug fixes also made by Timothy W Macinta. *

- * Please note: I (Timothy Macinta) have put this code in the - * com.twmacinta.util package only because it came without a package. I - * was not the the original author of the code, although I did - * optimize it (substantially) and fix some bugs. + * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I + * was not the the original author of the code, although I did optimize it (substantially) and fix some bugs. *

- * This Java class has been derived from the RSA Data Security, Inc. MD5 - * Message-Digest Algorithm and its reference implementation. + * This Java class has been derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm and its reference + * implementation. *

- * This class will attempt to use a native method to quickly compute - * checksums when the appropriate native library is available. On Linux, - * this library should be named "MD5.so" and on Windows it should be - * named "MD5.dll". The code will attempt to locate the library in the - * following locations in the order given: - * + * This class will attempt to use a native method to quickly compute checksums when the appropriate native library is available. + * On Linux, this library should be named "MD5.so" and on Windows it should be named "MD5.dll". The code will attempt to locate + * the library in the following locations in the order given: + * *

    - *
  1. The path specified by the system property - * "com.twmacinta.util.MD5.NATIVE_LIB_FILE" - * (be sure to include "MD5.so", "MD5.dll", - * or "MD5.jnilib" as appropriate at the end - * of the path). - *
  2. A platform specific directory beneath the "lib/arch/" directory. - * For example, On Windows for 32 bit x86 architectures, this is - * "lib/arch/win32_x86/". - *
  3. Within the "lib/" directory. - *
  4. Within the current directory. + *
  5. The path specified by the system property "com.twmacinta.util.MD5.NATIVE_LIB_FILE" (be sure to include "MD5.so", "MD5.dll", + * or "MD5.jnilib" as appropriate at the end of the path). + *
  6. A platform specific directory beneath the "lib/arch/" directory. For example, On Windows for 32 bit x86 architectures, this + * is "lib/arch/win32_x86/". + *
  7. Within the "lib/" directory. + *
  8. Within the current directory. *
- * + * *

- * If the library is not found, the code will fall back to the default - * (slower) Java code. + * If the library is not found, the code will fall back to the default (slower) Java code. *

- * As a side effect of having the code search for the native library, - * SecurityExceptions might be thrown on JVMs that have a restrictive - * SecurityManager. The initialization code attempts to silently discard - * these exceptions and continue, but many SecurityManagers will - * attempt to notify the user directly of all SecurityExceptions thrown. - * Consequently, the code has provisions for skipping the search for - * the native library. Any of these provisions may be used to skip the - * search as long as they are performed before the first - * instance of a com.twmacinta.util.MD5 object is constructed (note that - * the convenience stream objects will implicitly create an MD5 object). + * As a side effect of having the code search for the native library, SecurityExceptions might be thrown on JVMs that have a + * restrictive SecurityManager. The initialization code attempts to silently discard these exceptions and continue, but many + * SecurityManagers will attempt to notify the user directly of all SecurityExceptions thrown. Consequently, the code has + * provisions for skipping the search for the native library. Any of these provisions may be used to skip the search as long as + * they are performed before the first instance of a com.twmacinta.util.MD5 object is constructed (note that the + * convenience stream objects will implicitly create an MD5 object). *

- * The first option is to set the system property - * "com.twmacinta.util.MD5.NO_NATIVE_LIB" to "true" or "1". - * Unfortunately, SecurityManagers may also choose to disallow system - * property setting, so this won't be of use in all cases. + * The first option is to set the system property "com.twmacinta.util.MD5.NO_NATIVE_LIB" to "true" or "1". Unfortunately, + * SecurityManagers may also choose to disallow system property setting, so this won't be of use in all cases. *

- * The second option is to call - * com.twmacinta.util.MD5.initNativeLibrary(true) before any MD5 objects - * are constructed. - * + * The second option is to call com.twmacinta.util.MD5.initNativeLibrary(true) before any MD5 objects are constructed. + * * @author Santeri Paavolainen * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes) */ public class MD5 { - - /** - * MD5 state - **/ - MD5State state; - - /** - * If Final() has been called, finals is set to the current finals - * state. Any Update() causes this to be set to null. - **/ - MD5State finals; - - /** - * Padding for Final() - **/ - static byte padding[] = { - (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - - private static boolean native_lib_loaded = false; - private static boolean native_lib_init_pending = true; - - /** - * Initialize MD5 internal state (object can be reused just by - * calling Init() after every Final() - **/ - public synchronized void Init () { - state = new MD5State(); - finals = null; - } - - /** - * Class constructor - **/ - public MD5 () { - if (native_lib_init_pending) _initNativeLibrary(); - this.Init(); - } - - /** - * Initialize class, and update hash with ob.toString() - * - * @param ob Object, ob.toString() is used to update hash - * after initialization - **/ - public MD5 (Object ob) { - this(); - Update(ob.toString()); - } - - private void Decode (byte buffer[], int shift, int[] out) { - /*len += shift; - for (int i = 0; shift < len; i++, shift += 4) { - out[i] = ((int) (buffer[shift] & 0xff)) | - (((int) (buffer[shift + 1] & 0xff)) << 8) | - (((int) (buffer[shift + 2] & 0xff)) << 16) | - (((int) buffer[shift + 3]) << 24); - }*/ - - // unrolled loop (original loop shown above) - - out[0] = ((int) (buffer[shift] & 0xff)) | - (((int) (buffer[shift + 1] & 0xff)) << 8) | - (((int) (buffer[shift + 2] & 0xff)) << 16) | - (((int) buffer[shift + 3]) << 24); - out[1] = ((int) (buffer[shift + 4] & 0xff)) | - (((int) (buffer[shift + 5] & 0xff)) << 8) | - (((int) (buffer[shift + 6] & 0xff)) << 16) | - (((int) buffer[shift + 7]) << 24); - out[2] = ((int) (buffer[shift + 8] & 0xff)) | - (((int) (buffer[shift + 9] & 0xff)) << 8) | - (((int) (buffer[shift + 10] & 0xff)) << 16) | - (((int) buffer[shift + 11]) << 24); - out[3] = ((int) (buffer[shift + 12] & 0xff)) | - (((int) (buffer[shift + 13] & 0xff)) << 8) | - (((int) (buffer[shift + 14] & 0xff)) << 16) | - (((int) buffer[shift + 15]) << 24); - out[4] = ((int) (buffer[shift + 16] & 0xff)) | - (((int) (buffer[shift + 17] & 0xff)) << 8) | - (((int) (buffer[shift + 18] & 0xff)) << 16) | - (((int) buffer[shift + 19]) << 24); - out[5] = ((int) (buffer[shift + 20] & 0xff)) | - (((int) (buffer[shift + 21] & 0xff)) << 8) | - (((int) (buffer[shift + 22] & 0xff)) << 16) | - (((int) buffer[shift + 23]) << 24); - out[6] = ((int) (buffer[shift + 24] & 0xff)) | - (((int) (buffer[shift + 25] & 0xff)) << 8) | - (((int) (buffer[shift + 26] & 0xff)) << 16) | - (((int) buffer[shift + 27]) << 24); - out[7] = ((int) (buffer[shift + 28] & 0xff)) | - (((int) (buffer[shift + 29] & 0xff)) << 8) | - (((int) (buffer[shift + 30] & 0xff)) << 16) | - (((int) buffer[shift + 31]) << 24); - out[8] = ((int) (buffer[shift + 32] & 0xff)) | - (((int) (buffer[shift + 33] & 0xff)) << 8) | - (((int) (buffer[shift + 34] & 0xff)) << 16) | - (((int) buffer[shift + 35]) << 24); - out[9] = ((int) (buffer[shift + 36] & 0xff)) | - (((int) (buffer[shift + 37] & 0xff)) << 8) | - (((int) (buffer[shift + 38] & 0xff)) << 16) | - (((int) buffer[shift + 39]) << 24); - out[10] = ((int) (buffer[shift + 40] & 0xff)) | - (((int) (buffer[shift + 41] & 0xff)) << 8) | - (((int) (buffer[shift + 42] & 0xff)) << 16) | - (((int) buffer[shift + 43]) << 24); - out[11] = ((int) (buffer[shift + 44] & 0xff)) | - (((int) (buffer[shift + 45] & 0xff)) << 8) | - (((int) (buffer[shift + 46] & 0xff)) << 16) | - (((int) buffer[shift + 47]) << 24); - out[12] = ((int) (buffer[shift + 48] & 0xff)) | - (((int) (buffer[shift + 49] & 0xff)) << 8) | - (((int) (buffer[shift + 50] & 0xff)) << 16) | - (((int) buffer[shift + 51]) << 24); - out[13] = ((int) (buffer[shift + 52] & 0xff)) | - (((int) (buffer[shift + 53] & 0xff)) << 8) | - (((int) (buffer[shift + 54] & 0xff)) << 16) | - (((int) buffer[shift + 55]) << 24); - out[14] = ((int) (buffer[shift + 56] & 0xff)) | - (((int) (buffer[shift + 57] & 0xff)) << 8) | - (((int) (buffer[shift + 58] & 0xff)) << 16) | - (((int) buffer[shift + 59]) << 24); - out[15] = ((int) (buffer[shift + 60] & 0xff)) | - (((int) (buffer[shift + 61] & 0xff)) << 8) | - (((int) (buffer[shift + 62] & 0xff)) << 16) | - (((int) buffer[shift + 63]) << 24); - } - - private native void Transform_native (int[] state, byte buffer[], int shift, int length); - - private void Transform (MD5State state, byte buffer[], int shift, int[] decode_buf) { - int - a = state.state[0], - b = state.state[1], - c = state.state[2], - d = state.state[3], - x[] = decode_buf; - - Decode(buffer, shift, decode_buf); - - /* Round 1 */ - a += ((b & c) | (~b & d)) + x[ 0] + 0xd76aa478; /* 1 */ - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[ 1] + 0xe8c7b756; /* 2 */ - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[ 2] + 0x242070db; /* 3 */ - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[ 3] + 0xc1bdceee; /* 4 */ - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[ 4] + 0xf57c0faf; /* 5 */ - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[ 5] + 0x4787c62a; /* 6 */ - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[ 6] + 0xa8304613; /* 7 */ - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[ 7] + 0xfd469501; /* 8 */ - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[ 8] + 0x698098d8; /* 9 */ - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[ 9] + 0x8b44f7af; /* 10 */ - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; /* 11 */ - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */ - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */ - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; /* 14 */ - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; /* 15 */ - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */ - b = ((b << 22) | (b >>> 10)) + c; - - - /* Round 2 */ - a += ((b & d) | (c & ~d)) + x[ 1] + 0xf61e2562; /* 17 */ - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[ 6] + 0xc040b340; /* 18 */ - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; /* 19 */ - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[ 0] + 0xe9b6c7aa; /* 20 */ - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[ 5] + 0xd62f105d; /* 21 */ - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; /* 22 */ - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; /* 23 */ - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[ 4] + 0xe7d3fbc8; /* 24 */ - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[ 9] + 0x21e1cde6; /* 25 */ - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; /* 26 */ - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[ 3] + 0xf4d50d87; /* 27 */ - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[ 8] + 0x455a14ed; /* 28 */ - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */ - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[ 2] + 0xfcefa3f8; /* 30 */ - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[ 7] + 0x676f02d9; /* 31 */ - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */ - b = ((b << 20) | (b >>> 12)) + c; - - - /* Round 3 */ - a += (b ^ c ^ d) + x[ 5] + 0xfffa3942; /* 33 */ - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[ 8] + 0x8771f681; /* 34 */ - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[11] + 0x6d9d6122; /* 35 */ - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[14] + 0xfde5380c; /* 36 */ - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[ 1] + 0xa4beea44; /* 37 */ - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[ 4] + 0x4bdecfa9; /* 38 */ - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[ 7] + 0xf6bb4b60; /* 39 */ - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[10] + 0xbebfbc70; /* 40 */ - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[13] + 0x289b7ec6; /* 41 */ - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[ 0] + 0xeaa127fa; /* 42 */ - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[ 3] + 0xd4ef3085; /* 43 */ - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[ 6] + 0x04881d05; /* 44 */ - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[ 9] + 0xd9d4d039; /* 33 */ - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[12] + 0xe6db99e5; /* 34 */ - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; /* 35 */ - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[ 2] + 0xc4ac5665; /* 36 */ - b = ((b << 23) | (b >>> 9)) + c; - - - /* Round 4 */ - a += (c ^ (b | ~d)) + x[ 0] + 0xf4292244; /* 49 */ - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[ 7] + 0x432aff97; /* 50 */ - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; /* 51 */ - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[ 5] + 0xfc93a039; /* 52 */ - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; /* 53 */ - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[ 3] + 0x8f0ccc92; /* 54 */ - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; /* 55 */ - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[ 1] + 0x85845dd1; /* 56 */ - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[ 8] + 0x6fa87e4f; /* 57 */ - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; /* 58 */ - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[ 6] + 0xa3014314; /* 59 */ - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; /* 60 */ - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[ 4] + 0xf7537e82; /* 61 */ - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; /* 62 */ - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[ 2] + 0x2ad7d2bb; /* 63 */ - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[ 9] + 0xeb86d391; /* 64 */ - b = ((b << 21) | (b >>> 11)) + c; - - state.state[0] += a; - state.state[1] += b; - state.state[2] += c; - state.state[3] += d; - } - - /** - * Updates hash with the bytebuffer given (using at maximum length bytes from - * that buffer) - * - * @param stat Which state is updated - * @param buffer Array of bytes to be hashed - * @param offset Offset to buffer array - * @param length Use at maximum `length' bytes (absolute - * maximum is buffer.length) - */ - public void Update (MD5State stat, byte buffer[], int offset, int length) { - int index, partlen, i, start; - finals = null; - - /* Length can be told to be shorter, but not inter */ - if ((length - offset)> buffer.length) - length = buffer.length - offset; - - /* compute number of bytes mod 64 */ - - index = (int) (stat.count & 0x3f); - stat.count += length; - - partlen = 64 - index; - - if (length >= partlen) { - - // update state (using native method) to reflect input - - if (native_lib_loaded) { - if (partlen == 64) { - partlen = 0; - } else { - for (i = 0; i < partlen; i++) - stat.buffer[i + index] = buffer[i + offset]; - Transform_native(stat.state, stat.buffer, 0, 64); - } - i = partlen + ((length - partlen) / 64) * 64; - - // break into chunks to guard against stack overflow in JNI - - int transformLength = length - partlen; - int transformOffset = partlen + offset; - final int MAX_LENGTH = 65536; // prevent stack overflow in JNI - while (true) { - if (transformLength > MAX_LENGTH) { - Transform_native(stat.state, buffer, transformOffset, MAX_LENGTH); - transformLength -= MAX_LENGTH; - transformOffset += MAX_LENGTH; - } else { - Transform_native(stat.state, buffer, transformOffset, transformLength); - break; - } - } - } - - // update state (using only Java) to reflect input - - else { - int[] decode_buf = new int[16]; - if (partlen == 64) { - partlen = 0; - } else { - for (i = 0; i < partlen; i++) - stat.buffer[i + index] = buffer[i + offset]; - Transform(stat, stat.buffer, 0, decode_buf); - } - for (i = partlen; (i + 63) < length; i+= 64) { - Transform(stat, buffer, i + offset, decode_buf); - } - } - index = 0; - } else { - i = 0; - } - - /* buffer remaining input */ - if (i < length) { - start = i; - for (; i < length; i++) { - stat.buffer[index + i - start] = buffer[i + offset]; - } - } - } - - /* - * Update()s for other datatypes than byte[] also. Update(byte[], int) - * is only the main driver. - */ - - /** - * Plain update, updates this object - **/ - public void Update (byte buffer[], int offset, int length) { - Update(this.state, buffer, offset, length); - } - - public void Update (byte buffer[], int length) { - Update(this.state, buffer, 0, length); - } - - /** - * Updates hash with given array of bytes - * - * @param buffer Array of bytes to use for updating the hash - **/ - public void Update (byte buffer[]) { - Update(buffer, 0, buffer.length); - } - - /** - * Updates hash with a single byte - * - * @param b Single byte to update the hash - **/ - public void Update (byte b) { - byte buffer[] = new byte[1]; - buffer[0] = b; - - Update(buffer, 1); - } - - /** - * Update buffer with given string. Note that because the version of - * the s.getBytes() method without parameters is used to convert the - * string to a byte array, the results of this method may be different - * on different platforms. The s.getBytes() method converts the string - * into a byte array using the current platform's default character set - * and may therefore have different results on platforms with different - * default character sets. If a version that works consistently - * across platforms with different default character sets is desired, - * use the overloaded version of the Update() method which takes a - * string and a character encoding. - * - * @param s String to be update to hash (is used as s.getBytes()) - **/ - public void Update (String s) { - byte chars[] = s.getBytes(); - Update(chars, chars.length); - } - - /** - * Update buffer with given string using the given encoding. If the - * given encoding is null, the encoding "ISO8859_1" is used. - * - * @param s String to be update to hash (is used as - * s.getBytes(charset_name)) - * @param charset_name The character set to use to convert s to a - * byte array, or null if the "ISO8859_1" - * character set is desired. - * @exception java.io.UnsupportedEncodingException If the named - * charset is not supported. - **/ - public void Update (String s, String charset_name) throws java.io.UnsupportedEncodingException { - if (charset_name == null) charset_name = "ISO8859_1"; - byte chars[] = s.getBytes(charset_name); - Update(chars, chars.length); - } - - /** - * Update buffer with a single integer (only & 0xff part is used, - * as a byte) - * - * @param i Integer value, which is then converted to byte as i & 0xff - **/ - public void Update (int i) { - Update((byte) (i & 0xff)); - } - - private byte[] Encode (int input[], int len) { - int i, j; - byte out[]; - - out = new byte[len]; - - for (i = j = 0; j < len; i++, j += 4) { - out[j] = (byte) (input[i] & 0xff); - out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); - out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); - out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); - } - - return out; - } - - /** - * Returns array of bytes (16 bytes) representing hash as of the - * current state of this object. Note: getting a hash does not - * invalidate the hash object, it only creates a copy of the real - * state which is finalized. - * - * @return Array of 16 bytes, the hash of all updated bytes - **/ - public synchronized byte[] Final () { - byte bits[]; - int index, padlen; - MD5State fin; - - if (finals == null) { - fin = new MD5State(state); - - int[] count_ints = {(int) (fin.count << 3), (int) (fin.count >> 29)}; - bits = Encode(count_ints, 8); - - index = (int) (fin.count & 0x3f); - padlen = (index < 56) ? (56 - index) : (120 - index); - - Update(fin, padding, 0, padlen); - Update(fin, bits, 0, 8); - - /* Update() sets finals to null */ - finals = fin; - } - - return Encode(finals.state, 16); - } - - private static final char[] HEX_CHARS = {'0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f',}; - - /** - * Turns array of bytes into string representing each byte as - * unsigned hex number. - * - * @param hash Array of bytes to convert to hex-string - * @return Generated hex string - */ - public static String asHex (byte hash[]) { - char buf[] = new char[hash.length * 2]; - for (int i = 0, x = 0; i < hash.length; i++) { - buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf]; - buf[x++] = HEX_CHARS[hash[i] & 0xf]; - } - return new String(buf); - } - - /** - * Returns 32-character hex representation of this objects hash - * - * @return String of this object's hash - */ - public String asHex () { - return asHex(this.Final()); - } - - /** - * Convenience method for initNativeLibrary(false). - **/ - public static synchronized final boolean initNativeLibrary () { - return initNativeLibrary(false); - } - - /** - * Attempts to initialize native library support. If - * 'disallow_lib_loading' is true, will indicate that the native - * library should not be loaded now or in the future. If native - * library support has been previously loaded or disabled, this - * method has no effect. - * - * @return true iff native library support has been loaded - **/ - public static synchronized final boolean initNativeLibrary (boolean disallow_lib_loading) { - if (disallow_lib_loading) { - native_lib_init_pending = false; - } else { - _initNativeLibrary(); - } - return native_lib_loaded; - } - - private static synchronized final void _initNativeLibrary () { - if (!native_lib_init_pending) return; - native_lib_loaded = _loadNativeLibrary(); - native_lib_init_pending = false; - } - - private static synchronized final boolean _loadNativeLibrary () { - try { - - // don't try to load if the right property is set - - String prop = System.getProperty("com.twmacinta.util.MD5.NO_NATIVE_LIB"); - if (prop != null) { - prop = prop.trim(); - if (prop.equalsIgnoreCase("true") || prop.equals("1")) return false; - } - - // the library to load can be specified as a property - - File f; - prop = System.getProperty("com.twmacinta.util.MD5.NATIVE_LIB_FILE"); - if (prop != null) { - f = new File(prop); - if (f.canRead()) { - System.load(f.getAbsolutePath()); - return true; - } - } - - // determine the operating system and architecture - - String os_name = System.getProperty("os.name"); - String os_arch = System.getProperty("os.arch"); - if (os_name == null || os_arch == null) return false; - os_name = os_name.toLowerCase(); - os_arch = os_arch.toLowerCase(); - - // define settings which are OS arch architecture independent - - File arch_lib_path = null; - String arch_libfile_suffix = null; - - // fill in settings for Linux on x86 - - if (os_name.equals("linux") && - (os_arch.equals("x86") || - os_arch.equals("i386") || - os_arch.equals("i486") || - os_arch.equals("i586") || - os_arch.equals("i686"))) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_x86"); - arch_libfile_suffix = ".so"; - } - - // fill in settings for Linux on amd64 - - else if (os_name.equals("linux") && - os_arch.equals("amd64")) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_amd64"); - arch_libfile_suffix = ".so"; - } - - // fill in settings for Windows on x86 - - else if (os_name.startsWith("windows ") && - (os_arch.equals("x86") || - os_arch.equals("i386") || - os_arch.equals("i486") || - os_arch.equals("i586") || - os_arch.equals("i686"))) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "win32_x86"); - arch_libfile_suffix = ".dll"; - } - - // fill in settings for Windows on amd64 - - else if (os_name.startsWith("windows ") && - os_arch.equals("amd64")) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "win_amd64"); - arch_libfile_suffix = ".dll"; - } - - // fill in settings for Mac OS X on PPC - - else if (os_name.startsWith("mac os x") && - (os_arch.equals("ppc"))) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_ppc"); - arch_libfile_suffix = ".jnilib"; - } - - // fill in settings for Mac OS X on x86 - - else if (os_name.startsWith("mac os x") && - (os_arch.equals("x86") || - os_arch.equals("i386") || - os_arch.equals("i486") || - os_arch.equals("i586") || - os_arch.equals("i686"))) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86"); - arch_libfile_suffix = ".jnilib"; - } - - // fill in settings for Mac OS X on x86_64 - - else if (os_name.startsWith("mac os x") && - os_arch.equals("x86_64")) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86_64"); - arch_libfile_suffix = ".jnilib"; - } - - // fill in settings for FreeBSD on x86 - - else if (os_name.equals("freebsd") && - (os_arch.equals("x86") || - os_arch.equals("i386") || - os_arch.equals("i486") || - os_arch.equals("i586") || - os_arch.equals("i686"))) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "freebsd_x86"); - arch_libfile_suffix = ".so"; - } - - // fill in settings for FreeBSD on amd64 - - else if (os_name.equals("freebsd") && - os_arch.equals("amd64")) { - arch_lib_path = new File(new File(new File("lib"), "arch"), "freebsd_amd64"); - arch_libfile_suffix = ".so"; - } - - // default to .so files with no architecture specific subdirectory - - else { - arch_libfile_suffix = ".so"; - } - - // build the required filename - - String fname = "MD5" + arch_libfile_suffix; - - // try the architecture specific directory - - if (arch_lib_path != null) { - f = new File(arch_lib_path, fname); - if (f.canRead()) { - System.load(f.getAbsolutePath()); - return true; - } - } - - // try the "lib" subdirectory - - f = new File(new File("lib"), fname); - if (f.canRead()) { - System.load(f.getAbsolutePath()); - return true; - } - - // try the working directory - - f = new File(fname); - if (f.canRead()) { - System.load(f.getAbsolutePath()); - return true; - } - } - - // discard SecurityExceptions - - catch (SecurityException e) {} - - // Intercept UnsatisfiedLinkError since the code will still - // work without the native method, but report it because it - // indicates that the architecture detection and/or support - // should be updated. - - catch (UnsatisfiedLinkError e) { - e.printStackTrace(); - } - - // unable to load - - return false; - } - - /** - * Calculates and returns the hash of the contents of the given file. - **/ - public static byte[] getHash (File f) throws IOException { - if (!f.exists()) throw new FileNotFoundException(f.toString()); - InputStream close_me = null; - try { - long buf_size = f.length(); - if (buf_size < 512) buf_size = 512; - if (buf_size > 65536) buf_size = 65536; - byte[] buf = new byte[(int) buf_size]; - MD5InputStream in = new MD5InputStream(new FileInputStream(f)); - close_me = in; - while (in.read(buf) != -1); - in.close(); - return in.hash(); - } catch (IOException e) { - if (close_me != null) try { close_me.close(); } catch (Exception e2) {} - throw e; - } - } - - /** - * @return true iff the first 16 bytes of both hash1 and hash2 are - * equal; both hash1 and hash2 are null; or either hash - * array is less than 16 bytes in length and their lengths and - * all of their bytes are equal. - **/ - public static boolean hashesEqual (byte[] hash1, byte[] hash2) { - if (hash1 == null) return hash2 == null; - if (hash2 == null) return false; - int targ = 16; - if (hash1.length < 16) { - if (hash2.length != hash1.length) return false; - targ = hash1.length; - } else if (hash2.length < 16) { - return false; - } - for (int i = 0; i < targ; i++) { - if (hash1[i] != hash2[i]) return false; - } - return true; - } - + + /** + * MD5 state + **/ + MD5State state; + + /** + * If Final() has been called, finals is set to the current finals state. Any Update() causes this to be set to null. + **/ + MD5State finals; + + /** + * Padding for Final() + **/ + static byte padding[] = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + private static boolean native_lib_loaded = false; + private static boolean native_lib_init_pending = true; + + /** + * Initialize MD5 internal state (object can be reused just by calling Init() after every Final() + **/ + public synchronized void Init() { + state = new MD5State(); + finals = null; + } + + /** + * Class constructor + **/ + public MD5() { + if (native_lib_init_pending) + _initNativeLibrary(); + this.Init(); + } + + /** + * Initialize class, and update hash with ob.toString() + * + * @param ob + * Object, ob.toString() is used to update hash after initialization + **/ + public MD5(Object ob) { + this(); + Update(ob.toString()); + } + + private void Decode(byte buffer[], int shift, int[] out) { + /* + * len += shift; for (int i = 0; shift < len; i++, shift += 4) { out[i] = ((int) (buffer[shift] & 0xff)) | (((int) + * (buffer[shift + 1] & 0xff)) << 8) | (((int) (buffer[shift + 2] & 0xff)) << 16) | (((int) buffer[shift + 3]) << 24); } + */ + + // unrolled loop (original loop shown above) + + out[0] = ((int) (buffer[shift] & 0xff)) | (((int) (buffer[shift + 1] & 0xff)) << 8) + | (((int) (buffer[shift + 2] & 0xff)) << 16) | (((int) buffer[shift + 3]) << 24); + out[1] = ((int) (buffer[shift + 4] & 0xff)) | (((int) (buffer[shift + 5] & 0xff)) << 8) + | (((int) (buffer[shift + 6] & 0xff)) << 16) | (((int) buffer[shift + 7]) << 24); + out[2] = ((int) (buffer[shift + 8] & 0xff)) | (((int) (buffer[shift + 9] & 0xff)) << 8) + | (((int) (buffer[shift + 10] & 0xff)) << 16) | (((int) buffer[shift + 11]) << 24); + out[3] = ((int) (buffer[shift + 12] & 0xff)) | (((int) (buffer[shift + 13] & 0xff)) << 8) + | (((int) (buffer[shift + 14] & 0xff)) << 16) | (((int) buffer[shift + 15]) << 24); + out[4] = ((int) (buffer[shift + 16] & 0xff)) | (((int) (buffer[shift + 17] & 0xff)) << 8) + | (((int) (buffer[shift + 18] & 0xff)) << 16) | (((int) buffer[shift + 19]) << 24); + out[5] = ((int) (buffer[shift + 20] & 0xff)) | (((int) (buffer[shift + 21] & 0xff)) << 8) + | (((int) (buffer[shift + 22] & 0xff)) << 16) | (((int) buffer[shift + 23]) << 24); + out[6] = ((int) (buffer[shift + 24] & 0xff)) | (((int) (buffer[shift + 25] & 0xff)) << 8) + | (((int) (buffer[shift + 26] & 0xff)) << 16) | (((int) buffer[shift + 27]) << 24); + out[7] = ((int) (buffer[shift + 28] & 0xff)) | (((int) (buffer[shift + 29] & 0xff)) << 8) + | (((int) (buffer[shift + 30] & 0xff)) << 16) | (((int) buffer[shift + 31]) << 24); + out[8] = ((int) (buffer[shift + 32] & 0xff)) | (((int) (buffer[shift + 33] & 0xff)) << 8) + | (((int) (buffer[shift + 34] & 0xff)) << 16) | (((int) buffer[shift + 35]) << 24); + out[9] = ((int) (buffer[shift + 36] & 0xff)) | (((int) (buffer[shift + 37] & 0xff)) << 8) + | (((int) (buffer[shift + 38] & 0xff)) << 16) | (((int) buffer[shift + 39]) << 24); + out[10] = ((int) (buffer[shift + 40] & 0xff)) | (((int) (buffer[shift + 41] & 0xff)) << 8) + | (((int) (buffer[shift + 42] & 0xff)) << 16) | (((int) buffer[shift + 43]) << 24); + out[11] = ((int) (buffer[shift + 44] & 0xff)) | (((int) (buffer[shift + 45] & 0xff)) << 8) + | (((int) (buffer[shift + 46] & 0xff)) << 16) | (((int) buffer[shift + 47]) << 24); + out[12] = ((int) (buffer[shift + 48] & 0xff)) | (((int) (buffer[shift + 49] & 0xff)) << 8) + | (((int) (buffer[shift + 50] & 0xff)) << 16) | (((int) buffer[shift + 51]) << 24); + out[13] = ((int) (buffer[shift + 52] & 0xff)) | (((int) (buffer[shift + 53] & 0xff)) << 8) + | (((int) (buffer[shift + 54] & 0xff)) << 16) | (((int) buffer[shift + 55]) << 24); + out[14] = ((int) (buffer[shift + 56] & 0xff)) | (((int) (buffer[shift + 57] & 0xff)) << 8) + | (((int) (buffer[shift + 58] & 0xff)) << 16) | (((int) buffer[shift + 59]) << 24); + out[15] = ((int) (buffer[shift + 60] & 0xff)) | (((int) (buffer[shift + 61] & 0xff)) << 8) + | (((int) (buffer[shift + 62] & 0xff)) << 16) | (((int) buffer[shift + 63]) << 24); + } + + private native void Transform_native(int[] state, byte buffer[], int shift, int length); + + private void Transform(MD5State state, byte buffer[], int shift, int[] decode_buf) { + int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[] = decode_buf; + + Decode(buffer, shift, decode_buf); + + /* Round 1 */ + a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; /* 1 */ + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[1] + 0xe8c7b756; /* 2 */ + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[2] + 0x242070db; /* 3 */ + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; /* 4 */ + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; /* 5 */ + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[5] + 0x4787c62a; /* 6 */ + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[6] + 0xa8304613; /* 7 */ + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; /* 8 */ + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; /* 9 */ + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[9] + 0x8b44f7af; /* 10 */ + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; /* 11 */ + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */ + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */ + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; /* 14 */ + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; /* 15 */ + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */ + b = ((b << 22) | (b >>> 10)) + c; + + /* Round 2 */ + a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; /* 17 */ + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[6] + 0xc040b340; /* 18 */ + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; /* 19 */ + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; /* 20 */ + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; /* 21 */ + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; /* 22 */ + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; /* 23 */ + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; /* 24 */ + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; /* 25 */ + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; /* 26 */ + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[3] + 0xf4d50d87; /* 27 */ + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; /* 28 */ + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */ + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[2] + 0xfcefa3f8; /* 30 */ + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[7] + 0x676f02d9; /* 31 */ + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */ + b = ((b << 20) | (b >>> 12)) + c; + + /* Round 3 */ + a += (b ^ c ^ d) + x[5] + 0xfffa3942; /* 33 */ + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[8] + 0x8771f681; /* 34 */ + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[11] + 0x6d9d6122; /* 35 */ + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[14] + 0xfde5380c; /* 36 */ + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[1] + 0xa4beea44; /* 37 */ + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[4] + 0x4bdecfa9; /* 38 */ + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[7] + 0xf6bb4b60; /* 39 */ + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[10] + 0xbebfbc70; /* 40 */ + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[13] + 0x289b7ec6; /* 41 */ + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[0] + 0xeaa127fa; /* 42 */ + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[3] + 0xd4ef3085; /* 43 */ + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[6] + 0x04881d05; /* 44 */ + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[9] + 0xd9d4d039; /* 33 */ + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[12] + 0xe6db99e5; /* 34 */ + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; /* 35 */ + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[2] + 0xc4ac5665; /* 36 */ + b = ((b << 23) | (b >>> 9)) + c; + + /* Round 4 */ + a += (c ^ (b | ~d)) + x[0] + 0xf4292244; /* 49 */ + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[7] + 0x432aff97; /* 50 */ + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; /* 51 */ + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; /* 52 */ + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; /* 53 */ + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[3] + 0x8f0ccc92; /* 54 */ + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; /* 55 */ + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; /* 56 */ + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; /* 57 */ + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; /* 58 */ + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[6] + 0xa3014314; /* 59 */ + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; /* 60 */ + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; /* 61 */ + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; /* 62 */ + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[2] + 0x2ad7d2bb; /* 63 */ + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; /* 64 */ + b = ((b << 21) | (b >>> 11)) + c; + + state.state[0] += a; + state.state[1] += b; + state.state[2] += c; + state.state[3] += d; + } + + /** + * Updates hash with the bytebuffer given (using at maximum length bytes from that buffer) + * + * @param stat + * Which state is updated + * @param buffer + * Array of bytes to be hashed + * @param offset + * Offset to buffer array + * @param length + * Use at maximum `length' bytes (absolute maximum is buffer.length) + */ + public void Update(MD5State stat, byte buffer[], int offset, int length) { + int index, partlen, i, start; + finals = null; + + /* Length can be told to be shorter, but not inter */ + if ((length - offset) > buffer.length) + length = buffer.length - offset; + + /* compute number of bytes mod 64 */ + + index = (int) (stat.count & 0x3f); + stat.count += length; + + partlen = 64 - index; + + if (length >= partlen) { + + // update state (using native method) to reflect input + + if (native_lib_loaded) { + if (partlen == 64) { + partlen = 0; + } else { + for (i = 0; i < partlen; i++) + stat.buffer[i + index] = buffer[i + offset]; + Transform_native(stat.state, stat.buffer, 0, 64); + } + i = partlen + ((length - partlen) / 64) * 64; + + // break into chunks to guard against stack overflow in JNI + + int transformLength = length - partlen; + int transformOffset = partlen + offset; + final int MAX_LENGTH = 65536; // prevent stack overflow in JNI + while (true) { + if (transformLength > MAX_LENGTH) { + Transform_native(stat.state, buffer, transformOffset, MAX_LENGTH); + transformLength -= MAX_LENGTH; + transformOffset += MAX_LENGTH; + } else { + Transform_native(stat.state, buffer, transformOffset, transformLength); + break; + } + } + } + + // update state (using only Java) to reflect input + + else { + int[] decode_buf = new int[16]; + if (partlen == 64) { + partlen = 0; + } else { + for (i = 0; i < partlen; i++) + stat.buffer[i + index] = buffer[i + offset]; + Transform(stat, stat.buffer, 0, decode_buf); + } + for (i = partlen; (i + 63) < length; i += 64) { + Transform(stat, buffer, i + offset, decode_buf); + } + } + index = 0; + } else { + i = 0; + } + + /* buffer remaining input */ + if (i < length) { + start = i; + for (; i < length; i++) { + stat.buffer[index + i - start] = buffer[i + offset]; + } + } + } + + /* + * Update()s for other datatypes than byte[] also. Update(byte[], int) is only the main driver. + */ + + /** + * Plain update, updates this object + **/ + public void Update(byte buffer[], int offset, int length) { + Update(this.state, buffer, offset, length); + } + + public void Update(byte buffer[], int length) { + Update(this.state, buffer, 0, length); + } + + /** + * Updates hash with given array of bytes + * + * @param buffer + * Array of bytes to use for updating the hash + **/ + public void Update(byte buffer[]) { + Update(buffer, 0, buffer.length); + } + + /** + * Updates hash with a single byte + * + * @param b + * Single byte to update the hash + **/ + public void Update(byte b) { + byte buffer[] = new byte[1]; + buffer[0] = b; + + Update(buffer, 1); + } + + /** + * Update buffer with given string. Note that because the version of the s.getBytes() method without parameters is used to + * convert the string to a byte array, the results of this method may be different on different platforms. The s.getBytes() + * method converts the string into a byte array using the current platform's default character set and may therefore have + * different results on platforms with different default character sets. If a version that works consistently across platforms + * with different default character sets is desired, use the overloaded version of the Update() method which takes a string + * and a character encoding. + * + * @param s + * String to be update to hash (is used as s.getBytes()) + **/ + public void Update(String s) { + byte chars[] = s.getBytes(); + Update(chars, chars.length); + } + + /** + * Update buffer with given string using the given encoding. If the given encoding is null, the encoding "ISO8859_1" is used. + * + * @param s + * String to be update to hash (is used as s.getBytes(charset_name)) + * @param charset_name + * The character set to use to convert s to a byte array, or null if the "ISO8859_1" character set is desired. + * @exception java.io.UnsupportedEncodingException + * If the named charset is not supported. + **/ + public void Update(String s, String charset_name) throws java.io.UnsupportedEncodingException { + if (charset_name == null) + charset_name = "ISO8859_1"; + byte chars[] = s.getBytes(charset_name); + Update(chars, chars.length); + } + + /** + * Update buffer with a single integer (only & 0xff part is used, as a byte) + * + * @param i + * Integer value, which is then converted to byte as i & 0xff + **/ + public void Update(int i) { + Update((byte) (i & 0xff)); + } + + private byte[] Encode(int input[], int len) { + int i, j; + byte out[]; + + out = new byte[len]; + + for (i = j = 0; j < len; i++, j += 4) { + out[j] = (byte) (input[i] & 0xff); + out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); + out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); + out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); + } + + return out; + } + + /** + * Returns array of bytes (16 bytes) representing hash as of the current state of this object. Note: getting a hash does not + * invalidate the hash object, it only creates a copy of the real state which is finalized. + * + * @return Array of 16 bytes, the hash of all updated bytes + **/ + public synchronized byte[] Final() { + byte bits[]; + int index, padlen; + MD5State fin; + + if (finals == null) { + fin = new MD5State(state); + + int[] count_ints = { (int) (fin.count << 3), (int) (fin.count >> 29) }; + bits = Encode(count_ints, 8); + + index = (int) (fin.count & 0x3f); + padlen = (index < 56) ? (56 - index) : (120 - index); + + Update(fin, padding, 0, padlen); + Update(fin, bits, 0, 8); + + /* Update() sets finals to null */ + finals = fin; + } + + return Encode(finals.state, 16); + } + + private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; + + /** + * Turns array of bytes into string representing each byte as unsigned hex number. + * + * @param hash + * Array of bytes to convert to hex-string + * @return Generated hex string + */ + public static String asHex(byte hash[]) { + char buf[] = new char[hash.length * 2]; + for (int i = 0, x = 0; i < hash.length; i++) { + buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf]; + buf[x++] = HEX_CHARS[hash[i] & 0xf]; + } + return new String(buf); + } + + /** + * Returns 32-character hex representation of this objects hash + * + * @return String of this object's hash + */ + public String asHex() { + return asHex(this.Final()); + } + + /** + * Convenience method for initNativeLibrary(false). + **/ + public static synchronized final boolean initNativeLibrary() { + return initNativeLibrary(false); + } + + /** + * Attempts to initialize native library support. If 'disallow_lib_loading' is true, will indicate that the native library + * should not be loaded now or in the future. If native library support has been previously loaded or disabled, this method + * has no effect. + * + * @return true iff native library support has been loaded + **/ + public static synchronized final boolean initNativeLibrary(boolean disallow_lib_loading) { + if (disallow_lib_loading) { + native_lib_init_pending = false; + } else { + _initNativeLibrary(); + } + return native_lib_loaded; + } + + private static synchronized final void _initNativeLibrary() { + if (!native_lib_init_pending) + return; + native_lib_loaded = _loadNativeLibrary(); + native_lib_init_pending = false; + } + + private static synchronized final boolean _loadNativeLibrary() { + try { + + // don't try to load if the right property is set + + String prop = System.getProperty("com.twmacinta.util.MD5.NO_NATIVE_LIB"); + if (prop != null) { + prop = prop.trim(); + if (prop.equalsIgnoreCase("true") || prop.equals("1")) + return false; + } + + // the library to load can be specified as a property + + File f; + prop = System.getProperty("com.twmacinta.util.MD5.NATIVE_LIB_FILE"); + if (prop != null) { + f = new File(prop); + if (f.canRead()) { + System.load(f.getAbsolutePath()); + return true; + } + } + + // determine the operating system and architecture + + String os_name = System.getProperty("os.name"); + String os_arch = System.getProperty("os.arch"); + if (os_name == null || os_arch == null) + return false; + os_name = os_name.toLowerCase(); + os_arch = os_arch.toLowerCase(); + + // define settings which are OS arch architecture independent + + File arch_lib_path = null; + String arch_libfile_suffix = null; + + // fill in settings for Linux on x86 + + if (os_name.equals("linux") + && (os_arch.equals("x86") || os_arch.equals("i386") || os_arch.equals("i486") || os_arch.equals("i586") || os_arch + .equals("i686"))) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_x86"); + arch_libfile_suffix = ".so"; + } + + // fill in settings for Linux on amd64 + + else if (os_name.equals("linux") && os_arch.equals("amd64")) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_amd64"); + arch_libfile_suffix = ".so"; + } + + // fill in settings for Windows on x86 + + else if (os_name.startsWith("windows ") + && (os_arch.equals("x86") || os_arch.equals("i386") || os_arch.equals("i486") || os_arch.equals("i586") || os_arch + .equals("i686"))) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "win32_x86"); + arch_libfile_suffix = ".dll"; + } + + // fill in settings for Windows on amd64 + + else if (os_name.startsWith("windows ") && os_arch.equals("amd64")) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "win_amd64"); + arch_libfile_suffix = ".dll"; + } + + // fill in settings for Mac OS X on PPC + + else if (os_name.startsWith("mac os x") && (os_arch.equals("ppc"))) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_ppc"); + arch_libfile_suffix = ".jnilib"; + } + + // fill in settings for Mac OS X on x86 + + else if (os_name.startsWith("mac os x") + && (os_arch.equals("x86") || os_arch.equals("i386") || os_arch.equals("i486") || os_arch.equals("i586") || os_arch + .equals("i686"))) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86"); + arch_libfile_suffix = ".jnilib"; + } + + // fill in settings for Mac OS X on x86_64 + + else if (os_name.startsWith("mac os x") && os_arch.equals("x86_64")) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86_64"); + arch_libfile_suffix = ".jnilib"; + } + + // fill in settings for FreeBSD on x86 + + else if (os_name.equals("freebsd") + && (os_arch.equals("x86") || os_arch.equals("i386") || os_arch.equals("i486") || os_arch.equals("i586") || os_arch + .equals("i686"))) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "freebsd_x86"); + arch_libfile_suffix = ".so"; + } + + // fill in settings for FreeBSD on amd64 + + else if (os_name.equals("freebsd") && os_arch.equals("amd64")) { + arch_lib_path = new File(new File(new File("lib"), "arch"), "freebsd_amd64"); + arch_libfile_suffix = ".so"; + } + + // default to .so files with no architecture specific subdirectory + + else { + arch_libfile_suffix = ".so"; + } + + // build the required filename + + String fname = "MD5" + arch_libfile_suffix; + + // try the architecture specific directory + + if (arch_lib_path != null) { + f = new File(arch_lib_path, fname); + if (f.canRead()) { + System.load(f.getAbsolutePath()); + return true; + } + } + + // try the "lib" subdirectory + + f = new File(new File("lib"), fname); + if (f.canRead()) { + System.load(f.getAbsolutePath()); + return true; + } + + // try the working directory + + f = new File(fname); + if (f.canRead()) { + System.load(f.getAbsolutePath()); + return true; + } + } + + // discard SecurityExceptions + + catch (SecurityException e) { + } + + // Intercept UnsatisfiedLinkError since the code will still + // work without the native method, but report it because it + // indicates that the architecture detection and/or support + // should be updated. + + catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + } + + // unable to load + + return false; + } + + /** + * Calculates and returns the hash of the contents of the given file. + **/ + public static byte[] getHash(File f) throws IOException { + if (!f.exists()) + throw new FileNotFoundException(f.toString()); + InputStream close_me = null; + try { + long buf_size = f.length(); + if (buf_size < 512) + buf_size = 512; + if (buf_size > 65536) + buf_size = 65536; + byte[] buf = new byte[(int) buf_size]; + MD5InputStream in = new MD5InputStream(new FileInputStream(f)); + close_me = in; + while (in.read(buf) != -1) + ; + in.close(); + return in.hash(); + } catch (IOException e) { + if (close_me != null) + try { + close_me.close(); + } catch (Exception e2) { + } + throw e; + } + } + + /** + * @return true iff the first 16 bytes of both hash1 and hash2 are equal; both hash1 and hash2 are null; or either hash array + * is less than 16 bytes in length and their lengths and all of their bytes are equal. + **/ + public static boolean hashesEqual(byte[] hash1, byte[] hash2) { + if (hash1 == null) + return hash2 == null; + if (hash2 == null) + return false; + int targ = 16; + if (hash1.length < 16) { + if (hash2.length != hash1.length) + return false; + targ = hash1.length; + } else if (hash2.length < 16) { + return false; + } + for (int i = 0; i < targ; i++) { + if (hash1[i] != hash2[i]) + return false; + } + return true; + } + } diff --git a/marytts-common/src/main/java/com/twmacinta/util/MD5InputStream.java b/marytts-common/src/main/java/com/twmacinta/util/MD5InputStream.java index e121671d..b18354e3 100644 --- a/marytts-common/src/main/java/com/twmacinta/util/MD5InputStream.java +++ b/marytts-common/src/main/java/com/twmacinta/util/MD5InputStream.java @@ -6,168 +6,160 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -/** - * MD5InputStream, a subclass of FilterInputStream implementing MD5 - * functionality on a stream. +/** + * MD5InputStream, a subclass of FilterInputStream implementing MD5 functionality on a stream. *

* Originally written by Santeri Paavolainen, Helsinki Finland 1996
* (c) Santeri Paavolainen, Helsinki Finland 1996
* Some changes Copyright (c) 2002 Timothy W Macinta
*

- * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. *

- * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free + * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *

- * See http://www.twmacinta.com/myjava/fast_md5.php for more information - * on this file. + * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file. *

- * Please note: I (Timothy Macinta) have put this code in the - * com.twmacinta.util package only because it came without a package. I - * was not the the original author of the code, although I did - * optimize it (substantially) and fix some bugs. - * - * @author Santeri Paavolainen - * @author Timothy W Macinta (twm@alum.mit.edu) (added main() method) + * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I + * was not the the original author of the code, although I did optimize it (substantially) and fix some bugs. + * + * @author Santeri Paavolainen + * @author Timothy W Macinta (twm@alum.mit.edu) (added main() method) **/ - public class MD5InputStream extends FilterInputStream { - /** - * MD5 context - */ - private MD5 md5; - - /** - * Creates a MD5InputStream - * @param in The input stream - */ - public MD5InputStream (InputStream in) { - super(in); + /** + * MD5 context + */ + private MD5 md5; - md5 = new MD5(); - } + /** + * Creates a MD5InputStream + * + * @param in + * The input stream + */ + public MD5InputStream(InputStream in) { + super(in); - /** - * Read a byte of data. - * @see java.io.FilterInputStream - */ - public int read() throws IOException { - int c = in.read(); - - if (c == -1) - return -1; - - if ((c & ~0xff) != 0) { - System.out.println("MD5InputStream.read() got character with (c & ~0xff) != 0)!"); - } else { - md5.Update(c); - } - - return c; - } - - /** - * Reads into an array of bytes. - * - * @see java.io.FilterInputStream - */ - public int read (byte bytes[], int offset, int length) throws IOException { - int r; - - if ((r = in.read(bytes, offset, length)) == -1) - return r; - - md5.Update(bytes, offset, r); - - return r; - } - - /** - * Returns array of bytes representing hash of the stream as - * finalized for the current state. - * @see MD5#Final - */ - public byte[] hash () { - return md5.Final(); - } - - public MD5 getMD5() { - return md5; - } - - /** - * This method is here for testing purposes only - do not rely - * on it being here. - **/ - public static void main(String[] arg) { - try { - - //////////////////////////////////////////////////////////////// - // - // usage: java com.twmacinta.util.MD5InputStream [--use-default-md5] [--no-native-lib] filename - // - ///////// - - // determine the filename to use and the MD5 impelementation to use - - String filename = arg[arg.length-1]; - boolean use_default_md5 = false; - boolean use_native_lib = true; - for (int i = 0; i < arg.length-1; i++) { - if (arg[i].equals("--use-default-md5")) { - use_default_md5 = true; - } else if (arg[i].equals("--no-native-lib")) { - use_native_lib = false; - } - } - - // initialize common variables - - byte[] buf = new byte[65536]; - int num_read; - - // Use the default MD5 implementation that comes with Java - - if (use_default_md5) { - InputStream in = new BufferedInputStream(new FileInputStream(filename)); - java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); - while ((num_read = in.read(buf)) != -1) { - digest.update(buf, 0, num_read); - } - System.out.println(MD5.asHex(digest.digest())+" "+filename); - in.close(); - - // Use the optimized MD5 implementation - - } else { - - // disable the native library search, if requested - - if (!use_native_lib) { - MD5.initNativeLibrary(true); + md5 = new MD5(); } - // calculate the checksum + /** + * Read a byte of data. + * + * @see java.io.FilterInputStream + */ + public int read() throws IOException { + int c = in.read(); - MD5InputStream in = new MD5InputStream(new BufferedInputStream(new FileInputStream(filename))); - while ((num_read = in.read(buf)) != -1); - System.out.println(MD5.asHex(in.hash())+" "+filename); - in.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } + if (c == -1) + return -1; + + if ((c & ~0xff) != 0) { + System.out.println("MD5InputStream.read() got character with (c & ~0xff) != 0)!"); + } else { + md5.Update(c); + } + + return c; + } + + /** + * Reads into an array of bytes. + * + * @see java.io.FilterInputStream + */ + public int read(byte bytes[], int offset, int length) throws IOException { + int r; + + if ((r = in.read(bytes, offset, length)) == -1) + return r; + + md5.Update(bytes, offset, r); + + return r; + } + + /** + * Returns array of bytes representing hash of the stream as finalized for the current state. + * + * @see MD5#Final + */ + public byte[] hash() { + return md5.Final(); + } + + public MD5 getMD5() { + return md5; + } + + /** + * This method is here for testing purposes only - do not rely on it being here. + **/ + public static void main(String[] arg) { + try { + + // ////////////////////////////////////////////////////////////// + // + // usage: java com.twmacinta.util.MD5InputStream [--use-default-md5] [--no-native-lib] filename + // + // /////// + + // determine the filename to use and the MD5 impelementation to use + + String filename = arg[arg.length - 1]; + boolean use_default_md5 = false; + boolean use_native_lib = true; + for (int i = 0; i < arg.length - 1; i++) { + if (arg[i].equals("--use-default-md5")) { + use_default_md5 = true; + } else if (arg[i].equals("--no-native-lib")) { + use_native_lib = false; + } + } + + // initialize common variables + + byte[] buf = new byte[65536]; + int num_read; + + // Use the default MD5 implementation that comes with Java + + if (use_default_md5) { + InputStream in = new BufferedInputStream(new FileInputStream(filename)); + java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); + while ((num_read = in.read(buf)) != -1) { + digest.update(buf, 0, num_read); + } + System.out.println(MD5.asHex(digest.digest()) + " " + filename); + in.close(); + + // Use the optimized MD5 implementation + + } else { + + // disable the native library search, if requested + + if (!use_native_lib) { + MD5.initNativeLibrary(true); + } + + // calculate the checksum + + MD5InputStream in = new MD5InputStream(new BufferedInputStream(new FileInputStream(filename))); + while ((num_read = in.read(buf)) != -1) + ; + System.out.println(MD5.asHex(in.hash()) + " " + filename); + in.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } } - diff --git a/marytts-common/src/main/java/com/twmacinta/util/MD5OutputStream.java b/marytts-common/src/main/java/com/twmacinta/util/MD5OutputStream.java index 322ff5bc..84169544 100644 --- a/marytts-common/src/main/java/com/twmacinta/util/MD5OutputStream.java +++ b/marytts-common/src/main/java/com/twmacinta/util/MD5OutputStream.java @@ -7,116 +7,106 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -/** - * MD5OutputStream is a subclass of FilterOutputStream adding MD5 - * hashing of the output. +/** + * MD5OutputStream is a subclass of FilterOutputStream adding MD5 hashing of the output. *

* Originally written by Santeri Paavolainen, Helsinki Finland 1996
* (c) Santeri Paavolainen, Helsinki Finland 1996
* Some changes Copyright (c) 2002 Timothy W Macinta
*

- * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. *

- * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free + * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *

- * See http://www.twmacinta.com/myjava/fast_md5.php for more information - * on this file. + * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file. *

- * Please note: I (Timothy Macinta) have put this code in the - * com.twmacinta.util package only because it came without a package. I - * was not the the original author of the code, although I did - * optimize it (substantially) and fix some bugs. - * - * @author Santeri Paavolainen - * @author Timothy W Macinta (twm@alum.mit.edu) (added main() method) + * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I + * was not the the original author of the code, although I did optimize it (substantially) and fix some bugs. + * + * @author Santeri Paavolainen + * @author Timothy W Macinta (twm@alum.mit.edu) (added main() method) **/ public class MD5OutputStream extends FilterOutputStream { - /** - * MD5 context - */ - private MD5 md5; + /** + * MD5 context + */ + private MD5 md5; - /** - * Creates MD5OutputStream - * @param out The output stream - */ + /** + * Creates MD5OutputStream + * + * @param out + * The output stream + */ - public MD5OutputStream (OutputStream out) { - super(out); + public MD5OutputStream(OutputStream out) { + super(out); - md5 = new MD5(); - } + md5 = new MD5(); + } - /** - * Writes a byte. - * - * @see java.io.FilterOutputStream - */ + /** + * Writes a byte. + * + * @see java.io.FilterOutputStream + */ - public void write (int b) throws IOException { - out.write(b); - md5.Update((byte) b); - } + public void write(int b) throws IOException { + out.write(b); + md5.Update((byte) b); + } - /** - * Writes a sub array of bytes. - * - * @see java.io.FilterOutputStream - */ + /** + * Writes a sub array of bytes. + * + * @see java.io.FilterOutputStream + */ - public void write (byte b[], int off, int len) throws IOException { - out.write(b, off, len); - md5.Update(b, off, len); - } + public void write(byte b[], int off, int len) throws IOException { + out.write(b, off, len); + md5.Update(b, off, len); + } - /** - * Returns array of bytes representing hash of the stream as finalized - * for the current state. - * @see MD5#Final - */ + /** + * Returns array of bytes representing hash of the stream as finalized for the current state. + * + * @see MD5#Final + */ - public byte[] hash () { - return md5.Final(); - } + public byte[] hash() { + return md5.Final(); + } - public MD5 getMD5() { - return md5; - } - - /** - * This method is here for testing purposes only - do not rely - * on it being here. - **/ - public static void main(String[] arg) { - try { - MD5OutputStream out = new MD5OutputStream(new com.twmacinta.io.NullOutputStream()); - InputStream in = new BufferedInputStream(new FileInputStream(arg[0])); - byte[] buf = new byte[65536]; - int num_read; - long total_read = 0; - while ((num_read = in.read(buf)) != -1) { - total_read += num_read; - out.write(buf, 0, num_read); - } - System.out.println(MD5.asHex(out.hash())+" "+arg[0]); - in.close(); - out.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } + public MD5 getMD5() { + return md5; + } + /** + * This method is here for testing purposes only - do not rely on it being here. + **/ + public static void main(String[] arg) { + try { + MD5OutputStream out = new MD5OutputStream(new com.twmacinta.io.NullOutputStream()); + InputStream in = new BufferedInputStream(new FileInputStream(arg[0])); + byte[] buf = new byte[65536]; + int num_read; + long total_read = 0; + while ((num_read = in.read(buf)) != -1) { + total_read += num_read; + out.write(buf, 0, num_read); + } + System.out.println(MD5.asHex(out.hash()) + " " + arg[0]); + in.close(); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } } - diff --git a/marytts-common/src/main/java/com/twmacinta/util/MD5State.java b/marytts-common/src/main/java/com/twmacinta/util/MD5State.java index f8cb0415..2e596d10 100644 --- a/marytts-common/src/main/java/com/twmacinta/util/MD5State.java +++ b/marytts-common/src/main/java/com/twmacinta/util/MD5State.java @@ -6,74 +6,66 @@ package com.twmacinta.util; * (c) Santeri Paavolainen, Helsinki Finland 1996
* Some changes Copyright (c) 2002 Timothy W Macinta
*

- * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. *

- * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free + * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *

- * See http://www.twmacinta.com/myjava/fast_md5.php for more information - * on this file. + * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file. *

* Contains internal state of the MD5 class *

- * Please note: I (Timothy Macinta) have put this code in the - * com.twmacinta.util package only because it came without a package. I - * was not the the original author of the code, although I did - * optimize it (substantially) and fix some bugs. - * - * @author Santeri Paavolainen - * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes) + * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I + * was not the the original author of the code, although I did optimize it (substantially) and fix some bugs. + * + * @author Santeri Paavolainen + * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes) **/ class MD5State { - /** - * 128-bit state - */ - int state[]; - - /** - * 64-bit character count - */ - long count; - - /** - * 64-byte buffer (512 bits) for storing to-be-hashed characters - */ - byte buffer[]; + /** + * 128-bit state + */ + int state[]; - public MD5State() { - buffer = new byte[64]; - count = 0; - state = new int[4]; - - state[0] = 0x67452301; - state[1] = 0xefcdab89; - state[2] = 0x98badcfe; - state[3] = 0x10325476; + /** + * 64-bit character count + */ + long count; - } + /** + * 64-byte buffer (512 bits) for storing to-be-hashed characters + */ + byte buffer[]; - /** Create this State as a copy of another state */ - public MD5State (MD5State from) { - this(); - - int i; - - for (i = 0; i < buffer.length; i++) - this.buffer[i] = from.buffer[i]; - - for (i = 0; i < state.length; i++) - this.state[i] = from.state[i]; - - this.count = from.count; - } + public MD5State() { + buffer = new byte[64]; + count = 0; + state = new int[4]; + + state[0] = 0x67452301; + state[1] = 0xefcdab89; + state[2] = 0x98badcfe; + state[3] = 0x10325476; + + } + + /** Create this State as a copy of another state */ + public MD5State(MD5State from) { + this(); + + int i; + + for (i = 0; i < buffer.length; i++) + this.buffer[i] = from.buffer[i]; + + for (i = 0; i < state.length; i++) + this.state[i] = from.state[i]; + + this.count = from.count; + } }; diff --git a/marytts-common/src/main/java/marytts/Version.java b/marytts-common/src/main/java/marytts/Version.java index 9b041afc..a8a488d1 100644 --- a/marytts-common/src/main/java/marytts/Version.java +++ b/marytts-common/src/main/java/marytts/Version.java @@ -24,50 +24,48 @@ import java.io.InputStream; import marytts.util.io.FileUtils; - /** * Provide Version information for the Mary server and client. + * * @author Marc Schröder - * + * */ public class Version { - private static String specificationVersion; - private static String implementationVersion; + private static String specificationVersion; + private static String implementationVersion; - static { - InputStream specVersionStream = Version.class. - getResourceAsStream("specification-version.txt"); - if (specVersionStream != null) { - try { - specificationVersion = FileUtils.getStreamAsString(specVersionStream, "UTF-8").trim(); - } catch (IOException e) { - specificationVersion = "undeterminable"; - } - } else { - specificationVersion = "unknown"; - } - - InputStream implVersionStream = Version.class. - getResourceAsStream("implementation-version.txt"); - if (implVersionStream != null) { - try { - implementationVersion = FileUtils.getStreamAsString(implVersionStream, "UTF-8").trim(); - } catch (IOException e) { - implementationVersion = "undeterminable"; - } - } else { - implementationVersion = "unknown"; - } - } + static { + InputStream specVersionStream = Version.class.getResourceAsStream("specification-version.txt"); + if (specVersionStream != null) { + try { + specificationVersion = FileUtils.getStreamAsString(specVersionStream, "UTF-8").trim(); + } catch (IOException e) { + specificationVersion = "undeterminable"; + } + } else { + specificationVersion = "unknown"; + } - /** Specification version */ - public static String specificationVersion() { - return specificationVersion; - } - /** Implementation version */ - public static String implementationVersion() { - return implementationVersion; - } + InputStream implVersionStream = Version.class.getResourceAsStream("implementation-version.txt"); + if (implVersionStream != null) { + try { + implementationVersion = FileUtils.getStreamAsString(implVersionStream, "UTF-8").trim(); + } catch (IOException e) { + implementationVersion = "undeterminable"; + } + } else { + implementationVersion = "unknown"; + } + } + + /** Specification version */ + public static String specificationVersion() { + return specificationVersion; + } + + /** Implementation version */ + public static String implementationVersion() { + return implementationVersion; + } } - diff --git a/marytts-common/src/main/java/marytts/exceptions/ExecutionException.java b/marytts-common/src/main/java/marytts/exceptions/ExecutionException.java index f1bea6c8..0faf61d5 100644 --- a/marytts-common/src/main/java/marytts/exceptions/ExecutionException.java +++ b/marytts-common/src/main/java/marytts/exceptions/ExecutionException.java @@ -20,30 +20,27 @@ package marytts.exceptions; /** - * A special type of expected error conditions - * This class represents error conditions for external scripts - * such as Exceptions at runtime when processing fails + * A special type of expected error conditions This class represents error conditions for external scripts such as Exceptions at + * runtime when processing fails + * * @author sathish - * + * */ -public class ExecutionException extends Exception -{ - public ExecutionException() - { - super(); - } - public ExecutionException(String message) - { - super(message); - } - public ExecutionException(String message, Throwable cause) - { - super(message, cause); - } - public ExecutionException(Throwable cause) - { - super(cause); - } +public class ExecutionException extends Exception { + public ExecutionException() { + super(); + } + + public ExecutionException(String message) { + super(message); + } + + public ExecutionException(String message, Throwable cause) { + super(message, cause); + } + + public ExecutionException(Throwable cause) { + super(cause); + } } - diff --git a/marytts-common/src/main/java/marytts/exceptions/InvalidDataException.java b/marytts-common/src/main/java/marytts/exceptions/InvalidDataException.java index 3795068d..26d19270 100644 --- a/marytts-common/src/main/java/marytts/exceptions/InvalidDataException.java +++ b/marytts-common/src/main/java/marytts/exceptions/InvalidDataException.java @@ -5,8 +5,9 @@ package marytts.exceptions; /** * An exception class representing cases where data provided to a processing unit does not match the specifications. + * * @author marc - * + * */ public class InvalidDataException extends RuntimeException { diff --git a/marytts-common/src/main/java/marytts/exceptions/MaryConfigurationException.java b/marytts-common/src/main/java/marytts/exceptions/MaryConfigurationException.java index bd8dd658..43f176ff 100644 --- a/marytts-common/src/main/java/marytts/exceptions/MaryConfigurationException.java +++ b/marytts-common/src/main/java/marytts/exceptions/MaryConfigurationException.java @@ -20,40 +20,37 @@ package marytts.exceptions; /** - * A class representing severe expected error conditions, - * such as wrong format of data files needed to set up the system. - * Typically a MaryConfigurationException means it is impossible to continue operating. - * According to the fail-early strategy, it is preferable to throw MaryConfigurationException - * during server startup, and to abort the startup if one is thrown. + * A class representing severe expected error conditions, such as wrong format of data files needed to set up the system. + * Typically a MaryConfigurationException means it is impossible to continue operating. According to the fail-early strategy, it + * is preferable to throw MaryConfigurationException during server startup, and to abort the startup if one is thrown. + * * @author marc - * + * */ -public class MaryConfigurationException extends Exception -{ - /** - * Construct a MaryConfigurationException with only an error message. - * This constructor should only be used if our program code - * has identified the error condition. In order to wrap - * another Exception into a MaryConfigurationException with a - * meaningful error message, use {@link #MaryConfigurationException(String, Throwable)}. - * @param message a meaningful error message describing the problem. - */ - public MaryConfigurationException(String message) - { - super(message); - } +public class MaryConfigurationException extends Exception { + /** + * Construct a MaryConfigurationException with only an error message. This constructor should only be used if our program code + * has identified the error condition. In order to wrap another Exception into a MaryConfigurationException with a meaningful + * error message, use {@link #MaryConfigurationException(String, Throwable)}. + * + * @param message + * a meaningful error message describing the problem. + */ + public MaryConfigurationException(String message) { + super(message); + } - /** - * Create a MaryConfigurationException with a message and a cause. - * Use this to wrap another Exception into a MaryConfigurationException with a - * meaningful error message. - * @param message a meaningful error message describing the problem. - * @param cause the exception or error that caused the problem. - */ - public MaryConfigurationException(String message, Throwable cause) - { - super(message, cause); - } + /** + * Create a MaryConfigurationException with a message and a cause. Use this to wrap another Exception into a + * MaryConfigurationException with a meaningful error message. + * + * @param message + * a meaningful error message describing the problem. + * @param cause + * the exception or error that caused the problem. + */ + public MaryConfigurationException(String message, Throwable cause) { + super(message, cause); + } } - diff --git a/marytts-common/src/main/java/marytts/exceptions/NoSuchPropertyException.java b/marytts-common/src/main/java/marytts/exceptions/NoSuchPropertyException.java index 41623a80..1555dd87 100644 --- a/marytts-common/src/main/java/marytts/exceptions/NoSuchPropertyException.java +++ b/marytts-common/src/main/java/marytts/exceptions/NoSuchPropertyException.java @@ -21,16 +21,16 @@ package marytts.exceptions; /** * @author Marc Schröder - * - * Thrown by MaryProperties if a property is needed but cannot be found. + * + * Thrown by MaryProperties if a property is needed but cannot be found. */ public class NoSuchPropertyException extends RuntimeException { - public NoSuchPropertyException(String message) { - super(message); - } - public NoSuchPropertyException(String message, Throwable cause) { - super(message, cause); - } + public NoSuchPropertyException(String message) { + super(message); + } + + public NoSuchPropertyException(String message, Throwable cause) { + super(message, cause); + } } - diff --git a/marytts-common/src/main/java/marytts/exceptions/SynthesisException.java b/marytts-common/src/main/java/marytts/exceptions/SynthesisException.java index b116baba..ec06f0ac 100644 --- a/marytts-common/src/main/java/marytts/exceptions/SynthesisException.java +++ b/marytts-common/src/main/java/marytts/exceptions/SynthesisException.java @@ -19,24 +19,21 @@ */ package marytts.exceptions; -public class SynthesisException extends Exception -{ - public SynthesisException() - { - super(); - } - public SynthesisException(String message) - { - super(message); - } - public SynthesisException(String message, Throwable cause) - { - super(message, cause); - } - public SynthesisException(Throwable cause) - { - super(cause); - } +public class SynthesisException extends Exception { + public SynthesisException() { + super(); + } + + public SynthesisException(String message) { + super(message); + } + + public SynthesisException(String message, Throwable cause) { + super(message, cause); + } + + public SynthesisException(Throwable cause) { + super(cause); + } } - diff --git a/marytts-common/src/main/java/marytts/fst/FST.java b/marytts-common/src/main/java/marytts/fst/FST.java index 62702324..302d5a28 100644 --- a/marytts-common/src/main/java/marytts/fst/FST.java +++ b/marytts-common/src/main/java/marytts/fst/FST.java @@ -29,107 +29,111 @@ import java.nio.charset.Charset; import java.util.ArrayList; /** - * An implementation of a finite state transducer. This class does nothing but - * load and represent the FST. It is used by other classes doing something - * reasonable with it. + * An implementation of a finite state transducer. This class does nothing but load and represent the FST. It is used by other + * classes doing something reasonable with it. + * * @author Andreas Eisele */ -public class FST -{ - // The following variables are package-readable, so that they can be - // directly accessed by all classes in this package. - int[] targets; - short[] labels; - boolean[] isLast; - - short[] offsets; - byte[] bytes; - int[] mapping; - ArrayList strings=new ArrayList(); - - public FST(String fileName) throws IOException - { - FileInputStream fis = new FileInputStream(fileName); - try { - load(fis); - } finally { - fis.close(); - } - } +public class FST { + // The following variables are package-readable, so that they can be + // directly accessed by all classes in this package. + int[] targets; + short[] labels; + boolean[] isLast; - /** - * Load the fst from the given input stream. Assumes header. - * @param inStream - * @throws IOException - */ - public FST(InputStream inStream) throws IOException { - load(inStream); - } - + short[] offsets; + byte[] bytes; + int[] mapping; + ArrayList strings = new ArrayList(); + public FST(String fileName) throws IOException { + FileInputStream fis = new FileInputStream(fileName); + try { + load(fis); + } finally { + fis.close(); + } + } - /** - * Initialise the finite state transducer. Loads from headerless legacy file format. - * @param fileName the name of the file from which to load the FST. - * @param encoding the name of the encoding used in the file (e.g., UTF-8 - * or ISO-8859-1). - * @throws IOException if the FST cannot be loaded from the given file. - * @throws UnsupportedEncodingException if the encoding is not supported. - */ - public FST(String fileName, String encoding) - throws IOException, UnsupportedEncodingException - { - this(fileName, encoding, false); - } + /** + * Load the fst from the given input stream. Assumes header. + * + * @param inStream + * @throws IOException + */ + public FST(InputStream inStream) throws IOException { + load(inStream); + } - /** - * Initialise the finite state transducer. This constructor will - * assume that the file uses the system default encoding. - * @param fileName the name of the file from which to load the FST. - * @param verbose whether to write a report to stderr after loading. - * @throws IOException if the FST cannot be loaded from the given file. - */ - public FST(String fileName, boolean verbose) throws IOException - { - this(fileName, null, verbose); - } + /** + * Initialise the finite state transducer. Loads from headerless legacy file format. + * + * @param fileName + * the name of the file from which to load the FST. + * @param encoding + * the name of the encoding used in the file (e.g., UTF-8 or ISO-8859-1). + * @throws IOException + * if the FST cannot be loaded from the given file. + * @throws UnsupportedEncodingException + * if the encoding is not supported. + */ + public FST(String fileName, String encoding) throws IOException, UnsupportedEncodingException { + this(fileName, encoding, false); + } - /** - * Initialise the finite state transducer. - * @param fileName the name of the file from which to load the FST. - * @param encoding the name of the encoding used in the file (e.g., UTF-8 - * or ISO-8859-1). - * - * This constructor is to be used for old FST-files where the encoding was - * not yet specified in the header. - * - * @param verbose whether to write a report to stderr after loading. - * @throws IOException if the FST cannot be loaded from the given file. - * @throws UnsupportedEncodingException if the encoding is not supported. - */ - public FST(String fileName, String encoding, boolean verbose) - throws IOException, UnsupportedEncodingException - { - FileInputStream fis = new FileInputStream(fileName); - try { - loadHeaderless(fis, encoding, verbose); - } finally { - fis.close(); - } - } - - /** - * Load the fst from the given input stream. Assumes headerless legacy file format. - * @param inStream - * @param encoding - * @throws IOException - * @throws UnsupportedEncodingException - */ - public FST(InputStream inStream, String encoding) throws IOException, UnsupportedEncodingException { - loadHeaderless(inStream, encoding, false); - } - - private void load(InputStream inStream) + /** + * Initialise the finite state transducer. This constructor will assume that the file uses the system default encoding. + * + * @param fileName + * the name of the file from which to load the FST. + * @param verbose + * whether to write a report to stderr after loading. + * @throws IOException + * if the FST cannot be loaded from the given file. + */ + public FST(String fileName, boolean verbose) throws IOException { + this(fileName, null, verbose); + } + + /** + * Initialise the finite state transducer. + * + * @param fileName + * the name of the file from which to load the FST. + * @param encoding + * the name of the encoding used in the file (e.g., UTF-8 or ISO-8859-1). + * + * This constructor is to be used for old FST-files where the encoding was not yet specified in the header. + * + * @param verbose + * whether to write a report to stderr after loading. + * @throws IOException + * if the FST cannot be loaded from the given file. + * @throws UnsupportedEncodingException + * if the encoding is not supported. + */ + public FST(String fileName, String encoding, boolean verbose) throws IOException, UnsupportedEncodingException { + FileInputStream fis = new FileInputStream(fileName); + try { + loadHeaderless(fis, encoding, verbose); + } finally { + fis.close(); + } + } + + /** + * Load the fst from the given input stream. Assumes headerless legacy file format. + * + * @param inStream + * @param encoding + * @throws IOException + * @throws UnsupportedEncodingException + */ + public FST(InputStream inStream, String encoding) throws IOException, UnsupportedEncodingException { + loadHeaderless(inStream, encoding, false); + } + + private void load(InputStream inStream) throws IOException, UnsupportedEncodingException { int i; @@ -188,73 +192,64 @@ public class FST createMapping(mapping, bytes, encoding); } - private void loadHeaderless(InputStream inStream, String encoding, boolean verbose) - throws IOException, UnsupportedEncodingException - { - int i; - DataInputStream in = new DataInputStream(new BufferedInputStream(inStream)); - //int fileSize= (int) f.length(); - int fileSize = in.available(); // TODO: how robust is this?? - int nArcs=in.readInt(); - // arcs = new int[nArcs]; - - targets = new int[nArcs]; - labels = new short[nArcs]; - isLast = new boolean[nArcs]; - - for(i=0; i>20) & 2047); - isLast[i]=((byte)(thisArc >> 31))!=0; - - } - - int nPairs=in.readInt(); - offsets = new short[2*nPairs]; - for(i=0; i<2*nPairs; i++) - offsets[i] = in.readShort(); - int nBytes = fileSize - 8 - 4 * (nPairs + nArcs); - mapping=new int[nBytes]; - bytes = new byte[nBytes]; - in.readFully(bytes); - if(verbose) { - System.err.println("FST (" - + fileSize + " Bytes, " - + nArcs + " Arcs, " - + nPairs + " Labels)" - + " loaded"); - } - in.close(); - createMapping(mapping, bytes, encoding); - } - - private void createMapping(int[] mapping, byte[] bytes, String encoding) - throws UnsupportedEncodingException - { - mapping[0]=0; - int last0=-1; - String s; - int len; - for (int i=0;i> 20) & 2047); + isLast[i] = ((byte) (thisArc >> 31)) != 0; + + } + + int nPairs = in.readInt(); + offsets = new short[2 * nPairs]; + for (i = 0; i < 2 * nPairs; i++) + offsets[i] = in.readShort(); + int nBytes = fileSize - 8 - 4 * (nPairs + nArcs); + mapping = new int[nBytes]; + bytes = new byte[nBytes]; + in.readFully(bytes); + if (verbose) { + System.err.println("FST (" + fileSize + " Bytes, " + nArcs + " Arcs, " + nPairs + " Labels)" + " loaded"); + } + in.close(); + createMapping(mapping, bytes, encoding); + } + + private void createMapping(int[] mapping, byte[] bytes, String encoding) throws UnsupportedEncodingException { + mapping[0] = 0; + int last0 = -1; + String s; + int len; + for (int i = 0; i < bytes.length; i++) { + if (bytes[i] == 0) { + len = i - last0 - 1; + if (len == 0) + strings.add(""); + else { + String str; + if (encoding != null) + str = new String(bytes, last0 + 1, len, encoding); + else + str = new String(bytes, last0 + 1, len); + strings.add(str); + } + mapping[last0 + 1] = strings.size() - 1; + last0 = i; + } + } + } } - - - diff --git a/marytts-common/src/main/java/marytts/fst/StringPair.java b/marytts-common/src/main/java/marytts/fst/StringPair.java index c4cfde91..ca15303d 100644 --- a/marytts-common/src/main/java/marytts/fst/StringPair.java +++ b/marytts-common/src/main/java/marytts/fst/StringPair.java @@ -21,60 +21,57 @@ package marytts.fst; /** * A Pair of Strings. + * * @author benjaminroth */ -public class StringPair{ +public class StringPair { - //private boolean hasHash; - //private int hash; - private String string1; - private String string2; + // private boolean hasHash; + // private int hash; + private String string1; + private String string2; - public StringPair(String s1, String s2) { - this.string1 = s1; - this.string2 = s2; - //this.hasHash = false; - } + public StringPair(String s1, String s2) { + this.string1 = s1; + this.string2 = s2; + // this.hasHash = false; + } - public void setString1(String s1){ - this.string1 = s1; - } + public void setString1(String s1) { + this.string1 = s1; + } - public void setString2(String s2){ - this.string2 = s2; - } + public void setString2(String s2) { + this.string2 = s2; + } - public int hashCode() { - /*if (!hasHash){ - this.hash = 31 * string1.hashCode() + string2.hashCode(); - this.hasHash = true; - } + public int hashCode() { + /* + * if (!hasHash){ this.hash = 31 * string1.hashCode() + string2.hashCode(); this.hasHash = true; } + * + * return this.hash; + */ + return 31 * string1.hashCode() + string2.hashCode(); + } - return this.hash;*/ - return 31 * string1.hashCode() + string2.hashCode(); - } + public boolean equals(Object o) { - public boolean equals(Object o) { + if (o instanceof StringPair && ((StringPair) o).getString1().equals(string1) + && ((StringPair) o).getString2().equals(string2)) + return true; - if (o instanceof StringPair && - ((StringPair) o).getString1().equals(string1) && - ((StringPair) o).getString2().equals(string2)) - return true; + return false; + } - return false; - } + public String getString1() { + return string1; + } - public String getString1() { - return string1; - } + public String getString2() { + return string2; + } - public String getString2() { - return string2; - } - - public String toString(){ - return string1 + " " + string2; - } + public String toString() { + return string1 + " " + string2; + } } - - diff --git a/marytts-common/src/main/java/marytts/util/ConversionUtils.java b/marytts-common/src/main/java/marytts/util/ConversionUtils.java index d3413e31..5744b67e 100644 --- a/marytts-common/src/main/java/marytts/util/ConversionUtils.java +++ b/marytts-common/src/main/java/marytts/util/ConversionUtils.java @@ -1,6 +1,6 @@ -/** - * Copyright 2007 DFKI GmbH. - * All Rights Reserved. Use is subject to license terms. +/** + * Copyright 2007 DFKI GmbH. + * All Rights Reserved. Use is subject to license terms. * * This file is part of MARY TTS. * @@ -17,491 +17,387 @@ * along with this program. If not, see . * */ -package marytts.util; - -/** - * @author Oytun Türk - * - */ -public class ConversionUtils -{ - public static byte[] toByteArray(byte byteArray) - { - return new byte[]{byteArray}; - } - - public static byte[] toByteArray(byte[] byteArray) - { - return byteArray; - } - - public static byte[] toByteArray(short data) - { - return new byte[] { - (byte)((data >> 8) & 0xff), - (byte)((data >> 0) & 0xff), - }; - } - - public static byte[] toByteArray(short[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length * 2]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i * 2, 2); - - return byts; - } - - public static byte[] toByteArray(char data) - { - return new byte[] { - (byte)((data >> 8) & 0xff), - (byte)((data >> 0) & 0xff), - }; - } - - public static byte[] toByteArray(char[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length * 2]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i*2, 2); - - return byts; - } - - public static byte[] toByteArray(int data) - { - return new byte[] { - (byte)((data >> 24) & 0xff), - (byte)((data >> 16) & 0xff), - (byte)((data >> 8) & 0xff), - (byte)((data >> 0) & 0xff), - }; - } - - public static byte[] toByteArray(int[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length*4]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i*4, 4); - - return byts; - } - - public static byte[] toByteArray(long data) - { - return new byte[] { - (byte)((data >> 56) & 0xff), - (byte)((data >> 48) & 0xff), - (byte)((data >> 40) & 0xff), - (byte)((data >> 32) & 0xff), - (byte)((data >> 24) & 0xff), - (byte)((data >> 16) & 0xff), - (byte)((data >> 8) & 0xff), - (byte)((data >> 0) & 0xff), - }; - } - - public static byte[] toByteArray(long[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length*8]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i*8, 8); - - return byts; - } - - public static byte[] toByteArray(float data) - { - return toByteArray(Float.floatToRawIntBits(data)); - } - - public static byte[] toByteArray(float[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length*4]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i*4, 4); - - return byts; - } - - public static byte[] toByteArray(double data) - { - return toByteArray(Double.doubleToRawLongBits(data)); - } - - public static byte[] toByteArray(double[] data) - { - if (data == null) - return null; - - byte[] byts = new byte[data.length*8]; - - for (int i = 0; i < data.length; i++) - System.arraycopy(toByteArray(data[i]), 0, byts, i*8, 8); - - return byts; - } - - public static byte[] toByteArray(boolean data) - { - return new byte[]{(byte)(data ? 0x01 : 0x00)}; - } - - public static byte[] toByteArray(boolean[] data) - { - if (data == null) - return null; - - int len = data.length; - byte[] lena = toByteArray(len); - byte[] byts = new byte[lena.length + (len / 8) + (len % 8 != 0 ? 1 : 0)]; - - System.arraycopy(lena, 0, byts, 0, lena.length); - - for (int i = 0, j = lena.length, k = 7; i < data.length; i++) - { - byts[j] |= (data[i] ? 1 : 0) << k--; - if (k < 0) { j++; k = 7; } - } - - return byts; - } - - public static byte[] toByteArray(String data) - { - return (data == null) ? null : data.getBytes(); - } - - public static byte[] toByteArray(String[] data) - { - if (data == null) - return null; - - int totalLength = 0; - int bytesPos = 0; - - byte[] dLen = toByteArray(data.length); - totalLength += dLen.length; - - int[] sLens = new int[data.length]; - totalLength += (sLens.length * 4); - byte[][] strs = new byte[data.length][]; - - for (int i = 0; i < data.length; i++) - { - if (data[i] != null) - { - strs[i] = toByteArray(data[i]); - sLens[i] = strs[i].length; - totalLength += strs[i].length; - } - else - { - sLens[i] = 0; - strs[i] = new byte[0]; - } - } - - byte[] bytes = new byte[totalLength]; - System.arraycopy(dLen, 0, bytes, 0, 4); - - byte[] bsLens = toByteArray(sLens); - System.arraycopy(bsLens, 0, bytes, 4, bsLens.length); - - bytesPos += 4 + bsLens.length; // mark position - - for (byte[] sba : strs) - { - System.arraycopy(sba, 0, bytes, bytesPos, sba.length); - bytesPos += sba.length; - } - - return bytes; - } - - public static byte toByte(byte[] byteArray) - { - return (byteArray == null || byteArray.length == 0) ? 0x0 : byteArray[0]; - } - - public static short toShort(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 2) return 0x0; - // ---------- - return (short)( - (0xff & byteArray[0]) << 8 | - (0xff & byteArray[1]) << 0 - ); - } - - public static short[] toShortArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length % 2 != 0) - return null; - - short[] shts = new short[byteArray.length / 2]; - - for (int i = 0; i < shts.length; i++) - { - shts[i] = toShort( new byte[] { - byteArray[(i*2)], - byteArray[(i*2)+1] - } ); - } - - return shts; - } - - public static char toChar(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 2) - return 0x0; - - return (char)( - (0xff & byteArray[0]) << 8 | - (0xff & byteArray[1]) << 0 - ); - } - - public static char[] toCharArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length % 2 != 0) - return null; - - char[] chrs = new char[byteArray.length / 2]; - - for (int i = 0; i < chrs.length; i++) - { - chrs[i] = toChar( new byte[] { - byteArray[(i*2)], - byteArray[(i*2)+1], - } ); - } - - return chrs; - } - - public static int toInt(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 4) - return 0x0; - - return (int)( - (0xff & byteArray[0]) << 24 | - (0xff & byteArray[1]) << 16 | - (0xff & byteArray[2]) << 8 | - (0xff & byteArray[3]) << 0 - ); - } - - public static int[] toIntArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length % 4 != 0) - return null; - - int[] ints = new int[byteArray.length / 4]; - - for (int i = 0; i < ints.length; i++) - { - ints[i] = toInt( new byte[] { - byteArray[(i*4)], - byteArray[(i*4)+1], - byteArray[(i*4)+2], - byteArray[(i*4)+3], - } ); - } - - return ints; - } - - public static long toLong(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 8) - return 0x0; - - return (long)( - (long)(0xff & byteArray[0]) << 56 | - (long)(0xff & byteArray[1]) << 48 | - (long)(0xff & byteArray[2]) << 40 | - (long)(0xff & byteArray[3]) << 32 | - (long)(0xff & byteArray[4]) << 24 | - (long)(0xff & byteArray[5]) << 16 | - (long)(0xff & byteArray[6]) << 8 | - (long)(0xff & byteArray[7]) << 0 - ); - } - - public static long[] toLongArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length % 8 != 0) - return null; - - long[] lngs = new long[byteArray.length / 8]; - - for (int i = 0; i < lngs.length; i++) - { - lngs[i] = toLong( new byte[] { - byteArray[(i*8)], - byteArray[(i*8)+1], - byteArray[(i*8)+2], - byteArray[(i*8)+3], - byteArray[(i*8)+4], - byteArray[(i*8)+5], - byteArray[(i*8)+6], - byteArray[(i*8)+7], - } ); - } - - return lngs; - } - - public static float toFloat(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 4) - return 0x0; - - return Float.intBitsToFloat(toInt(byteArray)); - } - - public static float[] toFloatArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length % 4 != 0) - return null; - - float[] flts = new float[byteArray.length / 4]; - - for (int i = 0; i < flts.length; i++) - { - flts[i] = toFloat( new byte[] { - byteArray[(i*4)], - byteArray[(i*4)+1], - byteArray[(i*4)+2], - byteArray[(i*4)+3], - } ); - } - - return flts; - } - - public static double toDouble(byte[] byteArray) - { - if (byteArray == null || byteArray.length != 8) - return 0x0; - - return Double.longBitsToDouble(toLong(byteArray)); - } - - public static double[] toDoubleArray(byte[] byteArray) - { - if (byteArray == null) - return null; - - if (byteArray.length % 8 != 0) - return null; - - double[] dbls = new double[byteArray.length / 8]; - - for (int i = 0; i < dbls.length; i++) - { - dbls[i] = toDouble( new byte[] { - byteArray[(i*8)], - byteArray[(i*8)+1], - byteArray[(i*8)+2], - byteArray[(i*8)+3], - byteArray[(i*8)+4], - byteArray[(i*8)+5], - byteArray[(i*8)+6], - byteArray[(i*8)+7], - } ); - } - - return dbls; - } - - public static boolean toBoolean(byte[] byteArray) - { - return (byteArray == null || byteArray.length == 0) ? false : byteArray[0] != 0x00; - } - - public static boolean[] toBooleanArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length < 4) - return null; - - int len = toInt(new byte[]{byteArray[0], byteArray[1], byteArray[2], byteArray[3]}); - boolean[] bools = new boolean[len]; - - for (int i = 0, j = 4, k = 7; i < bools.length; i++) - { - bools[i] = ((byteArray[j] >> k--) & 0x01) == 1; - if (k < 0) { j++; k = 7; } - } - - return bools; - } - - public static String toString(byte[] byteArray) - { - return (byteArray == null) ? null : new String(byteArray); - } - - public static String[] toStringArray(byte[] byteArray) - { - if (byteArray == null || byteArray.length < 4) - return null; - - byte[] bBuff = new byte[4]; - - System.arraycopy(byteArray, 0, bBuff, 0, 4); - int saLen = toInt(bBuff); - - if (byteArray.length < (4 + (saLen * 4))) - return null; - - bBuff = new byte[saLen*4]; - System.arraycopy(byteArray, 4, bBuff, 0, bBuff.length); - int[] sLens = toIntArray(bBuff); - if (sLens == null) - return null; - - String[] strs = new String[saLen]; - for (int i=0, dataPos=4+(saLen*4); i 0) - { - if (byteArray.length >= (dataPos + sLens[i])) - { - bBuff = new byte[sLens[i]]; - System.arraycopy(byteArray, dataPos, bBuff, 0, sLens[i]); - dataPos += sLens[i]; - strs[i] = toString(bBuff); - } - else - return null; - } - } - return strs; - } -} +package marytts.util; +/** + * @author Oytun Türk + * + */ +public class ConversionUtils { + public static byte[] toByteArray(byte byteArray) { + return new byte[] { byteArray }; + } + + public static byte[] toByteArray(byte[] byteArray) { + return byteArray; + } + + public static byte[] toByteArray(short data) { + return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; + } + + public static byte[] toByteArray(short[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 2]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 2, 2); + + return byts; + } + + public static byte[] toByteArray(char data) { + return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; + } + + public static byte[] toByteArray(char[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 2]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 2, 2); + + return byts; + } + + public static byte[] toByteArray(int data) { + return new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), + (byte) ((data >> 0) & 0xff), }; + } + + public static byte[] toByteArray(int[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 4]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 4, 4); + + return byts; + } + + public static byte[] toByteArray(long data) { + return new byte[] { (byte) ((data >> 56) & 0xff), (byte) ((data >> 48) & 0xff), (byte) ((data >> 40) & 0xff), + (byte) ((data >> 32) & 0xff), (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), + (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; + } + + public static byte[] toByteArray(long[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 8]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 8, 8); + + return byts; + } + + public static byte[] toByteArray(float data) { + return toByteArray(Float.floatToRawIntBits(data)); + } + + public static byte[] toByteArray(float[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 4]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 4, 4); + + return byts; + } + + public static byte[] toByteArray(double data) { + return toByteArray(Double.doubleToRawLongBits(data)); + } + + public static byte[] toByteArray(double[] data) { + if (data == null) + return null; + + byte[] byts = new byte[data.length * 8]; + + for (int i = 0; i < data.length; i++) + System.arraycopy(toByteArray(data[i]), 0, byts, i * 8, 8); + + return byts; + } + + public static byte[] toByteArray(boolean data) { + return new byte[] { (byte) (data ? 0x01 : 0x00) }; + } + + public static byte[] toByteArray(boolean[] data) { + if (data == null) + return null; + + int len = data.length; + byte[] lena = toByteArray(len); + byte[] byts = new byte[lena.length + (len / 8) + (len % 8 != 0 ? 1 : 0)]; + + System.arraycopy(lena, 0, byts, 0, lena.length); + + for (int i = 0, j = lena.length, k = 7; i < data.length; i++) { + byts[j] |= (data[i] ? 1 : 0) << k--; + if (k < 0) { + j++; + k = 7; + } + } + + return byts; + } + + public static byte[] toByteArray(String data) { + return (data == null) ? null : data.getBytes(); + } + + public static byte[] toByteArray(String[] data) + { + if (data == null) + return null; + + int totalLength = 0; + int bytesPos = 0; + + byte[] dLen = toByteArray(data.length); + totalLength += dLen.length; + + int[] sLens = new int[data.length]; + totalLength += (sLens.length * 4); + byte[][] strs = new byte[data.length][]; + + for (int i = 0; i < data.length; i++) + { + if (data[i] != null) + { + strs[i] = toByteArray(data[i]); + sLens[i] = strs[i].length; + totalLength += strs[i].length; + } + else + { + sLens[i] = 0; + strs[i] = new byte[0]; + } + } + + byte[] bytes = new byte[totalLength]; + System.arraycopy(dLen, 0, bytes, 0, 4); + + byte[] bsLens = toByteArray(sLens); + System.arraycopy(bsLens, 0, bytes, 4, bsLens.length); + + bytesPos += 4 + bsLens.length; // mark position + + for (byte[] sba : strs) + { + System.arraycopy(sba, 0, bytes, bytesPos, sba.length); + bytesPos += sba.length; + } + + return bytes; + } + + public static byte toByte(byte[] byteArray) { + return (byteArray == null || byteArray.length == 0) ? 0x0 : byteArray[0]; + } + + public static short toShort(byte[] byteArray) { + if (byteArray == null || byteArray.length != 2) + return 0x0; + // ---------- + return (short) ((0xff & byteArray[0]) << 8 | (0xff & byteArray[1]) << 0); + } + + public static short[] toShortArray(byte[] byteArray) { + if (byteArray == null || byteArray.length % 2 != 0) + return null; + + short[] shts = new short[byteArray.length / 2]; + + for (int i = 0; i < shts.length; i++) { + shts[i] = toShort(new byte[] { byteArray[(i * 2)], byteArray[(i * 2) + 1] }); + } + + return shts; + } + + public static char toChar(byte[] byteArray) { + if (byteArray == null || byteArray.length != 2) + return 0x0; + + return (char) ((0xff & byteArray[0]) << 8 | (0xff & byteArray[1]) << 0); + } + + public static char[] toCharArray(byte[] byteArray) { + if (byteArray == null || byteArray.length % 2 != 0) + return null; + + char[] chrs = new char[byteArray.length / 2]; + + for (int i = 0; i < chrs.length; i++) { + chrs[i] = toChar(new byte[] { byteArray[(i * 2)], byteArray[(i * 2) + 1], }); + } + + return chrs; + } + + public static int toInt(byte[] byteArray) { + if (byteArray == null || byteArray.length != 4) + return 0x0; + + return (int) ((0xff & byteArray[0]) << 24 | (0xff & byteArray[1]) << 16 | (0xff & byteArray[2]) << 8 | (0xff & byteArray[3]) << 0); + } + + public static int[] toIntArray(byte[] byteArray) { + if (byteArray == null || byteArray.length % 4 != 0) + return null; + + int[] ints = new int[byteArray.length / 4]; + + for (int i = 0; i < ints.length; i++) { + ints[i] = toInt(new byte[] { byteArray[(i * 4)], byteArray[(i * 4) + 1], byteArray[(i * 4) + 2], + byteArray[(i * 4) + 3], }); + } + + return ints; + } + + public static long toLong(byte[] byteArray) { + if (byteArray == null || byteArray.length != 8) + return 0x0; + + return (long) ((long) (0xff & byteArray[0]) << 56 | (long) (0xff & byteArray[1]) << 48 + | (long) (0xff & byteArray[2]) << 40 | (long) (0xff & byteArray[3]) << 32 | (long) (0xff & byteArray[4]) << 24 + | (long) (0xff & byteArray[5]) << 16 | (long) (0xff & byteArray[6]) << 8 | (long) (0xff & byteArray[7]) << 0); + } + + public static long[] toLongArray(byte[] byteArray) { + if (byteArray == null || byteArray.length % 8 != 0) + return null; + + long[] lngs = new long[byteArray.length / 8]; + + for (int i = 0; i < lngs.length; i++) { + lngs[i] = toLong(new byte[] { byteArray[(i * 8)], byteArray[(i * 8) + 1], byteArray[(i * 8) + 2], + byteArray[(i * 8) + 3], byteArray[(i * 8) + 4], byteArray[(i * 8) + 5], byteArray[(i * 8) + 6], + byteArray[(i * 8) + 7], }); + } + + return lngs; + } + + public static float toFloat(byte[] byteArray) { + if (byteArray == null || byteArray.length != 4) + return 0x0; + + return Float.intBitsToFloat(toInt(byteArray)); + } + + public static float[] toFloatArray(byte[] byteArray) { + if (byteArray == null || byteArray.length % 4 != 0) + return null; + + float[] flts = new float[byteArray.length / 4]; + + for (int i = 0; i < flts.length; i++) { + flts[i] = toFloat(new byte[] { byteArray[(i * 4)], byteArray[(i * 4) + 1], byteArray[(i * 4) + 2], + byteArray[(i * 4) + 3], }); + } + + return flts; + } + + public static double toDouble(byte[] byteArray) { + if (byteArray == null || byteArray.length != 8) + return 0x0; + + return Double.longBitsToDouble(toLong(byteArray)); + } + + public static double[] toDoubleArray(byte[] byteArray) { + if (byteArray == null) + return null; + + if (byteArray.length % 8 != 0) + return null; + + double[] dbls = new double[byteArray.length / 8]; + + for (int i = 0; i < dbls.length; i++) { + dbls[i] = toDouble(new byte[] { byteArray[(i * 8)], byteArray[(i * 8) + 1], byteArray[(i * 8) + 2], + byteArray[(i * 8) + 3], byteArray[(i * 8) + 4], byteArray[(i * 8) + 5], byteArray[(i * 8) + 6], + byteArray[(i * 8) + 7], }); + } + + return dbls; + } + + public static boolean toBoolean(byte[] byteArray) { + return (byteArray == null || byteArray.length == 0) ? false : byteArray[0] != 0x00; + } + + public static boolean[] toBooleanArray(byte[] byteArray) { + if (byteArray == null || byteArray.length < 4) + return null; + + int len = toInt(new byte[] { byteArray[0], byteArray[1], byteArray[2], byteArray[3] }); + boolean[] bools = new boolean[len]; + + for (int i = 0, j = 4, k = 7; i < bools.length; i++) { + bools[i] = ((byteArray[j] >> k--) & 0x01) == 1; + if (k < 0) { + j++; + k = 7; + } + } + + return bools; + } + + public static String toString(byte[] byteArray) { + return (byteArray == null) ? null : new String(byteArray); + } + + public static String[] toStringArray(byte[] byteArray) { + if (byteArray == null || byteArray.length < 4) + return null; + + byte[] bBuff = new byte[4]; + + System.arraycopy(byteArray, 0, bBuff, 0, 4); + int saLen = toInt(bBuff); + + if (byteArray.length < (4 + (saLen * 4))) + return null; + + bBuff = new byte[saLen * 4]; + System.arraycopy(byteArray, 4, bBuff, 0, bBuff.length); + int[] sLens = toIntArray(bBuff); + if (sLens == null) + return null; + + String[] strs = new String[saLen]; + for (int i = 0, dataPos = 4 + (saLen * 4); i < saLen; i++) { + if (sLens[i] > 0) { + if (byteArray.length >= (dataPos + sLens[i])) { + bBuff = new byte[sLens[i]]; + System.arraycopy(byteArray, dataPos, bBuff, 0, sLens[i]); + dataPos += sLens[i]; + strs[i] = toString(bBuff); + } else + return null; + } + } + return strs; + } +} diff --git a/marytts-common/src/main/java/marytts/util/PrintSystemProperties.java b/marytts-common/src/main/java/marytts/util/PrintSystemProperties.java index df60640e..bfa9cac7 100644 --- a/marytts-common/src/main/java/marytts/util/PrintSystemProperties.java +++ b/marytts-common/src/main/java/marytts/util/PrintSystemProperties.java @@ -26,18 +26,17 @@ import java.util.TreeSet; public class PrintSystemProperties { - /** - * @param args - */ - public static void main(String[] args) { - // TODO Auto-generated method stub - Properties p = System.getProperties(); - SortedSet keys = new TreeSet(p.keySet()); - for (Iterator it=keys.iterator(); it.hasNext(); ) { - String key = (String) it.next(); - System.out.println(key + " = " + p.getProperty(key)); - } - } + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Properties p = System.getProperties(); + SortedSet keys = new TreeSet(p.keySet()); + for (Iterator it = keys.iterator(); it.hasNext();) { + String key = (String) it.next(); + System.out.println(key + " = " + p.getProperty(key)); + } + } } - diff --git a/marytts-common/src/main/java/marytts/util/UncloseableBufferedReader.java b/marytts-common/src/main/java/marytts/util/UncloseableBufferedReader.java index b8f6b91b..9e4d7e27 100644 --- a/marytts-common/src/main/java/marytts/util/UncloseableBufferedReader.java +++ b/marytts-common/src/main/java/marytts/util/UncloseableBufferedReader.java @@ -24,17 +24,15 @@ import java.io.Reader; /** * A BufferedReader that ignores calls to close(). - * + * * @author Marc Schroeder */ -public class UncloseableBufferedReader extends BufferedReader -{ - public UncloseableBufferedReader(Reader i) { - super(i); - } - - public void close() { - } -} +public class UncloseableBufferedReader extends BufferedReader { + public UncloseableBufferedReader(Reader i) { + super(i); + } + public void close() { + } +} diff --git a/marytts-common/src/main/java/marytts/util/data/MaryHeader.java b/marytts-common/src/main/java/marytts/util/data/MaryHeader.java index 12997229..debcb50a 100644 --- a/marytts-common/src/main/java/marytts/util/data/MaryHeader.java +++ b/marytts-common/src/main/java/marytts/util/data/MaryHeader.java @@ -43,80 +43,78 @@ import java.nio.ByteBuffer; import marytts.exceptions.MaryConfigurationException; /** - * Common helper class to read/write a standard Mary header to/from the various - * Mary data files. + * Common helper class to read/write a standard Mary header to/from the various Mary data files. * * @author sacha - * + * */ -public class MaryHeader -{ - /* Global constants */ - private final static int MAGIC = 0x4d415259; // "MARY" - private final static int VERSION = 40; // 4.0 - - /* List of authorized file type identifier constants */ - public final static int UNKNOWN = 0; - public final static int CARTS = 100; - public final static int DIRECTED_GRAPH = 110; - public final static int UNITS = 200; - public final static int LISTENERUNITS = 225; - public final static int UNITFEATS = 300; - public final static int LISTENERFEATS = 325; - public final static int HALFPHONE_UNITFEATS = 301; - public final static int JOINFEATS = 400; - public final static int SCOST = 445; - public final static int PRECOMPUTED_JOINCOSTS = 450; - public final static int TIMELINE = 500; - - - /* Private fields */ - private int magic = MAGIC; - private int version = VERSION; - private int type = UNKNOWN; +public class MaryHeader { + /* Global constants */ + private final static int MAGIC = 0x4d415259; // "MARY" + private final static int VERSION = 40; // 4.0 - - // STATIC CODE + /* List of authorized file type identifier constants */ + public final static int UNKNOWN = 0; + public final static int CARTS = 100; + public final static int DIRECTED_GRAPH = 110; + public final static int UNITS = 200; + public final static int LISTENERUNITS = 225; + public final static int UNITFEATS = 300; + public final static int LISTENERFEATS = 325; + public final static int HALFPHONE_UNITFEATS = 301; + public final static int JOINFEATS = 400; + public final static int SCOST = 445; + public final static int PRECOMPUTED_JOINCOSTS = 450; + public final static int TIMELINE = 500; - /** - * For the given file, look inside and determine the file type. - * @param fileName - * @return the file type, or -1 if the file does not have a valid MARY header. - * @throws IOException if the file cannot be read - */ - public static int peekFileType(String fileName) throws IOException - { - DataInputStream dis = null; - dis = new DataInputStream( new BufferedInputStream( new FileInputStream( fileName ) ) ); - /* Load the Mary header */ - try { - MaryHeader hdr = new MaryHeader( dis ); - int type = hdr.getType(); - return type; - } catch (MaryConfigurationException e) { - // not a valid MARY header - return -1; - } finally { - dis.close(); - } + /* Private fields */ + private int magic = MAGIC; + private int version = VERSION; + private int type = UNKNOWN; - } - - - /****************/ - /* CONSTRUCTORS */ - /****************/ - - /** - * Consruct a MaryHeader from scratch. - * - * Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type. - * - * @param newType The type of MaryHeader to create. See public final constants in this class. - * - * @throws IllegalArgumentException if the input type is unknown. - */ - public MaryHeader( int newType ) { + // STATIC CODE + + /** + * For the given file, look inside and determine the file type. + * + * @param fileName + * @return the file type, or -1 if the file does not have a valid MARY header. + * @throws IOException + * if the file cannot be read + */ + public static int peekFileType(String fileName) throws IOException { + DataInputStream dis = null; + dis = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName))); + /* Load the Mary header */ + try { + MaryHeader hdr = new MaryHeader(dis); + int type = hdr.getType(); + return type; + } catch (MaryConfigurationException e) { + // not a valid MARY header + return -1; + } finally { + dis.close(); + } + + } + + /****************/ + /* CONSTRUCTORS */ + /****************/ + + /** + * Consruct a MaryHeader from scratch. + * + * Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type. + * + * @param newType + * The type of MaryHeader to create. See public final constants in this class. + * + * @throws IllegalArgumentException + * if the input type is unknown. + */ + public MaryHeader( int newType ) { if ( (newType > TIMELINE) || (newType < UNKNOWN) ) { throw new IllegalArgumentException( "Unauthorized Mary file type [" + type + "]." ); } @@ -127,16 +125,18 @@ public class MaryHeader assert hasLegalMagic(); assert hasLegalType(); } - - /** - * Construct a MaryHeader by reading from a file. - * Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type. - * - * @param input a DataInputStream or RandomAccessFile to read the header from. - * - * @throws MaryConfigurationException if no mary header can be read from input. - */ - public MaryHeader( DataInput input ) throws MaryConfigurationException { + + /** + * Construct a MaryHeader by reading from a file. Fundamental guarantee: after construction, the MaryHeader has a valid magic + * number and a valid type. + * + * @param input + * a DataInputStream or RandomAccessFile to read the header from. + * + * @throws MaryConfigurationException + * if no mary header can be read from input. + */ + public MaryHeader( DataInput input ) throws MaryConfigurationException { try { this.load( input ); } catch (IOException e) { @@ -151,15 +151,17 @@ public class MaryHeader assert hasLegalType(); } - /** - * Construct a MaryHeader by reading from a file. - * Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type. - * - * @param input a byte buffer to read the header from. - * - * @throws MaryConfigurationException if no mary header can be read from input. - */ - public MaryHeader( ByteBuffer input ) throws MaryConfigurationException { + /** + * Construct a MaryHeader by reading from a file. Fundamental guarantee: after construction, the MaryHeader has a valid magic + * number and a valid type. + * + * @param input + * a byte buffer to read the header from. + * + * @throws MaryConfigurationException + * if no mary header can be read from input. + */ + public MaryHeader( ByteBuffer input ) throws MaryConfigurationException { try { this.load( input ); } catch (BufferUnderflowException e) { @@ -174,19 +176,22 @@ public class MaryHeader assert hasLegalType(); } - /*****************/ - /* OTHER METHODS */ - /*****************/ - - /** Mary header writer - * - * @param output The DataOutputStream or RandomAccessFile to write to - * - * @return the number of written bytes. - * - * @throws IOException if the file type is unknown. - */ - public long writeTo( DataOutput output ) throws IOException { + /*****************/ + /* OTHER METHODS */ + /*****************/ + + /** + * Mary header writer + * + * @param output + * The DataOutputStream or RandomAccessFile to write to + * + * @return the number of written bytes. + * + * @throws IOException + * if the file type is unknown. + */ + public long writeTo( DataOutput output ) throws IOException { long nBytes = 0; @@ -198,44 +203,61 @@ public class MaryHeader return( nBytes ); } - - /** Load a mary header. - * - * @param input The data input (DataInputStream or RandomAccessFile) to read from. - * - * @throws IOException if the header data cannot be read - */ - private void load( DataInput input ) throws IOException { - - magic = input.readInt(); - version = input.readInt(); - type = input.readInt(); - } - - /** - * Load a mary header. - * - * @param input the byte buffer from which to read the mary header. - * @throws BufferUnderflowException if the header data cannot be read - */ - private void load(ByteBuffer input) { - magic = input.getInt(); - version = input.getInt(); - type = input.getInt(); - } - - /* Accessors */ - public int getMagic() { return(magic); } - public int getVersion() { return(version); } - public int getType() { return(type); } - /* Checkers */ - public boolean hasCurrentVersion() { return( version == VERSION ); } - private boolean hasLegalType() { - return (type <= TIMELINE) && (type > UNKNOWN) ; - } - private boolean hasLegalMagic() { - return( magic == MAGIC ); - } + /** + * Load a mary header. + * + * @param input + * The data input (DataInputStream or RandomAccessFile) to read from. + * + * @throws IOException + * if the header data cannot be read + */ + private void load(DataInput input) throws IOException { + + magic = input.readInt(); + version = input.readInt(); + type = input.readInt(); + } + + /** + * Load a mary header. + * + * @param input + * the byte buffer from which to read the mary header. + * @throws BufferUnderflowException + * if the header data cannot be read + */ + private void load(ByteBuffer input) { + magic = input.getInt(); + version = input.getInt(); + type = input.getInt(); + } + + /* Accessors */ + public int getMagic() { + return (magic); + } + + public int getVersion() { + return (version); + } + + public int getType() { + return (type); + } + + /* Checkers */ + public boolean hasCurrentVersion() { + return (version == VERSION); + } + + private boolean hasLegalType() { + return (type <= TIMELINE) && (type > UNKNOWN); + } + + private boolean hasLegalMagic() { + return (magic == MAGIC); + } } diff --git a/marytts-common/src/main/java/marytts/util/dom/LoggingErrorHandler.java b/marytts-common/src/main/java/marytts/util/dom/LoggingErrorHandler.java index d1d18d60..5d498b50 100644 --- a/marytts-common/src/main/java/marytts/util/dom/LoggingErrorHandler.java +++ b/marytts-common/src/main/java/marytts/util/dom/LoggingErrorHandler.java @@ -29,63 +29,46 @@ import org.xml.sax.ErrorHandler; import org.xml.sax.SAXParseException; /** - * Implements an ErrorHandler for XML parsing - * that provides error and warning messages to the log4j logger. - * + * Implements an ErrorHandler for XML parsing that provides error and warning messages to the log4j logger. + * * @author Marc Schröder */ -public class LoggingErrorHandler implements ErrorHandler, ErrorListener -{ - Logger logger; - public LoggingErrorHandler(String name) - { - logger = MaryUtils.getLogger(name); - } +public class LoggingErrorHandler implements ErrorHandler, ErrorListener { + Logger logger; - public void error(SAXParseException e) - throws SAXParseException - { - logger.warn(e.getMessage()); - throw e; - } + public LoggingErrorHandler(String name) { + logger = MaryUtils.getLogger(name); + } - public void error(TransformerException e) - throws TransformerException - { - logger.warn(e.getMessageAndLocation()); - throw e; - } + public void error(SAXParseException e) throws SAXParseException { + logger.warn(e.getMessage()); + throw e; + } - public void warning(SAXParseException e) - throws SAXParseException - { - logger.warn(e.getMessage()); - throw e; - } + public void error(TransformerException e) throws TransformerException { + logger.warn(e.getMessageAndLocation()); + throw e; + } - public void warning(TransformerException e) - throws TransformerException - { - logger.warn(e.getMessageAndLocation()); - throw e; - } + public void warning(SAXParseException e) throws SAXParseException { + logger.warn(e.getMessage()); + throw e; + } - public void fatalError(SAXParseException e) - throws SAXParseException - { - logger.warn(e.getMessage()); - throw e; - } + public void warning(TransformerException e) throws TransformerException { + logger.warn(e.getMessageAndLocation()); + throw e; + } - public void fatalError(TransformerException e) - throws TransformerException - { - logger.warn(e.getMessageAndLocation()); - throw e; - } + public void fatalError(SAXParseException e) throws SAXParseException { + logger.warn(e.getMessage()); + throw e; + } - + public void fatalError(TransformerException e) throws TransformerException { + logger.warn(e.getMessageAndLocation()); + throw e; + } } - diff --git a/marytts-common/src/main/java/marytts/util/dom/MaryNormalisedWriter.java b/marytts-common/src/main/java/marytts/util/dom/MaryNormalisedWriter.java index 39f9bd1c..53994b13 100644 --- a/marytts-common/src/main/java/marytts/util/dom/MaryNormalisedWriter.java +++ b/marytts-common/src/main/java/marytts/util/dom/MaryNormalisedWriter.java @@ -46,114 +46,113 @@ import org.apache.log4j.Logger; import org.w3c.dom.Node; /** - * A wrapper class for output of XML DOM trees in a Mary normalised way: - * One tag or text node per line, no indentation. - * This is only needed during the transition phase to "real" XML modules. + * A wrapper class for output of XML DOM trees in a Mary normalised way: One tag or text node per line, no indentation. This is + * only needed during the transition phase to "real" XML modules. + * * @author Marc Schröder */ public class MaryNormalisedWriter { - private static TransformerFactory tFactory = null; - private static Templates stylesheet = null; + private static TransformerFactory tFactory = null; + private static Templates stylesheet = null; - private static Logger logger; // only used for extensive debug output + private static Logger logger; // only used for extensive debug output - private Transformer transformer; + private Transformer transformer; - /** Default constructor. - * Calls startup() if it has not been called before. - * @see #startup(). - */ - public MaryNormalisedWriter() - throws MaryConfigurationException - { - try { - // startup every time: - startup(); - transformer = stylesheet.newTransformer(); - } catch (Exception e) { - throw new MaryConfigurationException("Cannot initialise XML writing code", e); - } - } + /** + * Default constructor. Calls startup() if it has not been called before. + * + * @see #startup(). + */ + public MaryNormalisedWriter() throws MaryConfigurationException { + try { + // startup every time: + startup(); + transformer = stylesheet.newTransformer(); + } catch (Exception e) { + throw new MaryConfigurationException("Cannot initialise XML writing code", e); + } + } - // Methods + // Methods - /** Start up the static parts, and compile the normalise-maryxml XSLT - * stylesheet which can then be used by multiple threads. - * @exception TransformerFactoryConfigurationError - * if the TransformerFactory cannot be instanciated. - * @exception FileNotFoundException - * if the stylesheet file cannot be found. - * @exception TransformerConfigurationException - * if the templates stylesheet cannot be generated. - */ - private static void startup() - throws TransformerFactoryConfigurationError, TransformerConfigurationException - { - // only start the stuff if it hasn't been started yet. - if (tFactory == null) { - tFactory = TransformerFactory.newInstance(); - } - if (stylesheet == null) { - StreamSource stylesheetStream = - new StreamSource( - MaryNormalisedWriter.class.getResourceAsStream( - "normalise-maryxml.xsl")); - stylesheet = tFactory.newTemplates(stylesheetStream); - } - if (logger == null) - logger = MaryUtils.getLogger("MaryNormalisedWriter"); + /** + * Start up the static parts, and compile the normalise-maryxml XSLT stylesheet which can then be used by multiple threads. + * + * @exception TransformerFactoryConfigurationError + * if the TransformerFactory cannot be instanciated. + * @exception FileNotFoundException + * if the stylesheet file cannot be found. + * @exception TransformerConfigurationException + * if the templates stylesheet cannot be generated. + */ + private static void startup() throws TransformerFactoryConfigurationError, TransformerConfigurationException { + // only start the stuff if it hasn't been started yet. + if (tFactory == null) { + tFactory = TransformerFactory.newInstance(); + } + if (stylesheet == null) { + StreamSource stylesheetStream = new StreamSource( + MaryNormalisedWriter.class.getResourceAsStream("normalise-maryxml.xsl")); + stylesheet = tFactory.newTemplates(stylesheetStream); + } + if (logger == null) + logger = MaryUtils.getLogger("MaryNormalisedWriter"); - } + } - /** The actual output to stdout. - * @param input a DOMSource, a SAXSource or a StreamSource. - * @see javax.xml.transform.Transformer - * @exception TransformerException - * if the transformation cannot be performed. - */ - public void output(Source input, Result destination) throws TransformerException { - //logger.debug("Before transform"); - transformer.transform(input, destination); - //logger.debug("After transform"); - } - - /** - * Output any Source to stdout. - */ - public void output(Source input) throws TransformerException { - output(input, new StreamResult(new PrintStream(System.out, true))); - } + /** + * The actual output to stdout. + * + * @param input + * a DOMSource, a SAXSource or a StreamSource. + * @see javax.xml.transform.Transformer + * @exception TransformerException + * if the transformation cannot be performed. + */ + public void output(Source input, Result destination) throws TransformerException { + // logger.debug("Before transform"); + transformer.transform(input, destination); + // logger.debug("After transform"); + } - /** Output a DOM node to stdout. - * @see #output(Source) - */ - public void output(Node input) throws TransformerException { - output(new DOMSource(input)); - } + /** + * Output any Source to stdout. + */ + public void output(Source input) throws TransformerException { + output(input, new StreamResult(new PrintStream(System.out, true))); + } - /** - * Output a DOM node to a specified destination - */ - public void output(Node input, OutputStream destination) throws TransformerException { - output(new DOMSource(input), new StreamResult(destination)); - } + /** + * Output a DOM node to stdout. + * + * @see #output(Source) + */ + public void output(Node input) throws TransformerException { + output(new DOMSource(input)); + } - /** - * The simplest possible command line interface to the - * MaryNormalisedWriter. Reads a "real" XML document from stdin, - * and outputs it in the MaryNormalised form to stdout. - */ - public static void main(String[] args) throws Throwable { - startup(); - MaryNormalisedWriter writer = new MaryNormalisedWriter(); + /** + * Output a DOM node to a specified destination + */ + public void output(Node input, OutputStream destination) throws TransformerException { + output(new DOMSource(input), new StreamResult(destination)); + } - ReaderSplitter splitter = new ReaderSplitter(new InputStreamReader(System.in), ""); + /** + * The simplest possible command line interface to the MaryNormalisedWriter. Reads a "real" XML document from stdin, and + * outputs it in the MaryNormalised form to stdout. + */ + public static void main(String[] args) throws Throwable { + startup(); + MaryNormalisedWriter writer = new MaryNormalisedWriter(); - Reader oneXMLStructure = null; - while ((oneXMLStructure = splitter.nextReader()) != null) { - writer.output(new StreamSource(oneXMLStructure)); - } - } + ReaderSplitter splitter = new ReaderSplitter(new InputStreamReader(System.in), ""); + + Reader oneXMLStructure = null; + while ((oneXMLStructure = splitter.nextReader()) != null) { + writer.output(new StreamSource(oneXMLStructure)); + } + } } - diff --git a/marytts-common/src/main/java/marytts/util/dom/RENodeFilter.java b/marytts-common/src/main/java/marytts/util/dom/RENodeFilter.java index 64d98857..ec208809 100644 --- a/marytts-common/src/main/java/marytts/util/dom/RENodeFilter.java +++ b/marytts-common/src/main/java/marytts/util/dom/RENodeFilter.java @@ -24,32 +24,27 @@ import java.util.regex.Pattern; import org.w3c.dom.Node; import org.w3c.dom.traversal.NodeFilter; - /** - * A NodeFilter accepting only nodes with names matching - * a given regular expression. + * A NodeFilter accepting only nodes with names matching a given regular expression. + * * @author Marc Schröder */ -public class RENodeFilter implements NodeFilter -{ - private Pattern re; - public RENodeFilter(String reString) - { - this.re = Pattern.compile(reString); - } +public class RENodeFilter implements NodeFilter { + private Pattern re; - public RENodeFilter(Pattern re) - { - this.re = re; - } + public RENodeFilter(String reString) { + this.re = Pattern.compile(reString); + } - public short acceptNode(Node n) - { - if (re.matcher(n.getNodeName()).matches()) - return NodeFilter.FILTER_ACCEPT; - else - return NodeFilter.FILTER_SKIP; - } + public RENodeFilter(Pattern re) { + this.re = re; + } + + public short acceptNode(Node n) { + if (re.matcher(n.getNodeName()).matches()) + return NodeFilter.FILTER_ACCEPT; + else + return NodeFilter.FILTER_SKIP; + } } - diff --git a/marytts-common/src/main/java/marytts/util/http/Address.java b/marytts-common/src/main/java/marytts/util/http/Address.java index 92c6be57..343ebcb9 100644 --- a/marytts-common/src/main/java/marytts/util/http/Address.java +++ b/marytts-common/src/main/java/marytts/util/http/Address.java @@ -1,6 +1,6 @@ -/** - * Copyright 2007 DFKI GmbH. - * All Rights Reserved. Use is subject to license terms. +/** + * Copyright 2007 DFKI GmbH. + * All Rights Reserved. Use is subject to license terms. * * This file is part of MARY TTS. * @@ -17,83 +17,80 @@ * along with this program. If not, see . * */ -package marytts.util.http; - -/** - * - * A class to keep host and port information in a structure - * - * @author Oytun Türk - */ -public class Address -{ - private String host; - private int port; - private String fullAddress; // --> host:port - private String httpAddress; // --> http://host:port - - public Address() - { - this("", ""); - } - - public Address(String hostIn, int portIn) - { - this(hostIn, String.valueOf(portIn)); - } - - public Address(String hostIn, String portIn) - { - init(hostIn, portIn); - } - - public Address(String fullAddress) - { - String tmpAddress = fullAddress.trim(); - int index = tmpAddress.lastIndexOf(':'); - - String hostIn = ""; - String portIn = ""; - if (index>0) - { - hostIn = tmpAddress.substring(0, index); - - if (index+10) - this.httpAddress = "http://" + this.fullAddress; - else - this.httpAddress = null; - } - - public String getHost() { return host; } - - public int getPort() { return port; } - - public String getFullAddress() { return fullAddress; } - - public String getHttpAddress() { return httpAddress; } -} +package marytts.util.http; +/** + * + * A class to keep host and port information in a structure + * + * @author Oytun Türk + */ +public class Address { + private String host; + private int port; + private String fullAddress; // --> host:port + private String httpAddress; // --> http://host:port + + public Address() { + this("", ""); + } + + public Address(String hostIn, int portIn) { + this(hostIn, String.valueOf(portIn)); + } + + public Address(String hostIn, String portIn) { + init(hostIn, portIn); + } + + public Address(String fullAddress) { + String tmpAddress = fullAddress.trim(); + int index = tmpAddress.lastIndexOf(':'); + + String hostIn = ""; + String portIn = ""; + if (index > 0) { + hostIn = tmpAddress.substring(0, index); + + if (index + 1 < tmpAddress.length()) + portIn = tmpAddress.substring(index + 1); + } else + hostIn = tmpAddress; + + init(hostIn, portIn); + } + + public void init(String hostIn, String portIn) { + this.host = hostIn; + + if (portIn != "") { + this.port = Integer.valueOf(portIn); + this.fullAddress = this.host + ":" + portIn; + } else // No port address specified, set fullAdrress equal to host address + { + this.port = Integer.MIN_VALUE; + this.fullAddress = this.host; + } + + if (this.fullAddress != null && this.fullAddress.length() > 0) + this.httpAddress = "http://" + this.fullAddress; + else + this.httpAddress = null; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + public String getFullAddress() { + return fullAddress; + } + + public String getHttpAddress() { + return httpAddress; + } +} diff --git a/marytts-common/src/main/java/marytts/util/io/BasenameList.java b/marytts-common/src/main/java/marytts/util/io/BasenameList.java index 1b02a1ea..929605d2 100644 --- a/marytts-common/src/main/java/marytts/util/io/BasenameList.java +++ b/marytts-common/src/main/java/marytts/util/io/BasenameList.java @@ -44,331 +44,346 @@ import java.util.Arrays; import java.util.Vector; /** - * The BasenameList class produces and stores an alphabetically-sorted - * array of basenames issued from the .wav files present in a given directory. + * The BasenameList class produces and stores an alphabetically-sorted array of basenames issued from the .wav files present in a + * given directory. * * @author sacha - * + * */ -public class BasenameList -{ - private Vector bList = null; - private String fromDir = null; - private String fromExt = null; - private boolean hasChanged; - private static final int DEFAULT_INCREMENT = 128; - - /****************/ - /* CONSTRUCTORS */ - /****************/ - - /** - * Default constructor for an empty list. - */ - public BasenameList() { - fromDir = null; - fromExt = null; - bList = new Vector( DEFAULT_INCREMENT, DEFAULT_INCREMENT ); - hasChanged = false; - } - - /** - * Default constructor from an existing vector and fields. - */ - public BasenameList( String setFromDir, String setFromExt, Vector setVec ) { - fromDir = setFromDir; - fromExt = setFromExt; - bList = setVec; - hasChanged = false; - } - - /** - * Constructor from an array of strings. - */ - public BasenameList( String[] str ) { - fromDir = null; - fromExt = null; - bList = new Vector( DEFAULT_INCREMENT, DEFAULT_INCREMENT ); - add( str ); - hasChanged = false; - } - - /** - * This constructor lists the . files from directory dir, - * and initializes an an array with their list of alphabetically - * sorted basenames. - * - * @param dir The name of the directory to list the files from. - * @param extension The extension of the files to list. - * - */ - public BasenameList( String dirName, final String extension ) { - fromDir = dirName; - if ( extension.indexOf(".") != 0 ) fromExt = "." + extension; // If the dot was not included, add it. - else fromExt = extension; - /* Turn the directory name into a file, to allow for checking and listing */ - File dir = new File( dirName ); - /* Check if the directory exists */ - if ( !dir.exists() ) { - throw new RuntimeException( "Directory [" + dirName + "] does not exist. Can't find the [" + extension + "] files." ); - } - /* List the .extension files */ - File[] selectedFiles = dir.listFiles(new FilenameFilter() { - public boolean accept(File dir, String name) { - return name.endsWith( extension ); - } - }); - - /* Sort the file names alphabetically */ - Arrays.sort( selectedFiles ); - - /* Extract the basenames and store them in a vector of strings */ - bList = new Vector( selectedFiles.length, DEFAULT_INCREMENT ); - String str = null; - int subtractFromFilename = extension.length(); - for ( int i = 0; i < selectedFiles.length; i++ ) { - str = selectedFiles[i].getName().substring( 0, selectedFiles[i].getName().length() - subtractFromFilename ); - add( str ); - } - hasChanged = false; - } - - /** - * This constructor loads the basename list from a random access file. - * - * @param fileName The file to read from. - */ - public BasenameList( String fileName ) throws IOException { - load( fileName ); - hasChanged = false; - } - - /*****************/ - /* I/O METHODS */ - /*****************/ +public class BasenameList { + private Vector bList = null; + private String fromDir = null; + private String fromExt = null; + private boolean hasChanged; + private static final int DEFAULT_INCREMENT = 128; - /** - * Write the basenameList to a file, identified by its name. - */ - public void write( String fileName ) throws IOException { - write( new File( fileName ) ); - } - - /** - * Write the basenameList to a File. - */ - public void write( File file ) throws IOException { - PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ), true ); - if ( fromDir != null ) { - pw.println( "FROM: " + fromDir + "*" + fromExt ); - } - String str = null; - for ( int i = 0; i < bList.size(); i++ ) { - str = (String)(bList.elementAt(i)); - pw.println( str ); - } - } - - /** - * Read the basenameList from a file - */ - public void load( String fileName ) throws IOException { - /* Open the file */ - BufferedReader bfr = new BufferedReader( new InputStreamReader( new FileInputStream( fileName ), "UTF-8" ) ); - /* Make the vector */ - if ( bList == null ) bList = new Vector( DEFAULT_INCREMENT, DEFAULT_INCREMENT ); - /* Check if the first line contains the origin information (directory+ext) */ - String line = bfr.readLine(); - if ( line.indexOf("FROM: ") != -1 ) { - line = line.substring( 6 ); - String[] parts = new String[2]; - parts = line.split( "\\*", 2 ); - fromDir = parts[0]; - fromExt = parts[1]; - } - else if ( !(line.matches("^\\s*$")) ) add( line ); - /* Add the lines to the vector, ignoring the blank ones. */ - while ( (line = bfr.readLine()) != null ) { - if ( !(line.matches("^\\s*$")) ) add( line ); - } - } - - - /*****************/ - /* OTHER METHODS */ - /*****************/ + /****************/ + /* CONSTRUCTORS */ + /****************/ + + /** + * Default constructor for an empty list. + */ + public BasenameList() { + fromDir = null; + fromExt = null; + bList = new Vector(DEFAULT_INCREMENT, DEFAULT_INCREMENT); + hasChanged = false; + } + + /** + * Default constructor from an existing vector and fields. + */ + public BasenameList(String setFromDir, String setFromExt, Vector setVec) { + fromDir = setFromDir; + fromExt = setFromExt; + bList = setVec; + hasChanged = false; + } + + /** + * Constructor from an array of strings. + */ + public BasenameList(String[] str) { + fromDir = null; + fromExt = null; + bList = new Vector(DEFAULT_INCREMENT, DEFAULT_INCREMENT); + add(str); + hasChanged = false; + } + + /** + * This constructor lists the . files from directory dir, and initializes an an array with their list of + * alphabetically sorted basenames. + * + * @param dir + * The name of the directory to list the files from. + * @param extension + * The extension of the files to list. + * + */ + public BasenameList(String dirName, final String extension) { + fromDir = dirName; + if (extension.indexOf(".") != 0) + fromExt = "." + extension; // If the dot was not included, add it. + else + fromExt = extension; + /* Turn the directory name into a file, to allow for checking and listing */ + File dir = new File(dirName); + /* Check if the directory exists */ + if (!dir.exists()) { + throw new RuntimeException("Directory [" + dirName + "] does not exist. Can't find the [" + extension + "] files."); + } + /* List the .extension files */ + File[] selectedFiles = dir.listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.endsWith(extension); + } + }); + + /* Sort the file names alphabetically */ + Arrays.sort(selectedFiles); + + /* Extract the basenames and store them in a vector of strings */ + bList = new Vector(selectedFiles.length, DEFAULT_INCREMENT); + String str = null; + int subtractFromFilename = extension.length(); + for (int i = 0; i < selectedFiles.length; i++) { + str = selectedFiles[i].getName().substring(0, selectedFiles[i].getName().length() - subtractFromFilename); + add(str); + } + hasChanged = false; + } + + /** + * This constructor loads the basename list from a random access file. + * + * @param fileName + * The file to read from. + */ + public BasenameList(String fileName) throws IOException { + load(fileName); + hasChanged = false; + } + + /*****************/ + /* I/O METHODS */ + /*****************/ + + /** + * Write the basenameList to a file, identified by its name. + */ + public void write(String fileName) throws IOException { + write(new File(fileName)); + } + + /** + * Write the basenameList to a File. + */ + public void write(File file) throws IOException { + PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), true); + if (fromDir != null) { + pw.println("FROM: " + fromDir + "*" + fromExt); + } + String str = null; + for (int i = 0; i < bList.size(); i++) { + str = (String) (bList.elementAt(i)); + pw.println(str); + } + } + + /** + * Read the basenameList from a file + */ + public void load(String fileName) throws IOException { + /* Open the file */ + BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); + /* Make the vector */ + if (bList == null) + bList = new Vector(DEFAULT_INCREMENT, DEFAULT_INCREMENT); + /* Check if the first line contains the origin information (directory+ext) */ + String line = bfr.readLine(); + if (line.indexOf("FROM: ") != -1) { + line = line.substring(6); + String[] parts = new String[2]; + parts = line.split("\\*", 2); + fromDir = parts[0]; + fromExt = parts[1]; + } else if (!(line.matches("^\\s*$"))) + add(line); + /* Add the lines to the vector, ignoring the blank ones. */ + while ((line = bfr.readLine()) != null) { + if (!(line.matches("^\\s*$"))) + add(line); + } + } + + /*****************/ + /* OTHER METHODS */ + /*****************/ + + /** + * Adds a basename to the list. + */ + public void add(String str) { + if (!bList.contains(str)) + bList.add(str); + hasChanged = true; + } + + /** + * Adds an array of basenames to the list. + */ + public void add(String[] str) { + for (int i = 0; i < str.length; i++) + add(str[i]); + hasChanged = true; + } + + /** + * Removes a basename from the list, if it was present. + * + * @param str + * The basename to remove. + * @return true if the list was containing the basename. + */ + public boolean remove(String str) { + hasChanged = true; + return (bList.remove(str)); + } + + /** + * Removes a list from another list. + * + * @param bnl + * The basename list to remove. + * @return true if the list was containing any element of the list to remove. + */ + public boolean remove(BasenameList bnl) { + boolean ret = true; + for (int i = 0; i < bnl.getLength(); i++) { + bList.remove(bnl.getName(i)); + } + hasChanged = true; + return (ret); + } + + /** + * Duplicates the list (i.e., emits an autonomous copy of it). + */ + public BasenameList duplicate() { + return (new BasenameList(this.fromDir, this.fromExt, (Vector) (this.bList.clone()))); + } + + /** + * Returns an autonomous sublist between fromIndex, inclusive, and toIndex, exclusive. + */ + public BasenameList subList(int fromIndex, int toIndex) { + Vector subVec = new Vector(toIndex - fromIndex, DEFAULT_INCREMENT); + for (int i = fromIndex; i < toIndex; i++) + subVec.add(this.getName(i)); + return (new BasenameList(this.fromDir, this.fromExt, subVec)); + } + + /** + * An accessor for the list of basenames, returned as an array of strings + */ + public String[] getListAsArray() { + String[] ret = new String[this.getLength()]; + ret = (String[]) bList.toArray(ret); + return ((String[]) (ret)); + } + + /** + * Another accessor for the list of basenames, returned as a vector of strings + */ + public Vector getListAsVector() { + return (bList); + } + + /** + * An accessor for the list's length + */ + public int getLength() { + return (bList.size()); + } + + /** + * An accessor for the original directory. Returns null if the original directory is undefined. + */ + public String getDir() { + return (fromDir); + } + + /** + * An accessor for the original extension. Returns null if the original extension is undefined. + */ + public String getExt() { + return (fromExt); + } + + /** + * Return a copy of the basename at index i. + * + * @param i + * The index of the basename to consider. + * @return The corresponding basename. + */ + public String getName(int i) { + return (String) bList.elementAt(i); + } + + /** + * Check if the given basename is part of the list. + * + * @param str + * The basename to check for. + * @return true if yes, false if no. + */ + public boolean contains(String str) { + return (bList.contains(str)); + } + + /** + * Check if the list contains another given one. + * + * @param bnl + * The list of basenames to check for. + * @return true if yes, false if no. + */ + public boolean contains(BasenameList bnl) { + /* The list cannot contain a bigger one: */ + if (bnl.getLength() > this.getLength()) + return (false); + for (int i = 0; i < bnl.getLength(); i++) { + if (!this.contains(bnl.getName(i))) + return (false); + } + return (true); + } + + /** + * Check if two lists are equal. + * + * @param bnl + * The list of basenames to check for. + * @return true if yes, false if no. + */ + public boolean equals(BasenameList bnl) { + if (bnl.getLength() != this.getLength()) + return (false); + for (int i = 0; i < bnl.getLength(); i++) { + if (!this.contains(bnl.getName(i))) + return (false); + } + return (true); + } + + /** + * Ensure that the list is alphabetically sorted. + * + */ + public void sort() { + String[] str = getListAsArray(); + Arrays.sort(str); + bList.removeAllElements(); + add(str); + hasChanged = true; + } + + /** + * Clear the list. + * + */ + public void clear() { + fromDir = null; + fromExt = null; + bList.removeAllElements(); + hasChanged = true; + } + + public boolean hasChanged() { + return hasChanged; + } - /** - * Adds a basename to the list. - */ - public void add( String str ) { - if ( !bList.contains( str ) ) bList.add(str); - hasChanged = true; - } - - /** - * Adds an array of basenames to the list. - */ - public void add( String[] str ) { - for ( int i = 0; i < str.length; i++ ) add( str[i] ); - hasChanged = true; - } - - /** - * Removes a basename from the list, if it was present. - * - * @param str The basename to remove. - * @return true if the list was containing the basename. - */ - public boolean remove( String str ) { - hasChanged = true; - return( bList.remove( str ) ); - } - - /** - * Removes a list from another list. - * - * @param bnl The basename list to remove. - * @return true if the list was containing any element of the list to remove. - */ - public boolean remove( BasenameList bnl ) { - boolean ret = true; - for ( int i = 0; i < bnl.getLength(); i++ ) { - bList.remove( bnl.getName(i) ); - } - hasChanged = true; - return( ret ); - } - - /** - * Duplicates the list (i.e., emits an autonomous copy of it). - */ - public BasenameList duplicate() { - return( new BasenameList( this.fromDir, this.fromExt, (Vector)(this.bList.clone()) ) ); - } - - /** - * Returns an autonomous sublist between fromIndex, inclusive, and toIndex, exclusive. - */ - public BasenameList subList( int fromIndex, int toIndex ) { - Vector subVec = new Vector( toIndex - fromIndex, DEFAULT_INCREMENT ); - for ( int i = fromIndex; i < toIndex; i++ ) subVec.add( this.getName(i) ); - return( new BasenameList( this.fromDir, this.fromExt, subVec ) ); - } - - /** - * An accessor for the list of basenames, returned as an array of strings - */ - public String[] getListAsArray() { - String[] ret = new String[this.getLength()]; - ret = (String[]) bList.toArray( ret ); - return( (String[])( ret ) ); - } - - /** - * Another accessor for the list of basenames, returned as a vector of strings - */ - public Vector getListAsVector() { - return( bList ); - } - - /** - * An accessor for the list's length - */ - public int getLength() { - return( bList.size() ); - } - - /** - * An accessor for the original directory. Returns null if the original - * directory is undefined. - */ - public String getDir() { - return( fromDir ); - } - - /** - * An accessor for the original extension. Returns null if the original - * extension is undefined. - */ - public String getExt() { - return( fromExt ); - } - - /** - * Return a copy of the basename at index i. - * - * @param i The index of the basename to consider. - * @return The corresponding basename. - */ - public String getName( int i ) { - return (String) bList.elementAt(i); - } - - /** - * Check if the given basename is part of the list. - * - * @param str The basename to check for. - * @return true if yes, false if no. - */ - public boolean contains( String str ) { - return( bList.contains( str ) ); - } - - /** - * Check if the list contains another given one. - * - * @param bnl The list of basenames to check for. - * @return true if yes, false if no. - */ - public boolean contains( BasenameList bnl ) { - /* The list cannot contain a bigger one: */ - if ( bnl.getLength() > this.getLength() ) return( false ); - for ( int i = 0; i < bnl.getLength(); i++ ) { - if ( !this.contains( bnl.getName(i) ) ) return( false ); - } - return( true ); - } - - /** - * Check if two lists are equal. - * - * @param bnl The list of basenames to check for. - * @return true if yes, false if no. - */ - public boolean equals( BasenameList bnl ) { - if ( bnl.getLength() != this.getLength() ) return( false ); - for ( int i = 0; i < bnl.getLength(); i++ ) { - if ( !this.contains( bnl.getName(i) ) ) return( false ); - } - return( true ); - } - - /** - * Ensure that the list is alphabetically sorted. - * - */ - public void sort() { - String[] str = getListAsArray(); - Arrays.sort( str ); - bList.removeAllElements(); - add( str ); - hasChanged = true; - } - - /** - * Clear the list. - * - */ - public void clear() { - fromDir = null; - fromExt = null; - bList.removeAllElements(); - hasChanged = true; - } - - public boolean hasChanged(){ - return hasChanged; - } - } \ No newline at end of file diff --git a/marytts-common/src/main/java/marytts/util/io/FileFilter.java b/marytts-common/src/main/java/marytts/util/io/FileFilter.java index 8de714a0..3697d76e 100644 --- a/marytts-common/src/main/java/marytts/util/io/FileFilter.java +++ b/marytts-common/src/main/java/marytts/util/io/FileFilter.java @@ -1,60 +1,56 @@ -/** - * Copyright 2007 DFKI GmbH. - * All Rights Reserved. Use is subject to license terms. - * - * Permission is hereby granted, free of charge, to use and distribute - * this software and its documentation without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of this work, and to - * permit persons to whom this work is furnished to do so, subject to - * the following conditions: - * - * 1. The code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * 2. Any modifications must be clearly marked as such. - * 3. Original authors' names are not deleted. - * 4. The authors' names are not used to endorse or promote products - * derived from this software without specific prior written - * permission. - * - * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE - * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF - * THIS SOFTWARE. - */ - -package marytts.util.io; - -import java.io.File; -import java.io.FilenameFilter; - - -/** - * @author oytun.turk - * - */ -public class FileFilter implements FilenameFilter -{ - private String extension; - - public FileFilter(String ext) - { - if (ext.startsWith(".") || ext.compareTo("*.*")==0) - extension = ext; - else - extension = "." + ext; - } - - public boolean accept(File dir, String name) - { - if (extension.compareTo("*.*")==0) - return true; - else - return name.endsWith(extension); - } - -} +/** + * Copyright 2007 DFKI GmbH. + * All Rights Reserved. Use is subject to license terms. + * + * Permission is hereby granted, free of charge, to use and distribute + * this software and its documentation without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of this work, and to + * permit persons to whom this work is furnished to do so, subject to + * the following conditions: + * + * 1. The code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Any modifications must be clearly marked as such. + * 3. Original authors' names are not deleted. + * 4. The authors' names are not used to endorse or promote products + * derived from this software without specific prior written + * permission. + * + * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE + * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + * THIS SOFTWARE. + */ + +package marytts.util.io; + +import java.io.File; +import java.io.FilenameFilter; + +/** + * @author oytun.turk + * + */ +public class FileFilter implements FilenameFilter { + private String extension; + + public FileFilter(String ext) { + if (ext.startsWith(".") || ext.compareTo("*.*") == 0) + extension = ext; + else + extension = "." + ext; + } + + public boolean accept(File dir, String name) { + if (extension.compareTo("*.*") == 0) + return true; + else + return name.endsWith(extension); + } + +} diff --git a/marytts-common/src/main/java/marytts/util/io/General.java b/marytts-common/src/main/java/marytts/util/io/General.java index 48f71a37..471af351 100644 --- a/marytts-common/src/main/java/marytts/util/io/General.java +++ b/marytts-common/src/main/java/marytts/util/io/General.java @@ -40,593 +40,605 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; - /** - * This class is for general purpose functions such as reading and - * writing from files, or converting formats of numbers. + * This class is for general purpose functions such as reading and writing from files, or converting formats of numbers. */ -public class General -{ +public class General { - /** - * Reads the next word (text separated by whitespace) from the - * given stream - * - * @param dis the input stream - * - * @return the next word - * - * @throws IOException on error - */ - public static String readWord(DataInputStream dis) throws IOException { - StringBuilder sb = new StringBuilder(); - char c; + /** + * Reads the next word (text separated by whitespace) from the given stream + * + * @param dis + * the input stream + * + * @return the next word + * + * @throws IOException + * on error + */ + public static String readWord(DataInputStream dis) throws IOException { + StringBuilder sb = new StringBuilder(); + char c; - // skip leading whitespace - do { - c = readChar(dis); - } while(Character.isWhitespace(c)); + // skip leading whitespace + do { + c = readChar(dis); + } while (Character.isWhitespace(c)); - // read the word - do { - sb.append(c); - c = readChar(dis); - } while (!Character.isWhitespace(c)); - return sb.toString(); - } + // read the word + do { + sb.append(c); + c = readChar(dis); + } while (!Character.isWhitespace(c)); + return sb.toString(); + } - /** - * Reads a single char from the stream - * - * @param dis the stream to read - * @return the next character on the stream - * - * @throws IOException if an error occurs - */ - public static char readChar(DataInputStream dis) throws IOException { - return (char) dis.readByte(); - } + /** + * Reads a single char from the stream + * + * @param dis + * the stream to read + * @return the next character on the stream + * + * @throws IOException + * if an error occurs + */ + public static char readChar(DataInputStream dis) throws IOException { + return (char) dis.readByte(); + } - /** - * Reads a given number of chars from the stream - * - * @param dis the stream to read - * @param num the number of chars to read - * @return a character array containing the next num - * in the stream - * - * @throws IOException if an error occurs - */ - public static char[] readChars(DataInputStream dis, int num) - throws IOException { - char[] carray = new char[num]; - for (int i = 0; i < num; i++) { - carray[i] = readChar(dis); - } - return carray; - } + /** + * Reads a given number of chars from the stream + * + * @param dis + * the stream to read + * @param num + * the number of chars to read + * @return a character array containing the next num + * in the stream + * + * @throws IOException + * if an error occurs + */ + public static char[] readChars(DataInputStream dis, int num) throws IOException { + char[] carray = new char[num]; + for (int i = 0; i < num; i++) { + carray[i] = readChar(dis); + } + return carray; + } - /** - * Read a float from the input stream, byte-swapping as - * necessary - * - * @param dis the inputstream - * @param isBigEndian whether or not the data being read in is in - * big endian format. - * - * @return a floating pint value - * - * @throws IOException on error - */ - public static float readFloat(DataInputStream dis, boolean isBigEndian) - throws IOException { - float val; - if (!isBigEndian) { - val = readLittleEndianFloat(dis); - } else { - val = dis.readFloat(); - } - return val; - } + /** + * Read a float from the input stream, byte-swapping as necessary + * + * @param dis + * the inputstream + * @param isBigEndian + * whether or not the data being read in is in big endian format. + * + * @return a floating pint value + * + * @throws IOException + * on error + */ + public static float readFloat(DataInputStream dis, boolean isBigEndian) throws IOException { + float val; + if (!isBigEndian) { + val = readLittleEndianFloat(dis); + } else { + val = dis.readFloat(); + } + return val; + } - /** - * Write a float from the output stream, byte-swapping as - * necessary - * - * @param dos the outputstream - * @param isBigEndian whether or not the data being read in is in - * big endian format. - * @param val the floating point value to write - * - * @throws IOException on error - */ - public static void writeFloat(DataOutputStream dos, boolean isBigEndian, float val) - throws IOException { - if (!isBigEndian) { - writeLittleEndianFloat(dos,val); - } else { - dos.writeFloat( val ); - } - } + /** + * Write a float from the output stream, byte-swapping as necessary + * + * @param dos + * the outputstream + * @param isBigEndian + * whether or not the data being read in is in big endian format. + * @param val + * the floating point value to write + * + * @throws IOException + * on error + */ + public static void writeFloat(DataOutputStream dos, boolean isBigEndian, float val) throws IOException { + if (!isBigEndian) { + writeLittleEndianFloat(dos, val); + } else { + dos.writeFloat(val); + } + } - /** - * Reads the next float from the given DataInputStream, - * where the data is in little endian. - * - * @param dataStream the DataInputStream to read from - * - * @return a float - */ - public static float readLittleEndianFloat(DataInputStream dataStream) - throws IOException { - return Float.intBitsToFloat(readLittleEndianInt(dataStream)); - } + /** + * Reads the next float from the given DataInputStream, where the data is in little endian. + * + * @param dataStream + * the DataInputStream to read from + * + * @return a float + */ + public static float readLittleEndianFloat(DataInputStream dataStream) throws IOException { + return Float.intBitsToFloat(readLittleEndianInt(dataStream)); + } - /** - * Writes a float to the given DataOutputStream, - * where the data is in little endian. - * - * @param dataStream the DataOutputStream to write to. - * @param val The float value to write. - */ - public static void writeLittleEndianFloat(DataOutputStream dataStream,float val) - throws IOException { - writeLittleEndianInt( dataStream, Float.floatToRawIntBits(val) ); - } + /** + * Writes a float to the given DataOutputStream, where the data is in little endian. + * + * @param dataStream + * the DataOutputStream to write to. + * @param val + * The float value to write. + */ + public static void writeLittleEndianFloat(DataOutputStream dataStream, float val) throws IOException { + writeLittleEndianInt(dataStream, Float.floatToRawIntBits(val)); + } - /** - * Read an integer from the input stream, byte-swapping as - * necessary - * - * @param dis the inputstream - * @param isBigEndian whether or not the data being read in is in - * big endian format. - * - * @return an integer value - * - * @throws IOException on error - */ - public static int readInt(DataInputStream dis, boolean isBigEndian) - throws IOException { - if (!isBigEndian) { - return readLittleEndianInt(dis); - } else { - return dis.readInt(); - } - } + /** + * Read an integer from the input stream, byte-swapping as necessary + * + * @param dis + * the inputstream + * @param isBigEndian + * whether or not the data being read in is in big endian format. + * + * @return an integer value + * + * @throws IOException + * on error + */ + public static int readInt(DataInputStream dis, boolean isBigEndian) throws IOException { + if (!isBigEndian) { + return readLittleEndianInt(dis); + } else { + return dis.readInt(); + } + } - /** - * Writes an integer to the output stream, byte-swapping as - * necessary - * - * @param dis the outputstream. - * @param isBigEndian whether or not the data being read in is in - * big endian format. - * @param val the integer value to write. - * - * @throws IOException on error - */ - public static void writeInt(DataOutputStream dis, boolean isBigEndian, int val) - throws IOException { - if (!isBigEndian) { - writeLittleEndianInt(dis,val); - } else { - dis.writeInt(val); - } - } + /** + * Writes an integer to the output stream, byte-swapping as necessary + * + * @param dis + * the outputstream. + * @param isBigEndian + * whether or not the data being read in is in big endian format. + * @param val + * the integer value to write. + * + * @throws IOException + * on error + */ + public static void writeInt(DataOutputStream dis, boolean isBigEndian, int val) throws IOException { + if (!isBigEndian) { + writeLittleEndianInt(dis, val); + } else { + dis.writeInt(val); + } + } - /** - * Reads the next little-endian integer from the given DataInputStream. - * - * @param dataStream the DataInputStream to read from - * - * @return an integer - */ - public static int readLittleEndianInt(DataInputStream dataStream) - throws IOException { - int bits = 0x00000000; - for (int shift = 0; shift < 32; shift += 8) { - int byteRead = (0x000000ff & dataStream.readByte()); - bits |= (byteRead << shift); - } - return bits; - } + /** + * Reads the next little-endian integer from the given DataInputStream. + * + * @param dataStream + * the DataInputStream to read from + * + * @return an integer + */ + public static int readLittleEndianInt(DataInputStream dataStream) throws IOException { + int bits = 0x00000000; + for (int shift = 0; shift < 32; shift += 8) { + int byteRead = (0x000000ff & dataStream.readByte()); + bits |= (byteRead << shift); + } + return bits; + } - /** - * Writes a little-endian integer to the given DataOutputStream. - * - * @param dataStream the DataOutputStream to write to - * @param val the integer value to write - * - * @throws IOException on error - */ - public static void writeLittleEndianInt(DataOutputStream dataStream, int val) - throws IOException { - int mask = 0x000000ff; - for (int shift = 0; shift < 32; shift += 8) { - dataStream.writeByte( mask & (val >> shift) ); - } - } + /** + * Writes a little-endian integer to the given DataOutputStream. + * + * @param dataStream + * the DataOutputStream to write to + * @param val + * the integer value to write + * + * @throws IOException + * on error + */ + public static void writeLittleEndianInt(DataOutputStream dataStream, int val) throws IOException { + int mask = 0x000000ff; + for (int shift = 0; shift < 32; shift += 8) { + dataStream.writeByte(mask & (val >> shift)); + } + } - /** - * Read a short from the input stream, byte-swapping as - * necessary - * - * @param dis the inputstream - * @param isBigEndian whether or not the data being read in is in - * big endian format. - * - * @return an integer value - * - * @throws IOException on error - */ - public static short readShort(DataInputStream dis, boolean isBigEndian) - throws IOException { - if (!isBigEndian) { - return readLittleEndianShort(dis); - } else { - return dis.readShort(); - } - } + /** + * Read a short from the input stream, byte-swapping as necessary + * + * @param dis + * the inputstream + * @param isBigEndian + * whether or not the data being read in is in big endian format. + * + * @return an integer value + * + * @throws IOException + * on error + */ + public static short readShort(DataInputStream dis, boolean isBigEndian) throws IOException { + if (!isBigEndian) { + return readLittleEndianShort(dis); + } else { + return dis.readShort(); + } + } - /** - * Reads the next little-endian short from the given DataInputStream. - * - * @param dataStream the DataInputStream to read from - * - * @return a short - */ - public static short readLittleEndianShort(DataInputStream dis) - throws IOException { - short bits = (short)(0x0000ff & dis.readByte()); - bits |= (((short)(0x0000ff & dis.readByte())) << 8); - return bits; - } + /** + * Reads the next little-endian short from the given DataInputStream. + * + * @param dataStream + * the DataInputStream to read from + * + * @return a short + */ + public static short readLittleEndianShort(DataInputStream dis) throws IOException { + short bits = (short) (0x0000ff & dis.readByte()); + bits |= (((short) (0x0000ff & dis.readByte())) << 8); + return bits; + } - /** - * Convert a short to ulaw format - * - * @param sample the short to convert - * - * @return a short containing an unsigned 8-bit quantity - * representing the ulaw - */ - public static byte shortToUlaw(short sample) { - final int[] exp_lut = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3, - 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, - 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}; + /** + * Convert a short to ulaw format + * + * @param sample + * the short to convert + * + * @return a short containing an unsigned 8-bit quantity representing the ulaw + */ + public static byte shortToUlaw(short sample) { + final int[] exp_lut = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 }; - int sign, exponent, mantissa; - short ulawbyte; + int sign, exponent, mantissa; + short ulawbyte; - final short CLIP = 32635; - final short BIAS = 0x0084; + final short CLIP = 32635; + final short BIAS = 0x0084; - /* Get the sample into sign-magnitude. */ - sign = (sample >> 8) & 0x80; /* set aside the sign */ - if ( sign != 0 ) { - sample = (short) -sample; /* get magnitude */ - } - if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */ + /* Get the sample into sign-magnitude. */ + sign = (sample >> 8) & 0x80; /* set aside the sign */ + if (sign != 0) { + sample = (short) -sample; /* get magnitude */ + } + if (sample > CLIP) + sample = CLIP; /* clip the magnitude */ - /* Convert from 16 bit linear to ulaw. */ - sample = (short) (sample + BIAS); - exponent = exp_lut[( sample >> 7 ) & 0xFF]; - mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F; - ulawbyte = (short) - ((~ ( sign | ( exponent << 4 ) | mantissa)) & 0x00FF); - if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */ - // Now ulawbyte is an unsigned 8-bit entity. - // Return as a (signed) byte: - return (byte) (ulawbyte-128); - } + /* Convert from 16 bit linear to ulaw. */ + sample = (short) (sample + BIAS); + exponent = exp_lut[(sample >> 7) & 0xFF]; + mantissa = (sample >> (exponent + 3)) & 0x0F; + ulawbyte = (short) ((~(sign | (exponent << 4) | mantissa)) & 0x00FF); + if (ulawbyte == 0) + ulawbyte = 0x02; /* optional CCITT trap */ + // Now ulawbyte is an unsigned 8-bit entity. + // Return as a (signed) byte: + return (byte) (ulawbyte - 128); + } - /** - * Convert a ulaw format to short - * - * @param ulaw a (signed) byte which, after converting into a short and - * adding 128, will be an unsigned 8-but quantity representing a ulaw - * - * @return the short equivalent of the ulaw - */ - public static short ulawToShort(byte ulaw) { - short ulawbyte = (short) (ulaw + 128); - final int[] exp_lut = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 }; - int sign, exponent, mantissa; - short sample; + /** + * Convert a ulaw format to short + * + * @param ulaw + * a (signed) byte which, after converting into a short and adding 128, will be an unsigned 8-but quantity + * representing a ulaw + * + * @return the short equivalent of the ulaw + */ + public static short ulawToShort(byte ulaw) { + short ulawbyte = (short) (ulaw + 128); + final int[] exp_lut = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 }; + int sign, exponent, mantissa; + short sample; - ulawbyte = (short) (ulawbyte & 0x00FF); - ulawbyte = (short) (~ulawbyte); - sign = ( ulawbyte & ((short) 0x80) ); - exponent = (int) ( (ulawbyte & (short) 0x00FF) >> 4 ) & 0x07; - mantissa = ulawbyte & (short) 0x0F; - sample = (short) (exp_lut[exponent] + (mantissa << (exponent + 3))); - if ( sign != 0 ) sample = (short) (-sample); + ulawbyte = (short) (ulawbyte & 0x00FF); + ulawbyte = (short) (~ulawbyte); + sign = (ulawbyte & ((short) 0x80)); + exponent = (int) ((ulawbyte & (short) 0x00FF) >> 4) & 0x07; + mantissa = ulawbyte & (short) 0x0F; + sample = (short) (exp_lut[exponent] + (mantissa << (exponent + 3))); + if (sign != 0) + sample = (short) (-sample); - return sample; - } + return sample; + } - /** - * Convert an array from short to ulaw. - * @param samples an array in linear representation - * @return an array in ulaw representation. - * @see #shortToUlaw(short) - */ - public static byte[] shortToUlaw(short[] samples) - { - if (samples == null) return null; - byte[] ulaw = new byte[samples.length]; - for (int i=0; if - */ - public static String hex(float f) { - return Integer.toHexString(Float.floatToIntBits(f)); - } + /** + * Convert an array from ulaw to short. + * + * @param samples + * an array in ulaw representation + * @return an array in linear representation. + * @see #ulawToShort(byte) + */ + public static short[] ulawToShort(byte[] ulaw) { + if (ulaw == null) + return null; + short[] samples = new short[ulaw.length]; + for (int i = 0; i < ulaw.length; i++) { + samples[i] = ulawToShort(ulaw[i]); + } + return samples; + } + /** + * Print a float type's internal bit representation in hex + * + * @param f + * the float to print + * + * @return a string containing the hex value of f + */ + public static String hex(float f) { + return Integer.toHexString(Float.floatToIntBits(f)); + } - /** - * Quantize a float variable over the 16bits signed short range - * - * @param f the float to quantize - * @param min the minimum possible value for variable f - * @param range the possible range for variable f - * - * @return the 16bits signed codeword, returned as a signed short - * - * @author Sacha K. - */ - public static short quantize( float f, float fMin, float fRange ) { - return( (short)( ( (double)f - (double)fMin ) * 65535.0/((double)fRange) - 32768.0) ); - } - + /** + * Quantize a float variable over the 16bits signed short range + * + * @param f + * the float to quantize + * @param min + * the minimum possible value for variable f + * @param range + * the possible range for variable f + * + * @return the 16bits signed codeword, returned as a signed short + * + * @author Sacha K. + */ + public static short quantize(float f, float fMin, float fRange) { + return ((short) (((double) f - (double) fMin) * 65535.0 / ((double) fRange) - 32768.0)); + } - /** - * Quantize an array of floats over the 16bits signed short range - * - * @param f the array of floats to quantize - * @param min the minimum possible value for variable f - * @param range the possible range for variable f - * - * @return an array of 16bits signed codewords, returned as signed shorts - * - * @author Sacha K. - */ - public static short[] quantize( float[] f, float fMin, float fRange ) { - - int len = f.length; - short[] ret = new short[len]; - - for( int i = 0; i < len; i++ ) ret[i] = quantize( f[i], fMin, fRange ); - - return( ret ); - } - + /** + * Quantize an array of floats over the 16bits signed short range + * + * @param f + * the array of floats to quantize + * @param min + * the minimum possible value for variable f + * @param range + * the possible range for variable f + * + * @return an array of 16bits signed codewords, returned as signed shorts + * + * @author Sacha K. + */ + public static short[] quantize(float[] f, float fMin, float fRange) { - /** - * Unquantize a 16bits signed short over a float range - * - * @param s the 16bits signed codeword - * @param min the minimum possible value for variable f - * @param range the possible range for variable f - * - * @return the corresponding float value - * - * @author Sacha K. - */ - public static float unQuantize( short s, float fMin, float fRange ) { - return( (float)( ((double)(s) + 32768.0) * (double)fRange / 65535.0 - (double)fMin ) ); - } - - /** - * Unquantize an array of 16bits signed shorts over a float range - * - * @param s the array of 16bits signed codewords - * @param min the minimum possible value for variable f - * @param range the possible range for variable f - * - * @return the corresponding array of float values - * - * @author Sacha K. - */ - public static float[] unQuantize( short[] s, float fMin, float fRange ) { - - int len = s.length; - float[] ret = new float[len]; - - for( int i = 0; i < len; i++ ) ret[i] = unQuantize( s[i], fMin, fRange ); - - return( ret ); - } - - - - /** - * A general process launcher for the various tasks - * - * @param cmdLine the command line to be launched. - * @param task a task tag for error messages, such as "Pitchmarks" or "LPC". - * @param the basename of the file currently processed, for error messages. - */ - public static void launchProc( String cmdLine, String task, String baseName ) { - - Process proc = null; - String line = null; - // String[] cmd = null; // Java 5.0 compliant code - - try { - /* Java 5.0 compliant code below. */ - /* Hook the command line to the process builder: */ - /* cmd = cmdLine.split( " " ); - pb.command( cmd ); /* - /* Launch the process: */ - /*proc = pb.start(); */ - - /* Java 1.0 equivalent: */ - proc = Runtime.getRuntime().exec( cmdLine ); - - /* Collect stdout and send it to System.out: */ - InputStream procStdOut = proc.getInputStream(); - InputStream procStdErr = proc.getErrorStream(); - - StreamLogger stdOutLogger = new StreamLogger(procStdOut, System.out); - StreamLogger stdErrLogger = new StreamLogger(procStdErr, System.err); - - stdOutLogger.start(); - stdErrLogger.start(); - - try { - stdOutLogger.join(); - stdErrLogger.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + int len = f.length; + short[] ret = new short[len]; - /* Wait and check the exit value */ - proc.waitFor(); - - if ( proc.exitValue() != 0 ) { - throw new RuntimeException( task + " computation failed on file [" + baseName + "]!\n" - + "Command line was: [" + cmdLine + "]." ); - } - } - catch ( IOException e ) { - throw new RuntimeException( task + " computation provoked an IOException on file [" + baseName + "].", e ); - } - catch ( InterruptedException e ) { - throw new RuntimeException( task + " computation interrupted on file [" + baseName + "].", e ); - } - - } - - /** - * A general process launcher for the various tasks but using an intermediate batch file - * @param cmdLine the command line to be launched. - * @param task a task tag for error messages, such as "Pitchmarks" or "LPC". - * @param the filedir of the file currently processed, for error messages and for creating a - * temporal batch file. - */ - public static void launchBatchProc( String cmdLine, String task, String filedir ) { - - Process proc = null; - Process proctmp = null; - BufferedReader procStdout = null; - String line = null; - String tmpFile = filedir + "tmp.bat"; - System.out.println("Running: "+ cmdLine); - // String[] cmd = null; // Java 5.0 compliant code - - try { - FileWriter tmp = new FileWriter(tmpFile); - tmp.write(cmdLine); - tmp.close(); - - /* make it executable... */ - proctmp = Runtime.getRuntime().exec( "chmod +x "+tmpFile ); - proctmp.waitFor(); - if ( proctmp.exitValue() != 0 ) { - BufferedReader errReader = new BufferedReader(new InputStreamReader(proctmp.getErrorStream())); - while ((line = errReader.readLine()) != null) { - System.err.println("ERR> "+line); - } - errReader.close(); - throw new RuntimeException( task + " computation failed on file [" + filedir + "]!\n" - + "Command line was: [chmod +x " + tmpFile + "]." ); - } - - /* Java 5.0 compliant code below. */ - /* Hook the command line to the process builder: */ - /* cmd = cmdLine.split( " " ); - pb.command( cmd ); /* - /* Launch the process: */ - /*proc = pb.start(); */ - - /* Java 1.0 equivalent: */ - proc = Runtime.getRuntime().exec( tmpFile ); - - InputStream procStdOut = proc.getInputStream(); - InputStream procStdErr = proc.getErrorStream(); - - StreamLogger stdOutLogger = new StreamLogger(procStdOut, System.out); - StreamLogger stdErrLogger = new StreamLogger(procStdErr, System.err); - - stdOutLogger.start(); - stdErrLogger.start(); - - try { - stdOutLogger.join(); - stdErrLogger.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + for (int i = 0; i < len; i++) + ret[i] = quantize(f[i], fMin, fRange); - /* Wait and check the exit value */ - proc.waitFor(); - if ( proc.exitValue() != 0 ) { - BufferedReader errReader = new BufferedReader(new InputStreamReader(proc.getErrorStream())); - while ((line = errReader.readLine()) != null) { - System.err.println("ERR> "+line); - } - errReader.close(); - throw new RuntimeException( task + " computation failed on file [" + filedir + "]!\n" - + "Command line was: [" + cmdLine + "]." ); - } - - // Delete tmp.bat if created - File batchFile = new File(tmpFile); - if(batchFile.exists()){ - batchFile.delete(); - } - - - } - catch ( IOException e ) { - throw new RuntimeException( task + " computation provoked an IOException on file [" + filedir + "].", e ); - } - catch ( InterruptedException e ) { - throw new RuntimeException( task + " computation interrupted on file [" + filedir + "].", e ); - } - - } + return (ret); + } + + /** + * Unquantize a 16bits signed short over a float range + * + * @param s + * the 16bits signed codeword + * @param min + * the minimum possible value for variable f + * @param range + * the possible range for variable f + * + * @return the corresponding float value + * + * @author Sacha K. + */ + public static float unQuantize(short s, float fMin, float fRange) { + return ((float) (((double) (s) + 32768.0) * (double) fRange / 65535.0 - (double) fMin)); + } + + /** + * Unquantize an array of 16bits signed shorts over a float range + * + * @param s + * the array of 16bits signed codewords + * @param min + * the minimum possible value for variable f + * @param range + * the possible range for variable f + * + * @return the corresponding array of float values + * + * @author Sacha K. + */ + public static float[] unQuantize(short[] s, float fMin, float fRange) { + + int len = s.length; + float[] ret = new float[len]; + + for (int i = 0; i < len; i++) + ret[i] = unQuantize(s[i], fMin, fRange); + + return (ret); + } + + /** + * A general process launcher for the various tasks + * + * @param cmdLine + * the command line to be launched. + * @param task + * a task tag for error messages, such as "Pitchmarks" or "LPC". + * @param the + * basename of the file currently processed, for error messages. + */ + public static void launchProc(String cmdLine, String task, String baseName) { + + Process proc = null; + String line = null; + // String[] cmd = null; // Java 5.0 compliant code + + try { + /* Java 5.0 compliant code below. */ + /* Hook the command line to the process builder: */ + /* + * cmd = cmdLine.split( " " ); pb.command( cmd ); /* /* Launch the process: + */ + /* proc = pb.start(); */ + + /* Java 1.0 equivalent: */ + proc = Runtime.getRuntime().exec(cmdLine); + + /* Collect stdout and send it to System.out: */ + InputStream procStdOut = proc.getInputStream(); + InputStream procStdErr = proc.getErrorStream(); + + StreamLogger stdOutLogger = new StreamLogger(procStdOut, System.out); + StreamLogger stdErrLogger = new StreamLogger(procStdErr, System.err); + + stdOutLogger.start(); + stdErrLogger.start(); + + try { + stdOutLogger.join(); + stdErrLogger.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + /* Wait and check the exit value */ + proc.waitFor(); + + if (proc.exitValue() != 0) { + throw new RuntimeException(task + " computation failed on file [" + baseName + "]!\n" + "Command line was: [" + + cmdLine + "]."); + } + } catch (IOException e) { + throw new RuntimeException(task + " computation provoked an IOException on file [" + baseName + "].", e); + } catch (InterruptedException e) { + throw new RuntimeException(task + " computation interrupted on file [" + baseName + "].", e); + } + + } + + /** + * A general process launcher for the various tasks but using an intermediate batch file + * + * @param cmdLine + * the command line to be launched. + * @param task + * a task tag for error messages, such as "Pitchmarks" or "LPC". + * @param the + * filedir of the file currently processed, for error messages and for creating a temporal batch file. + */ + public static void launchBatchProc(String cmdLine, String task, String filedir) { + + Process proc = null; + Process proctmp = null; + BufferedReader procStdout = null; + String line = null; + String tmpFile = filedir + "tmp.bat"; + System.out.println("Running: " + cmdLine); + // String[] cmd = null; // Java 5.0 compliant code + + try { + FileWriter tmp = new FileWriter(tmpFile); + tmp.write(cmdLine); + tmp.close(); + + /* make it executable... */ + proctmp = Runtime.getRuntime().exec("chmod +x " + tmpFile); + proctmp.waitFor(); + if (proctmp.exitValue() != 0) { + BufferedReader errReader = new BufferedReader(new InputStreamReader(proctmp.getErrorStream())); + while ((line = errReader.readLine()) != null) { + System.err.println("ERR> " + line); + } + errReader.close(); + throw new RuntimeException(task + " computation failed on file [" + filedir + "]!\n" + + "Command line was: [chmod +x " + tmpFile + "]."); + } + + /* Java 5.0 compliant code below. */ + /* Hook the command line to the process builder: */ + /* + * cmd = cmdLine.split( " " ); pb.command( cmd ); /* /* Launch the process: + */ + /* proc = pb.start(); */ + + /* Java 1.0 equivalent: */ + proc = Runtime.getRuntime().exec(tmpFile); + + InputStream procStdOut = proc.getInputStream(); + InputStream procStdErr = proc.getErrorStream(); + + StreamLogger stdOutLogger = new StreamLogger(procStdOut, System.out); + StreamLogger stdErrLogger = new StreamLogger(procStdErr, System.err); + + stdOutLogger.start(); + stdErrLogger.start(); + + try { + stdOutLogger.join(); + stdErrLogger.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + /* Wait and check the exit value */ + proc.waitFor(); + if (proc.exitValue() != 0) { + BufferedReader errReader = new BufferedReader(new InputStreamReader(proc.getErrorStream())); + while ((line = errReader.readLine()) != null) { + System.err.println("ERR> " + line); + } + errReader.close(); + throw new RuntimeException(task + " computation failed on file [" + filedir + "]!\n" + "Command line was: [" + + cmdLine + "]."); + } + + // Delete tmp.bat if created + File batchFile = new File(tmpFile); + if (batchFile.exists()) { + batchFile.delete(); + } + + } catch (IOException e) { + throw new RuntimeException(task + " computation provoked an IOException on file [" + filedir + "].", e); + } catch (InterruptedException e) { + throw new RuntimeException(task + " computation interrupted on file [" + filedir + "].", e); + } + + } - } - diff --git a/marytts-common/src/main/java/marytts/util/io/LEDataInputStream.java b/marytts-common/src/main/java/marytts/util/io/LEDataInputStream.java index 477d4e4b..563195f2 100644 --- a/marytts-common/src/main/java/marytts/util/io/LEDataInputStream.java +++ b/marytts-common/src/main/java/marytts/util/io/LEDataInputStream.java @@ -17,437 +17,409 @@ * along with this program. If not, see . * */ -package marytts.util.io; - -/** - * LEDataInputStream.java - * copyright (c) 1998-2007 Roedy Green, Canadian Mind* Products - * Very similar to DataInputStream except it reads - * little-endian instead of big-endian binary data. We can't extend - * DataInputStream directly since it has only final methods, though - * DataInputStream itself is not final. This forces us implement - * LEDataInputStream with a DataInputStream object, and use wrapper methods. - */ - +package marytts.util.io; + +/** + * LEDataInputStream.java + * copyright (c) 1998-2007 Roedy Green, Canadian Mind* Products + * Very similar to DataInputStream except it reads + * little-endian instead of big-endian binary data. We can't extend + * DataInputStream directly since it has only final methods, though + * DataInputStream itself is not final. This forces us implement + * LEDataInputStream with a DataInputStream object, and use wrapper methods. + */ + import java.io.DataInput; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; - -/** - * reads little endian binary data . - */ -public class LEDataInputStream implements DataInput { - - // ------------------------------ FIELDS ------------------------------ - - /** - * undisplayed copyright notice. - * - * @noinspection UnusedDeclaration - */ - private static final String EMBEDDEDCOPYRIGHT = - "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com"; - - /** - * to get at the big-Endian methods of a basic DataInputStream - * - * @noinspection WeakerAccess - */ - protected final DataInputStream dis; - - /** - * to get at the a basic readBytes method. - * - * @noinspection WeakerAccess - */ - protected final InputStream is; - - /** - * work array for buffering input. - * - * @noinspection WeakerAccess - */ - protected final byte[] work; - - // -------------------------- PUBLIC STATIC METHODS -------------------------- - - /** - * Note. This is a STATIC method! - * - * @param in stream to read UTF chars from (endian irrelevant) - * - * @return string from stream - * - * @throws IOException if read fails. - */ - public static String readUTF( DataInput in ) throws IOException - { - return DataInputStream.readUTF( in ); - } - - // -------------------------- PUBLIC INSTANCE METHODS -------------------------- - /** - * constructor. - * - * @param in binary inputstream of little-endian data. - */ - public LEDataInputStream( InputStream in ) - { - this.is = in; - this.dis = new DataInputStream( in ); - work = new byte[8]; - } - - public LEDataInputStream( String filename) throws FileNotFoundException - { - this(new FileInputStream(filename)); - } - - /** - * close. - * - * @throws IOException if close fails. - */ - public final void close() throws IOException - { - dis.close(); - } - - /** - * Read bytes. Watch out, read may return fewer bytes than requested. - * - * @param ba where the bytes go. - * @param off offset in buffer, not offset in file. - * @param len count of bytes to read. - * - * @return how many bytes read. - * - * @throws IOException if read fails. - */ - public final int read( byte ba[], int off, int len ) throws IOException - { - // For efficiency, we avoid one layer of wrapper - return is.read( ba, off, len ); - } - - /** - * read only a one-byte boolean. - * - * @return true or false. - * - * @throws IOException if read fails. - * @see java.io.DataInput#readBoolean() - */ - public final boolean readBoolean() throws IOException - { - return dis.readBoolean(); - } - - public final boolean [] readBoolean(int len) throws IOException - { - boolean [] ret = new boolean[len]; - - for (int i=0; iskipBytes method of DataInput. - *

- * Bytes for this operation are read from the contained input stream. - * - * @param n the number of bytes to be skipped. - * - * @return the actual number of bytes skipped. - * - * @throws IOException if an I/O error occurs. - */ - public final int skipBytes( int n ) throws IOException - { - return dis.skipBytes( n ); - } -}// end class LEDataInputStream + +/** + * reads little endian binary data . + */ +public class LEDataInputStream implements DataInput { + + // ------------------------------ FIELDS ------------------------------ + + /** + * undisplayed copyright notice. + * + * @noinspection UnusedDeclaration + */ + private static final String EMBEDDEDCOPYRIGHT = "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com"; + + /** + * to get at the big-Endian methods of a basic DataInputStream + * + * @noinspection WeakerAccess + */ + protected final DataInputStream dis; + + /** + * to get at the a basic readBytes method. + * + * @noinspection WeakerAccess + */ + protected final InputStream is; + + /** + * work array for buffering input. + * + * @noinspection WeakerAccess + */ + protected final byte[] work; + + // -------------------------- PUBLIC STATIC METHODS -------------------------- + + /** + * Note. This is a STATIC method! + * + * @param in + * stream to read UTF chars from (endian irrelevant) + * + * @return string from stream + * + * @throws IOException + * if read fails. + */ + public static String readUTF(DataInput in) throws IOException { + return DataInputStream.readUTF(in); + } + + // -------------------------- PUBLIC INSTANCE METHODS -------------------------- + /** + * constructor. + * + * @param in + * binary inputstream of little-endian data. + */ + public LEDataInputStream(InputStream in) { + this.is = in; + this.dis = new DataInputStream(in); + work = new byte[8]; + } + + public LEDataInputStream(String filename) throws FileNotFoundException { + this(new FileInputStream(filename)); + } + + /** + * close. + * + * @throws IOException + * if close fails. + */ + public final void close() throws IOException { + dis.close(); + } + + /** + * Read bytes. Watch out, read may return fewer bytes than requested. + * + * @param ba + * where the bytes go. + * @param off + * offset in buffer, not offset in file. + * @param len + * count of bytes to read. + * + * @return how many bytes read. + * + * @throws IOException + * if read fails. + */ + public final int read(byte ba[], int off, int len) throws IOException { + // For efficiency, we avoid one layer of wrapper + return is.read(ba, off, len); + } + + /** + * read only a one-byte boolean. + * + * @return true or false. + * + * @throws IOException + * if read fails. + * @see java.io.DataInput#readBoolean() + */ + public final boolean readBoolean() throws IOException { + return dis.readBoolean(); + } + + public final boolean[] readBoolean(int len) throws IOException { + boolean[] ret = new boolean[len]; + + for (int i = 0; i < len; i++) + ret[i] = readBoolean(); + + return ret; + } + + /** + * read byte. + * + * @return the byte read. + * + * @throws IOException + * if read fails. + * @see java.io.DataInput#readByte() + */ + public final byte readByte() throws IOException { + return dis.readByte(); + } + + public final byte[] readByte(int len) throws IOException { + byte[] ret = new byte[len]; + + for (int i = 0; i < len; i++) + ret[i] = readByte(); + + return ret; + } + + /** + * Read on char. like DataInputStream.readChar except little endian. + * + * @return little endian 16-bit unicode char from the stream. + * + * @throws IOException + * if read fails. + */ + public final char readChar() throws IOException { + dis.readFully(work, 0, 2); + return (char) ((work[1] & 0xff) << 8 | (work[0] & 0xff)); + } + + public final char[] readChar(int len) throws IOException { + char[] ret = new char[len]; + + for (int i = 0; i < len; i++) + ret[i] = readChar(); + + return ret; + } + + /** + * Read a double. like DataInputStream.readDouble except little endian. + * + * @return little endian IEEE double from the datastream. + * + * @throws IOException + */ + public final double readDouble() throws IOException { + return Double.longBitsToDouble(readLong()); + } + + public final double[] readDouble(int len) throws IOException { + double[] ret = new double[len]; + + for (int i = 0; i < len; i++) + ret[i] = readDouble(); + + return ret; + } + + public final int[] readDoubleToInt(int len) throws IOException { + int[] ret = new int[len]; + + for (int i = 0; i < len; i++) + ret[i] = (int) readDouble(); + + return ret; + } + + /** + * Read one float. Like DataInputStream.readFloat except little endian. + * + * @return little endian IEEE float from the datastream. + * + * @throws IOException + * if read fails. + */ + public final float readFloat() throws IOException { + return Float.intBitsToFloat(readInt()); + } + + public final float[] readFloat(int len) throws IOException { + float[] ret = new float[len]; + + for (int i = 0; i < len; i++) + ret[i] = readFloat(); + + return ret; + } + + /** + * Read bytes until the array is filled. + * + * @see java.io.DataInput#readFully(byte[]) + */ + public final void readFully(byte ba[]) throws IOException { + dis.readFully(ba, 0, ba.length); + } + + /** + * Read bytes until the count is satisfied. + * + * @throws IOException + * if read fails. + * @see java.io.DataInput#readFully(byte[],int,int) + */ + public final void readFully(byte ba[], int off, int len) throws IOException { + dis.readFully(ba, off, len); + } + + /** + * Read an int, 32-bits. Like DataInputStream.readInt except little endian. + * + * @return little-endian binary int from the datastream + * + * @throws IOException + * if read fails. + */ + public final int readInt() throws IOException { + dis.readFully(work, 0, 4); + return (work[3]) << 24 | (work[2] & 0xff) << 16 | (work[1] & 0xff) << 8 | (work[0] & 0xff); + } + + public final int[] readInt(int len) throws IOException { + int[] ret = new int[len]; + + for (int i = 0; i < len; i++) + ret[i] = readInt(); + + return ret; + } + + /** + * Read a line. + * + * @return a rough approximation of the 8-bit stream as a 16-bit unicode string + * + * @throws IOException + * @noinspection deprecation + * @deprecated This method does not properly convert bytes to characters. Use a Reader instead with a little-endian encoding. + */ + public final String readLine() throws IOException { + return dis.readLine(); + } + + /** + * read a long, 64-bits. Like DataInputStream.readLong except little endian. + * + * @return little-endian binary long from the datastream. + * + * @throws IOException + */ + public final long readLong() throws IOException { + dis.readFully(work, 0, 8); + return (long) (work[7]) << 56 | + /* long cast needed or shift done modulo 32 */ + (long) (work[6] & 0xff) << 48 | (long) (work[5] & 0xff) << 40 | (long) (work[4] & 0xff) << 32 + | (long) (work[3] & 0xff) << 24 | (long) (work[2] & 0xff) << 16 | (long) (work[1] & 0xff) << 8 + | (long) (work[0] & 0xff); + } + + public final long[] readLong(int len) throws IOException { + long[] ret = new long[len]; + + for (int i = 0; i < len; i++) + ret[i] = readLong(); + + return ret; + } + + /** + * Read short, 16-bits. Like DataInputStream.readShort except little endian. + * + * @return little endian binary short from stream. + * + * @throws IOException + * if read fails. + */ + public final short readShort() throws IOException { + dis.readFully(work, 0, 2); + return (short) ((work[1] & 0xff) << 8 | (work[0] & 0xff)); + } + + public final short[] readShort(int len) throws IOException { + short[] ret = new short[len]; + + for (int i = 0; i < len; i++) + ret[i] = readShort(); + + return ret; + } + + /** + * Read UTF counted string. + * + * @return String read. + */ + public final String readUTF() throws IOException { + return dis.readUTF(); + } + + /** + * Read an unsigned byte. Note: returns an int, even though says Byte (non-Javadoc) + * + * @throws IOException + * if read fails. + * @see java.io.DataInput#readUnsignedByte() + */ + public final int readUnsignedByte() throws IOException { + return dis.readUnsignedByte(); + } + + public final int[] readUnsignedByte(int len) throws IOException { + int[] ret = new int[len]; + + for (int i = 0; i < len; i++) + ret[i] = readUnsignedByte(); + + return ret; + } + + /** + * Read an unsigned short, 16 bits. Like DataInputStream.readUnsignedShort except little endian. Note, returns int even though + * it reads a short. + * + * @return little-endian int from the stream. + * + * @throws IOException + * if read fails. + */ + public final int readUnsignedShort() throws IOException { + dis.readFully(work, 0, 2); + return ((work[1] & 0xff) << 8 | (work[0] & 0xff)); + } + + public final int[] readUnsignedShort(int len) throws IOException { + int[] ret = new int[len]; + + for (int i = 0; i < len; i++) + ret[i] = readUnsignedShort(); + + return ret; + } + + /** + * Skip over bytes in the stream. See the general contract of the skipBytes method of DataInput. + *

+ * Bytes for this operation are read from the contained input stream. + * + * @param n + * the number of bytes to be skipped. + * + * @return the actual number of bytes skipped. + * + * @throws IOException + * if an I/O error occurs. + */ + public final int skipBytes(int n) throws IOException { + return dis.skipBytes(n); + } +}// end class LEDataInputStream diff --git a/marytts-common/src/main/java/marytts/util/io/LEDataOutputStream.java b/marytts-common/src/main/java/marytts/util/io/LEDataOutputStream.java index 81d17a86..11b93701 100644 --- a/marytts-common/src/main/java/marytts/util/io/LEDataOutputStream.java +++ b/marytts-common/src/main/java/marytts/util/io/LEDataOutputStream.java @@ -17,422 +17,412 @@ * along with this program. If not, see . * */ -package marytts.util.io; - +package marytts.util.io; + import java.io.DataOutput; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; - -/** - *

- *       LEDataOutputStream.java
- * 

- * copyright (c) 1998-2007 Roedy Green, - * Canadian Mind Products - * #101 - 2536 Wark Street - * Victoria, BC Canada V8T 4G8 - * hel: (250) 361-9093 - * mailto:roedyg@mindprod.com - * http://mindprod.com - *

- * Version 1.0 1998 January 6 - *

- * 1.1 1998 January 7 -officially implements DataInput - *

- * 1.2 1998 January 9 - add LERandomAccessFile - *

- * 1.3 1998 August 28 1.4 1998 November 10 - add new address and phone. - *

- * 1.5 1999 October 8 - use com.mindprod.ledatastream - * package name. Very similar to DataOutputStream except it writes - * little-endian - * instead of big-endian binary data. We can't extend DataOutputStream - * directly - * since it has only final methods. This forces us implement - * LEDataOutputStream - * with a DataOutputStream object, and use wrapper methods. - *

- * - * @noinspection WeakerAccess - */ -public class LEDataOutputStream implements DataOutput { - - // ------------------------------ FIELDS ------------------------------ - - /** - * undisplayed copyright notice. - * - * @noinspection UnusedDeclaration - */ - private static final String EMBEDDEDCOPYRIGHT = - "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com"; - - /** - * to get at big-Endian write methods of DataOutPutStream. - * - * @noinspection WeakerAccess - */ - protected final DataOutputStream dis; - - /** - * work array for composing output. - * - * @noinspection WeakerAccess - */ - protected final byte[] work; - - // -------------------------- PUBLIC INSTANCE METHODS -------------------------- - /** - * constructor. - * - * @param out the outputstream we write little endian binary data onto. - */ - public LEDataOutputStream( OutputStream out ) - { - this.dis = new DataOutputStream( out ); - work = new byte[8];// work array for composing output - } - - public LEDataOutputStream( String filename) throws FileNotFoundException - { - this(new FileOutputStream(filename)); - } - - /** - * Close stream. - * - * @throws IOException if close fails. - */ - public final void close() throws IOException - { - dis.close(); - } - - /** - * Flush stream without closing. - * - * @throws IOException if flush fails. - */ - public void flush() throws IOException - { - dis.flush(); - } - - /** - * Get size of stream. - * - * @return bytes written so far in the stream. Note this is a int, not a - * long as you would exect. This because the underlying - * DataInputStream has a design flaw. - */ - public final int size() - { - return dis.size(); - } - - /** - * This method writes only one byte, even though it says int (non-Javadoc) - * - * @param ib the byte to write. - * - * @throws IOException if write fails. - * @see java.io.DataOutput#write(int) - */ - public final synchronized void write( int ib ) throws IOException - { - dis.write( ib ); - } - - /** - * Write out an array of bytes. - * - * @throws IOException if write fails. - * @see java.io.DataOutput#write(byte[]) - */ - public final void write( byte ba[] ) throws IOException - { - dis.write( ba, 0, ba.length ); - } - - /** - * Writes out part of an array of bytes. - * - * @throws IOException if write fails. - * @see java.io.DataOutput#write(byte[],int,int) - */ - public final synchronized void write( byte ba[], - int off, - int len ) throws IOException - { - dis.write( ba, off, len ); - } - - /** - * Write a booleans as one byte. - * - * @param v boolean to write. - * - * @throws IOException if write fails. - * @see java.io.DataOutput#writeBoolean(boolean) - */ - /* Only writes one byte */ - public final void writeBoolean( boolean v ) throws IOException - { - dis.writeBoolean( v ); - } - - public final void writeBoolean( boolean [] v, int startPos, int len) throws IOException - { - assert v.length> 8 ); - dis.write( work, 0, 2 ); - } - - public final void writeChar( char [] v, int startPos, int len) throws IOException - { - assert v.length v.length! - // TODO remove this assert: - // assert v.length> 8 ); - work[ 2 ] = (byte) ( v >> 16 ); - work[ 3 ] = (byte) ( v >> 24 ); - dis.write( work, 0, 4 ); - } - - public final void writeInt( int [] v, int startPos, int len) throws IOException - { - assert v.length> 8 ); - work[ 2 ] = (byte) ( v >> 16 ); - work[ 3 ] = (byte) ( v >> 24 ); - work[ 4 ] = (byte) ( v >> 32 ); - work[ 5 ] = (byte) ( v >> 40 ); - work[ 6 ] = (byte) ( v >> 48 ); - work[ 7 ] = (byte) ( v >> 56 ); - dis.write( work, 0, 8 ); - } - - public final void writeLong (long [] v, int startPos, int len) throws IOException - { - assert v.length> 8 ); - dis.write( work, 0, 2 ); - } - - public final void writeShort( short [] v, int startPos, int len) throws IOException - { - assert v.length + * LEDataOutputStream.java + *

+ * copyright (c) 1998-2007 Roedy Green, + * Canadian Mind Products + * #101 - 2536 Wark Street + * Victoria, BC Canada V8T 4G8 + * hel: (250) 361-9093 + * mailto:roedyg@mindprod.com + * http://mindprod.com + *

+ * Version 1.0 1998 January 6 + *

+ * 1.1 1998 January 7 -officially implements DataInput + *

+ * 1.2 1998 January 9 - add LERandomAccessFile + *

+ * 1.3 1998 August 28 1.4 1998 November 10 - add new address and phone. + *

+ * 1.5 1999 October 8 - use com.mindprod.ledatastream + * package name. Very similar to DataOutputStream except it writes + * little-endian + * instead of big-endian binary data. We can't extend DataOutputStream + * directly + * since it has only final methods. This forces us implement + * LEDataOutputStream + * with a DataOutputStream object, and use wrapper methods. + * + * + * @noinspection WeakerAccess + */ +public class LEDataOutputStream implements DataOutput { + + // ------------------------------ FIELDS ------------------------------ + + /** + * undisplayed copyright notice. + * + * @noinspection UnusedDeclaration + */ + private static final String EMBEDDEDCOPYRIGHT = "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com"; + + /** + * to get at big-Endian write methods of DataOutPutStream. + * + * @noinspection WeakerAccess + */ + protected final DataOutputStream dis; + + /** + * work array for composing output. + * + * @noinspection WeakerAccess + */ + protected final byte[] work; + + // -------------------------- PUBLIC INSTANCE METHODS -------------------------- + /** + * constructor. + * + * @param out + * the outputstream we write little endian binary data onto. + */ + public LEDataOutputStream(OutputStream out) { + this.dis = new DataOutputStream(out); + work = new byte[8];// work array for composing output + } + + public LEDataOutputStream(String filename) throws FileNotFoundException { + this(new FileOutputStream(filename)); + } + + /** + * Close stream. + * + * @throws IOException + * if close fails. + */ + public final void close() throws IOException { + dis.close(); + } + + /** + * Flush stream without closing. + * + * @throws IOException + * if flush fails. + */ + public void flush() throws IOException { + dis.flush(); + } + + /** + * Get size of stream. + * + * @return bytes written so far in the stream. Note this is a int, not a long as you would exect. This because the underlying + * DataInputStream has a design flaw. + */ + public final int size() { + return dis.size(); + } + + /** + * This method writes only one byte, even though it says int (non-Javadoc) + * + * @param ib + * the byte to write. + * + * @throws IOException + * if write fails. + * @see java.io.DataOutput#write(int) + */ + public final synchronized void write(int ib) throws IOException { + dis.write(ib); + } + + /** + * Write out an array of bytes. + * + * @throws IOException + * if write fails. + * @see java.io.DataOutput#write(byte[]) + */ + public final void write(byte ba[]) throws IOException { + dis.write(ba, 0, ba.length); + } + + /** + * Writes out part of an array of bytes. + * + * @throws IOException + * if write fails. + * @see java.io.DataOutput#write(byte[],int,int) + */ + public final synchronized void write(byte ba[], int off, int len) throws IOException { + dis.write(ba, off, len); + } + + /** + * Write a booleans as one byte. + * + * @param v + * boolean to write. + * + * @throws IOException + * if write fails. + * @see java.io.DataOutput#writeBoolean(boolean) + */ + /* Only writes one byte */ + public final void writeBoolean(boolean v) throws IOException { + dis.writeBoolean(v); + } + + public final void writeBoolean( boolean [] v, int startPos, int len) throws IOException + { + assert v.length> 8); + dis.write(work, 0, 2); + } + + public final void writeChar( char [] v, int startPos, int len) throws IOException + { + assert v.length v.length! + // TODO remove this assert: + // assert v.length> 8); + work[2] = (byte) (v >> 16); + work[3] = (byte) (v >> 24); + dis.write(work, 0, 4); + } + + public final void writeInt( int [] v, int startPos, int len) throws IOException + { + assert v.length> 8); + work[2] = (byte) (v >> 16); + work[3] = (byte) (v >> 24); + work[4] = (byte) (v >> 32); + work[5] = (byte) (v >> 40); + work[6] = (byte) (v >> 48); + work[7] = (byte) (v >> 56); + dis.write(work, 0, 8); + } + + public final void writeLong (long [] v, int startPos, int len) throws IOException + { + assert v.length> 8); + dis.write(work, 0, 2); + } + + public final void writeShort( short [] v, int startPos, int len) throws IOException + { + assert v.length. * */ -package marytts.util.io; - +package marytts.util.io; + import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; - -public class LittleEndianBinaryReader { - private DataInputStream inputStream; - private long accumLong; - private int accumInt; - private int shiftBy; - private int low; - private int high; - - public LittleEndianBinaryReader(DataInputStream d) - { - this.inputStream = d; - } - - public LittleEndianBinaryReader(FileInputStream f) - { - this(new DataInputStream(f)); - } - - public LittleEndianBinaryReader(String filename) throws IOException - { - this(new DataInputStream(new FileInputStream(filename))); - } - - /* - public float readFloat() throws IOException { - for (int i=0; i<4; i++) - bytes[i] = inputStream.readByte(); - - return Float.intBitsToFloat(((0x0ff & bytes[0])<<0) | ((0x0ff & bytes[1])<<8) | ((0x0ff & bytes[2])<<16) | ((0x0ff & bytes[3])<<24)); - } - */ - - public short readShort() throws IOException - { - low = inputStream.readByte() & 0xff; - high = inputStream.readByte() & 0xff; - return(short)( high << 8 | low ); - } - - - public long readLong() throws IOException - { - accumLong = 0; - for (shiftBy=0; shiftBy<64; shiftBy+=8 ) - accumLong |= (long)(inputStream.readByte() & 0xff ) << shiftBy; - - return accumLong; - } - - public char readChar( ) throws IOException - { - low = inputStream.readByte() & 0xff; - high = inputStream.readByte(); - return(char)( high << 8 | low ); - } - - public int readInt( ) throws IOException - { - accumInt = 0; - for (shiftBy=0; shiftBy<32; shiftBy+=8 ) - { - accumInt |= (inputStream.readByte() & 0xff ) << shiftBy; - } - return accumInt; - } - - public double readDouble() throws IOException - { - accumLong = 0; - for (shiftBy=0; shiftBy<64; shiftBy+=8 ) - accumLong |= ( (long)( inputStream.readByte() & 0xff ) ) << shiftBy; - - return Double.longBitsToDouble( accumLong ); - } - - public float readFloat() throws IOException - { - accumInt = 0; - for (shiftBy=0; shiftBy<32; shiftBy+=8 ) - accumInt |= (inputStream.readByte () & 0xff ) << shiftBy; - - return Float.intBitsToFloat( accumInt ); - } - - public byte readByte( ) throws IOException - { - return inputStream.readByte(); - } - - public void close() throws IOException - { - if (inputStream!=null) - inputStream.close(); - } -} +public class LittleEndianBinaryReader { + private DataInputStream inputStream; + private long accumLong; + private int accumInt; + private int shiftBy; + private int low; + private int high; + + public LittleEndianBinaryReader(DataInputStream d) { + this.inputStream = d; + } + + public LittleEndianBinaryReader(FileInputStream f) { + this(new DataInputStream(f)); + } + + public LittleEndianBinaryReader(String filename) throws IOException { + this(new DataInputStream(new FileInputStream(filename))); + } + + /* + * public float readFloat() throws IOException { for (int i=0; i<4; i++) bytes[i] = inputStream.readByte(); + * + * return Float.intBitsToFloat(((0x0ff & bytes[0])<<0) | ((0x0ff & bytes[1])<<8) | ((0x0ff & bytes[2])<<16) | ((0x0ff & + * bytes[3])<<24)); } + */ + + public short readShort() throws IOException { + low = inputStream.readByte() & 0xff; + high = inputStream.readByte() & 0xff; + return (short) (high << 8 | low); + } + + public long readLong() throws IOException { + accumLong = 0; + for (shiftBy = 0; shiftBy < 64; shiftBy += 8) + accumLong |= (long) (inputStream.readByte() & 0xff) << shiftBy; + + return accumLong; + } + + public char readChar() throws IOException { + low = inputStream.readByte() & 0xff; + high = inputStream.readByte(); + return (char) (high << 8 | low); + } + + public int readInt() throws IOException { + accumInt = 0; + for (shiftBy = 0; shiftBy < 32; shiftBy += 8) { + accumInt |= (inputStream.readByte() & 0xff) << shiftBy; + } + return accumInt; + } + + public double readDouble() throws IOException { + accumLong = 0; + for (shiftBy = 0; shiftBy < 64; shiftBy += 8) + accumLong |= ((long) (inputStream.readByte() & 0xff)) << shiftBy; + + return Double.longBitsToDouble(accumLong); + } + + public float readFloat() throws IOException { + accumInt = 0; + for (shiftBy = 0; shiftBy < 32; shiftBy += 8) + accumInt |= (inputStream.readByte() & 0xff) << shiftBy; + + return Float.intBitsToFloat(accumInt); + } + + public byte readByte() throws IOException { + return inputStream.readByte(); + } + + public void close() throws IOException { + if (inputStream != null) + inputStream.close(); + } +} diff --git a/marytts-common/src/main/java/marytts/util/io/LoggingReader.java b/marytts-common/src/main/java/marytts/util/io/LoggingReader.java index 9743ccef..e3505540 100644 --- a/marytts-common/src/main/java/marytts/util/io/LoggingReader.java +++ b/marytts-common/src/main/java/marytts/util/io/LoggingReader.java @@ -25,52 +25,45 @@ import java.io.Reader; import org.apache.log4j.Logger; -public class LoggingReader extends FilterReader -{ - protected Logger logger; - protected StringBuffer logText; +public class LoggingReader extends FilterReader { + protected Logger logger; + protected StringBuffer logText; - public LoggingReader(Reader in, Logger logger) - { - super(in); - this.logger = logger; - logText = new StringBuffer(); - } + public LoggingReader(Reader in, Logger logger) { + super(in); + this.logger = logger; + logText = new StringBuffer(); + } - public int read() throws IOException - { - int c = super.read(); - if (c == -1) { - logRead(); - } else { - logText.append((char)c); - } - return c; - } + public int read() throws IOException { + int c = super.read(); + if (c == -1) { + logRead(); + } else { + logText.append((char) c); + } + return c; + } - public int read(char[] cbuf, int off, int len) throws IOException - { - int nr = super.read(cbuf, off, len); - if (nr == -1) { - logRead(); - } else { - logText.append(new String(cbuf, off, nr)); - } - return nr; - } + public int read(char[] cbuf, int off, int len) throws IOException { + int nr = super.read(cbuf, off, len); + if (nr == -1) { + logRead(); + } else { + logText.append(new String(cbuf, off, nr)); + } + return nr; + } - public void close() throws IOException - { - super.close(); - logRead(); - } + public void close() throws IOException { + super.close(); + logRead(); + } - public void logRead() - { - if (logText.length() > 0) { - logger.info("Read:\n" + logText.toString()); - logText.setLength(0); - } - } + public void logRead() { + if (logText.length() > 0) { + logger.info("Read:\n" + logText.toString()); + logText.setLength(0); + } + } } - diff --git a/marytts-common/src/main/java/marytts/util/io/MaryRandomAccessFile.java b/marytts-common/src/main/java/marytts/util/io/MaryRandomAccessFile.java index 0769430d..266ffc26 100644 --- a/marytts-common/src/main/java/marytts/util/io/MaryRandomAccessFile.java +++ b/marytts-common/src/main/java/marytts/util/io/MaryRandomAccessFile.java @@ -17,570 +17,509 @@ * along with this program. If not, see . * */ -package marytts.util.io; - +package marytts.util.io; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; - -/** - * A class that extends RandomAccessFile to read/write arrays of different types while allowing random - * access to a binary file (i.e. the file can be opened in both read/write mode and there is support for - * moving the file pointer to any location as required - * - * @author Oytun Türk - */ - -public final class MaryRandomAccessFile extends RandomAccessFile -{ - public MaryRandomAccessFile(File arg0, String arg1) throws FileNotFoundException { - super(arg0, arg1); - } - - public MaryRandomAccessFile(String arg0, String arg1) throws FileNotFoundException { - super(arg0, arg1); - } - - public final boolean readBooleanEndian() throws IOException - { - boolean ret = readBoolean(); - - return ret; - } - - public final boolean [] readBoolean(int len) throws IOException - { - boolean [] ret = new boolean[len]; - - for (int i=0; i" + line); - } catch (IOException ioe) - { - ioe.printStackTrace(); - } - } +public class StreamGobbler extends Thread { + InputStream is; + String type; + + public StreamGobbler(InputStream is, String type) { + this.is = is; + this.type = type; + } + + public void run() { + try { + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String line = null; + while ((line = br.readLine()) != null) + System.out.println(type + ">" + line); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } } \ No newline at end of file diff --git a/marytts-common/src/main/java/marytts/util/io/StreamLogger.java b/marytts-common/src/main/java/marytts/util/io/StreamLogger.java index 33e9849e..ff7db065 100644 --- a/marytts-common/src/main/java/marytts/util/io/StreamLogger.java +++ b/marytts-common/src/main/java/marytts/util/io/StreamLogger.java @@ -31,73 +31,64 @@ import marytts.util.MaryUtils; import org.apache.log4j.Logger; - /** * Read from a stream and log. + * * @author Marc Schröder */ -public class StreamLogger extends Thread -{ - private InputStream is; - private PrintStream ps; - private Logger logger; - private Pattern ignorePattern = null; +public class StreamLogger extends Thread { + private InputStream is; + private PrintStream ps; + private Logger logger; + private Pattern ignorePattern = null; - /** - * Read from an input stream, logging to category logCategory, - * ignoring lines matching - * the regular expression specified in ignorePattern. - * If logCategory is null, "unnamed" will be used. - * If ignorePattern is null, no filtering will be - * performed. - * The thread will silently die when it reaches end-of-file from the input - * stream. - */ - public StreamLogger(InputStream is, String logCategory, String ignorePattern) - { - this.is = is; - if (logCategory == null) - logger = MaryUtils.getLogger("unnamed"); - else - logger = MaryUtils.getLogger(logCategory); - if (ignorePattern != null) { - try { - this.ignorePattern = Pattern.compile(ignorePattern); - } catch (PatternSyntaxException e) { - logger.warn("Problem with regular expression pattern", e); - this.ignorePattern = null; - } - } - } + /** + * Read from an input stream, logging to category logCategory, ignoring lines matching the regular expression + * specified in ignorePattern. If logCategory is null, "unnamed" will be used. If + * ignorePattern is null, no filtering will be performed. The thread will silently die when it + * reaches end-of-file from the input stream. + */ + public StreamLogger(InputStream is, String logCategory, String ignorePattern) { + this.is = is; + if (logCategory == null) + logger = MaryUtils.getLogger("unnamed"); + else + logger = MaryUtils.getLogger(logCategory); + if (ignorePattern != null) { + try { + this.ignorePattern = Pattern.compile(ignorePattern); + } catch (PatternSyntaxException e) { + logger.warn("Problem with regular expression pattern", e); + this.ignorePattern = null; + } + } + } - public StreamLogger(InputStream is, PrintStream ps) { - this.is = is; - this.ps = ps; - } + public StreamLogger(InputStream is, PrintStream ps) { + this.is = is; + this.ps = ps; + } - public void run() - { - String line = null; - try { - BufferedReader b = new BufferedReader(new InputStreamReader(is)); - while ((line = b.readLine()) != null) { - if (ignorePattern != null && ignorePattern.matcher(line).matches()) - continue; // do not log - if (ps != null) { - ps.println(line); - } else { - logger.info(line); - } - } - } catch (IOException e) { - try { - logger.warn("Cannot read from stream", e); - } catch (NullPointerException npe) { - e.printStackTrace(); - } - } - } + public void run() { + String line = null; + try { + BufferedReader b = new BufferedReader(new InputStreamReader(is)); + while ((line = b.readLine()) != null) { + if (ignorePattern != null && ignorePattern.matcher(line).matches()) + continue; // do not log + if (ps != null) { + ps.println(line); + } else { + logger.info(line); + } + } + } catch (IOException e) { + try { + logger.warn("Cannot read from stream", e); + } catch (NullPointerException npe) { + e.printStackTrace(); + } + } + } } - - diff --git a/marytts-common/src/main/java/marytts/util/io/StreamUtils.java b/marytts-common/src/main/java/marytts/util/io/StreamUtils.java index 8fb71412..8850ffe8 100644 --- a/marytts-common/src/main/java/marytts/util/io/StreamUtils.java +++ b/marytts-common/src/main/java/marytts/util/io/StreamUtils.java @@ -34,24 +34,22 @@ import java.nio.ByteBuffer; /** * @author marc - * + * */ public class StreamUtils { - public static double[] readDoubleArray(DataInput stream, int len) - throws IOException { - byte[] raw = new byte[len*Double.SIZE/8]; - stream.readFully(raw); - DataInputStream in = new DataInputStream(new ByteArrayInputStream(raw)); - double[] data = new double[len]; - for (int i=0; ibb a representation - * of a Unicode character string encoded in - * modified UTF-8 format; - * this string of characters is then returned as a String. - * The details of the modified UTF-8 representation - * are exactly the same as for the readUTF - * method of DataInput. - * - * @param in a byte buffer. - * @return a Unicode string. - * @exception BufferUnderflowException if the input stream reaches the end - * before all the bytes. - * @exception UTFDataFormatException if the bytes do not represent a - * valid modified UTF-8 encoding of a Unicode string. - * @see java.io.DataInputStream#readUnsignedShort() - */ - public static String readUTF(ByteBuffer bb) throws BufferUnderflowException, UTFDataFormatException { - int utflen = readUnsignedShort(bb); - byte[] bytearr = new byte[utflen]; - char[] chararr = new char[utflen]; - int c, char2, char3; - int count = 0; - int chararr_count=0; + /** + * Reads from the bytebuffer bb a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a + * String. The details of the modified UTF-8 representation are exactly the same as for the readUTF + * method of DataInput. + * + * @param in + * a byte buffer. + * @return a Unicode string. + * @exception BufferUnderflowException + * if the input stream reaches the end before all the bytes. + * @exception UTFDataFormatException + * if the bytes do not represent a valid modified UTF-8 encoding of a Unicode string. + * @see java.io.DataInputStream#readUnsignedShort() + */ + public static String readUTF(ByteBuffer bb) throws BufferUnderflowException, UTFDataFormatException { + int utflen = readUnsignedShort(bb); + byte[] bytearr = new byte[utflen]; + char[] chararr = new char[utflen]; - bb.get(bytearr); + int c, char2, char3; + int count = 0; + int chararr_count = 0; - while (count < utflen) { - c = (int) bytearr[count] & 0xff; - if (c > 127) break; - count++; - chararr[chararr_count++]=(char)c; - } + bb.get(bytearr); - while (count < utflen) { - c = (int) bytearr[count] & 0xff; - switch (c >> 4) { - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: - /* 0xxxxxxx*/ - count++; - chararr[chararr_count++]=(char)c; - break; - case 12: case 13: - /* 110x xxxx 10xx xxxx*/ - count += 2; - if (count > utflen) - throw new UTFDataFormatException( - "malformed input: partial character at end"); - char2 = (int) bytearr[count-1]; - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatException( - "malformed input around byte " + count); - chararr[chararr_count++]=(char)(((c & 0x1F) << 6) | - (char2 & 0x3F)); - break; - case 14: - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - count += 3; - if (count > utflen) - throw new UTFDataFormatException( - "malformed input: partial character at end"); - char2 = (int) bytearr[count-2]; - char3 = (int) bytearr[count-1]; - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatException( - "malformed input around byte " + (count-1)); - chararr[chararr_count++]=(char)(((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - ((char3 & 0x3F) << 0)); - break; - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatException( - "malformed input around byte " + count); - } - } - // The number of chars produced may be less than utflen - return new String(chararr, 0, chararr_count); - } - - /** - * See the general contract of the readUnsignedShort - * method of DataInput. - *

- * Bytes - * for this operation are read from the given byte buffer - * - * @return the next two bytes of this input stream, interpreted as an - * unsigned 16-bit integer. - * @exception EOFException if this input stream reaches the end before - * reading two bytes. - * @exception IOException the stream has been closed and the contained - * input stream does not support reading after close, or - * another I/O error occurs. - * @see java.io.FilterInputStream#in - */ - public static int readUnsignedShort(ByteBuffer bb) throws BufferUnderflowException { - int ch1 = bb.get() & 0xFF; // convert byte to unsigned byte - int ch2 = bb.get() & 0xFF; // convert byte to unsigned byte - return (ch1 << 8) + (ch2 << 0); - } + while (count < utflen) { + c = (int) bytearr[count] & 0xff; + if (c > 127) + break; + count++; + chararr[chararr_count++] = (char) c; + } + + while (count < utflen) { + c = (int) bytearr[count] & 0xff; + switch (c >> 4) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + /* 0xxxxxxx */ + count++; + chararr[chararr_count++] = (char) c; + break; + case 12: + case 13: + /* 110x xxxx 10xx xxxx */ + count += 2; + if (count > utflen) + throw new UTFDataFormatException("malformed input: partial character at end"); + char2 = (int) bytearr[count - 1]; + if ((char2 & 0xC0) != 0x80) + throw new UTFDataFormatException("malformed input around byte " + count); + chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F)); + break; + case 14: + /* 1110 xxxx 10xx xxxx 10xx xxxx */ + count += 3; + if (count > utflen) + throw new UTFDataFormatException("malformed input: partial character at end"); + char2 = (int) bytearr[count - 2]; + char3 = (int) bytearr[count - 1]; + if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) + throw new UTFDataFormatException("malformed input around byte " + (count - 1)); + chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); + break; + default: + /* 10xx xxxx, 1111 xxxx */ + throw new UTFDataFormatException("malformed input around byte " + count); + } + } + // The number of chars produced may be less than utflen + return new String(chararr, 0, chararr_count); + } + + /** + * See the general contract of the readUnsignedShort method of DataInput. + *

+ * Bytes for this operation are read from the given byte buffer + * + * @return the next two bytes of this input stream, interpreted as an unsigned 16-bit integer. + * @exception EOFException + * if this input stream reaches the end before reading two bytes. + * @exception IOException + * the stream has been closed and the contained input stream does not support reading after close, or another + * I/O error occurs. + * @see java.io.FilterInputStream#in + */ + public static int readUnsignedShort(ByteBuffer bb) throws BufferUnderflowException { + int ch1 = bb.get() & 0xFF; // convert byte to unsigned byte + int ch2 = bb.get() & 0xFF; // convert byte to unsigned byte + return (ch1 << 8) + (ch2 << 0); + } } diff --git a/marytts-common/src/main/java/marytts/util/string/IntStringTranslator.java b/marytts-common/src/main/java/marytts/util/string/IntStringTranslator.java index 004931f7..561ac3f1 100644 --- a/marytts-common/src/main/java/marytts/util/string/IntStringTranslator.java +++ b/marytts-common/src/main/java/marytts/util/string/IntStringTranslator.java @@ -17,95 +17,95 @@ * along with this program. If not, see . * */ -package marytts.util.string; - +package marytts.util.string; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; - -/** - * A helper class converting between a given set of integers and strings. - * @author schroed - * - */ -public class IntStringTranslator -{ - ArrayList list; - Map map; - - /** - * Initialize empty int-string two-way translator. - * - */ - public IntStringTranslator() - { - list = new ArrayList(); - map = new HashMap(); - } - - public IntStringTranslator(int initialRange) - { - list = new ArrayList(initialRange); - map = new HashMap(); - } - - /** - * Initialize a int-string two-way translator, - * setting int values according to the position of strings - * in the array. - * @param strings - */ - public IntStringTranslator(String[] strings) - { - list = new ArrayList(Arrays.asList(strings)); - map = new HashMap(); - for (int i=0; i= list.size()) return false; - return true; - } - - public int get(String s) - { - Integer index = map.get(s); - if (index == null) - throw new IllegalArgumentException("No int value known for string ["+s+"]"); - return index.intValue(); - } - - public String get(int i) - { - if (i < 0 || i >= list.size()) - throw new IndexOutOfBoundsException("Int value out of range: "+i); - return list.get(i); - } - - public String[] getStringValues() - { - return list.toArray(new String[0]); - } - - public int getHighestValue() - { - return list.size(); - } -} + +/** + * A helper class converting between a given set of integers and strings. + * @author schroed + * + */ +public class IntStringTranslator +{ + ArrayList list; + Map map; + + /** + * Initialize empty int-string two-way translator. + * + */ + public IntStringTranslator() + { + list = new ArrayList(); + map = new HashMap(); + } + + public IntStringTranslator(int initialRange) + { + list = new ArrayList(initialRange); + map = new HashMap(); + } + + /** + * Initialize a int-string two-way translator, + * setting int values according to the position of strings + * in the array. + * @param strings + */ + public IntStringTranslator(String[] strings) + { + list = new ArrayList(Arrays.asList(strings)); + map = new HashMap(); + for (int i=0; i= list.size()) return false; + return true; + } + + public int get(String s) + { + Integer index = map.get(s); + if (index == null) + throw new IllegalArgumentException("No int value known for string ["+s+"]"); + return index.intValue(); + } + + public String get(int i) + { + if (i < 0 || i >= list.size()) + throw new IndexOutOfBoundsException("Int value out of range: "+i); + return list.get(i); + } + + public String[] getStringValues() + { + return list.toArray(new String[0]); + } + + public int getHighestValue() + { + return list.size(); + } +} diff --git a/marytts-common/src/main/java/marytts/util/string/ShortStringTranslator.java b/marytts-common/src/main/java/marytts/util/string/ShortStringTranslator.java index 86bbc287..5e7906e6 100644 --- a/marytts-common/src/main/java/marytts/util/string/ShortStringTranslator.java +++ b/marytts-common/src/main/java/marytts/util/string/ShortStringTranslator.java @@ -17,99 +17,99 @@ * along with this program. If not, see . * */ -package marytts.util.string; - +package marytts.util.string; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; - -/** - * A helper class converting between a given set of shorts and strings. - * @author schroed - * - */ -public class ShortStringTranslator -{ - ArrayList list; - Map map; - - - /** - * Initialize empty short-string two-way translator. - * - */ - public ShortStringTranslator() - { - list = new ArrayList(); - map = new HashMap(); - } - - public ShortStringTranslator(short initialRange) - { - list = new ArrayList(initialRange); - map = new HashMap(); - } - - /** - * Initialize a short-string two-way translator, - * setting short values according to the position of strings - * in the array. - * @param strings - */ - public ShortStringTranslator(String[] strings) - { - if (strings.length > Short.MAX_VALUE) throw new IllegalArgumentException("Too many strings for a short-string translator"); - list = new ArrayList(Arrays.asList(strings)); - map = new HashMap(); - for (int i=0; i= list.size()) return false; - return true; - } - - public short get(String s) - { - Short index = map.get(s); - if (index == null) - throw new IllegalArgumentException("No short value known for string ["+s+"]"); - return index.shortValue(); - } - - public String get(short b) - { - int index = (int) b; - if (index < 0 || index >= list.size()) - throw new IndexOutOfBoundsException("Short value out of range: "+index); - return list.get(index); - } - - public String[] getStringValues() - { - return list.toArray(new String[0]); - } - - public short getNumberOfValues() - { - return (short) list.size(); - } - -} + +/** + * A helper class converting between a given set of shorts and strings. + * @author schroed + * + */ +public class ShortStringTranslator +{ + ArrayList list; + Map map; + + + /** + * Initialize empty short-string two-way translator. + * + */ + public ShortStringTranslator() + { + list = new ArrayList(); + map = new HashMap(); + } + + public ShortStringTranslator(short initialRange) + { + list = new ArrayList(initialRange); + map = new HashMap(); + } + + /** + * Initialize a short-string two-way translator, + * setting short values according to the position of strings + * in the array. + * @param strings + */ + public ShortStringTranslator(String[] strings) + { + if (strings.length > Short.MAX_VALUE) throw new IllegalArgumentException("Too many strings for a short-string translator"); + list = new ArrayList(Arrays.asList(strings)); + map = new HashMap(); + for (int i=0; i= list.size()) return false; + return true; + } + + public short get(String s) + { + Short index = map.get(s); + if (index == null) + throw new IllegalArgumentException("No short value known for string ["+s+"]"); + return index.shortValue(); + } + + public String get(short b) + { + int index = (int) b; + if (index < 0 || index >= list.size()) + throw new IndexOutOfBoundsException("Short value out of range: "+index); + return list.get(index); + } + + public String[] getStringValues() + { + return list.toArray(new String[0]); + } + + public short getNumberOfValues() + { + return (short) list.size(); + } + +}