зеркало из https://github.com/mozilla/marytts.git
format java code in marytts-common
incantation: $ mvn com.googlecode.maven-java-formatter-plugin:maven-java-formatter-plugin:format -pl marytts-common
This commit is contained in:
Родитель
f1ff6ad856
Коммит
1d71468ca8
|
@ -4,56 +4,56 @@ import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright (c) 2001, 2002 by Pensamos Digital, Inc., All Rights Reserved.<p>
|
* 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.
|
|
||||||
* <p>
|
* <p>
|
||||||
* This library is distributed in the hope that it will be useful,
|
*
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||||
* Library General Public License for more details.
|
|
||||||
* <p>
|
* <p>
|
||||||
* You should have received a copy of the GNU Library General Public
|
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* License along with this library; if not, write to the Free
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
|
||||||
|
* <p>
|
||||||
|
* 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.
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
* <p>
|
* <p>
|
||||||
* This OutputStream discards all data written to it.
|
* This OutputStream discards all data written to it.
|
||||||
*
|
*
|
||||||
* @author Tim Macinta (twm@alum.mit.edu)
|
* @author Tim Macinta (twm@alum.mit.edu)
|
||||||
**/
|
**/
|
||||||
|
|
||||||
public class NullOutputStream extends OutputStream {
|
public class NullOutputStream extends OutputStream {
|
||||||
|
|
||||||
private boolean closed = false;
|
private boolean closed = false;
|
||||||
|
|
||||||
public NullOutputStream() {
|
public NullOutputStream() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
this.closed = true;
|
this.closed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
if (this.closed) _throwClosed();
|
if (this.closed)
|
||||||
}
|
_throwClosed();
|
||||||
|
}
|
||||||
|
|
||||||
private void _throwClosed() throws IOException {
|
private void _throwClosed() throws IOException {
|
||||||
throw new IOException("This OutputStream has been closed");
|
throw new IOException("This OutputStream has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(byte[] b) throws IOException {
|
public void write(byte[] b) throws IOException {
|
||||||
if (this.closed) _throwClosed();
|
if (this.closed)
|
||||||
}
|
_throwClosed();
|
||||||
|
}
|
||||||
|
|
||||||
public void write(byte[] b, int offset, int len) throws IOException {
|
public void write(byte[] b, int offset, int len) throws IOException {
|
||||||
if (this.closed) _throwClosed();
|
if (this.closed)
|
||||||
}
|
_throwClosed();
|
||||||
|
}
|
||||||
|
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
if (this.closed) _throwClosed();
|
if (this.closed)
|
||||||
}
|
_throwClosed();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -6,168 +6,160 @@ import java.io.FilterInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MD5InputStream, a subclass of FilterInputStream implementing MD5
|
* MD5InputStream, a subclass of FilterInputStream implementing MD5 functionality on a stream.
|
||||||
* functionality on a stream.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
|
* Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
|
||||||
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
||||||
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
||||||
* <p>
|
* <p>
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
|
||||||
* 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.
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
|
||||||
* <p>
|
* <p>
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* 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.
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Library General Public License for more details.
|
|
||||||
* <p>
|
* <p>
|
||||||
* You should have received a copy of the GNU Library General Public
|
* You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free
|
||||||
* License along with this library; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
* <p>
|
* <p>
|
||||||
* See http://www.twmacinta.com/myjava/fast_md5.php for more information
|
* See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file.
|
||||||
* on this file.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Please note: I (Timothy Macinta) have put this code in the
|
* Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I
|
||||||
* 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.
|
||||||
* was not the the original author of the code, although I did
|
*
|
||||||
* optimize it (substantially) and fix some bugs.
|
* @author Santeri Paavolainen <santtu@cs.hut.fi>
|
||||||
*
|
* @author Timothy W Macinta (twm@alum.mit.edu) (added main() method)
|
||||||
* @author Santeri Paavolainen <santtu@cs.hut.fi>
|
|
||||||
* @author Timothy W Macinta (twm@alum.mit.edu) (added main() method)
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
|
||||||
public class MD5InputStream extends FilterInputStream {
|
public class MD5InputStream extends FilterInputStream {
|
||||||
/**
|
/**
|
||||||
* MD5 context
|
* MD5 context
|
||||||
*/
|
*/
|
||||||
private MD5 md5;
|
private MD5 md5;
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a MD5InputStream
|
|
||||||
* @param in The input stream
|
|
||||||
*/
|
|
||||||
public MD5InputStream (InputStream in) {
|
|
||||||
super(in);
|
|
||||||
|
|
||||||
md5 = new MD5();
|
/**
|
||||||
}
|
* Creates a MD5InputStream
|
||||||
|
*
|
||||||
|
* @param in
|
||||||
|
* The input stream
|
||||||
|
*/
|
||||||
|
public MD5InputStream(InputStream in) {
|
||||||
|
super(in);
|
||||||
|
|
||||||
/**
|
md5 = new MD5();
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)));
|
if (c == -1)
|
||||||
while ((num_read = in.read(buf)) != -1);
|
return -1;
|
||||||
System.out.println(MD5.asHex(in.hash())+" "+filename);
|
|
||||||
in.close();
|
if ((c & ~0xff) != 0) {
|
||||||
}
|
System.out.println("MD5InputStream.read() got character with (c & ~0xff) != 0)!");
|
||||||
} catch (Exception e) {
|
} else {
|
||||||
e.printStackTrace();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,116 +7,106 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MD5OutputStream is a subclass of FilterOutputStream adding MD5
|
* MD5OutputStream is a subclass of FilterOutputStream adding MD5 hashing of the output.
|
||||||
* hashing of the output.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
|
* Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
|
||||||
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
||||||
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
||||||
* <p>
|
* <p>
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
|
||||||
* 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.
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
|
||||||
* <p>
|
* <p>
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* 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.
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Library General Public License for more details.
|
|
||||||
* <p>
|
* <p>
|
||||||
* You should have received a copy of the GNU Library General Public
|
* You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free
|
||||||
* License along with this library; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
* <p>
|
* <p>
|
||||||
* See http://www.twmacinta.com/myjava/fast_md5.php for more information
|
* See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file.
|
||||||
* on this file.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Please note: I (Timothy Macinta) have put this code in the
|
* Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I
|
||||||
* 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.
|
||||||
* was not the the original author of the code, although I did
|
*
|
||||||
* optimize it (substantially) and fix some bugs.
|
* @author Santeri Paavolainen <santtu@cs.hut.fi>
|
||||||
*
|
* @author Timothy W Macinta (twm@alum.mit.edu) (added main() method)
|
||||||
* @author Santeri Paavolainen <santtu@cs.hut.fi>
|
|
||||||
* @author Timothy W Macinta (twm@alum.mit.edu) (added main() method)
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
public class MD5OutputStream extends FilterOutputStream {
|
public class MD5OutputStream extends FilterOutputStream {
|
||||||
/**
|
/**
|
||||||
* MD5 context
|
* MD5 context
|
||||||
*/
|
*/
|
||||||
private MD5 md5;
|
private MD5 md5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates MD5OutputStream
|
* Creates MD5OutputStream
|
||||||
* @param out The output stream
|
*
|
||||||
*/
|
* @param out
|
||||||
|
* The output stream
|
||||||
|
*/
|
||||||
|
|
||||||
public MD5OutputStream (OutputStream out) {
|
public MD5OutputStream(OutputStream out) {
|
||||||
super(out);
|
super(out);
|
||||||
|
|
||||||
md5 = new MD5();
|
md5 = new MD5();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a byte.
|
* Writes a byte.
|
||||||
*
|
*
|
||||||
* @see java.io.FilterOutputStream
|
* @see java.io.FilterOutputStream
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void write (int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
out.write(b);
|
out.write(b);
|
||||||
md5.Update((byte) b);
|
md5.Update((byte) b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a sub array of bytes.
|
* Writes a sub array of bytes.
|
||||||
*
|
*
|
||||||
* @see java.io.FilterOutputStream
|
* @see java.io.FilterOutputStream
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void write (byte b[], int off, int len) throws IOException {
|
public void write(byte b[], int off, int len) throws IOException {
|
||||||
out.write(b, off, len);
|
out.write(b, off, len);
|
||||||
md5.Update(b, off, len);
|
md5.Update(b, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns array of bytes representing hash of the stream as finalized
|
* Returns array of bytes representing hash of the stream as finalized for the current state.
|
||||||
* for the current state.
|
*
|
||||||
* @see MD5#Final
|
* @see MD5#Final
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public byte[] hash () {
|
public byte[] hash() {
|
||||||
return md5.Final();
|
return md5.Final();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MD5 getMD5() {
|
public MD5 getMD5() {
|
||||||
return md5;
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,74 +6,66 @@ package com.twmacinta.util;
|
||||||
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
||||||
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
||||||
* <p>
|
* <p>
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
|
||||||
* 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.
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
|
||||||
* <p>
|
* <p>
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* 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.
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Library General Public License for more details.
|
|
||||||
* <p>
|
* <p>
|
||||||
* You should have received a copy of the GNU Library General Public
|
* You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free
|
||||||
* License along with this library; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
* <p>
|
* <p>
|
||||||
* See http://www.twmacinta.com/myjava/fast_md5.php for more information
|
* See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file.
|
||||||
* on this file.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Contains internal state of the MD5 class
|
* Contains internal state of the MD5 class
|
||||||
* <p>
|
* <p>
|
||||||
* Please note: I (Timothy Macinta) have put this code in the
|
* Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I
|
||||||
* 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.
|
||||||
* was not the the original author of the code, although I did
|
*
|
||||||
* optimize it (substantially) and fix some bugs.
|
* @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
|
||||||
*
|
* @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes)
|
||||||
* @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
|
|
||||||
* @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes)
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
class MD5State {
|
class MD5State {
|
||||||
/**
|
/**
|
||||||
* 128-bit state
|
* 128-bit state
|
||||||
*/
|
*/
|
||||||
int state[];
|
int state[];
|
||||||
|
|
||||||
/**
|
|
||||||
* 64-bit character count
|
|
||||||
*/
|
|
||||||
long count;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 64-byte buffer (512 bits) for storing to-be-hashed characters
|
|
||||||
*/
|
|
||||||
byte buffer[];
|
|
||||||
|
|
||||||
public MD5State() {
|
/**
|
||||||
buffer = new byte[64];
|
* 64-bit character count
|
||||||
count = 0;
|
*/
|
||||||
state = new int[4];
|
long count;
|
||||||
|
|
||||||
state[0] = 0x67452301;
|
|
||||||
state[1] = 0xefcdab89;
|
|
||||||
state[2] = 0x98badcfe;
|
|
||||||
state[3] = 0x10325476;
|
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* 64-byte buffer (512 bits) for storing to-be-hashed characters
|
||||||
|
*/
|
||||||
|
byte buffer[];
|
||||||
|
|
||||||
/** Create this State as a copy of another state */
|
public MD5State() {
|
||||||
public MD5State (MD5State from) {
|
buffer = new byte[64];
|
||||||
this();
|
count = 0;
|
||||||
|
state = new int[4];
|
||||||
int i;
|
|
||||||
|
state[0] = 0x67452301;
|
||||||
for (i = 0; i < buffer.length; i++)
|
state[1] = 0xefcdab89;
|
||||||
this.buffer[i] = from.buffer[i];
|
state[2] = 0x98badcfe;
|
||||||
|
state[3] = 0x10325476;
|
||||||
for (i = 0; i < state.length; i++)
|
|
||||||
this.state[i] = from.state[i];
|
}
|
||||||
|
|
||||||
this.count = from.count;
|
/** 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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,50 +24,48 @@ import java.io.InputStream;
|
||||||
|
|
||||||
import marytts.util.io.FileUtils;
|
import marytts.util.io.FileUtils;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide Version information for the Mary server and client.
|
* Provide Version information for the Mary server and client.
|
||||||
|
*
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Version {
|
public class Version {
|
||||||
private static String specificationVersion;
|
private static String specificationVersion;
|
||||||
private static String implementationVersion;
|
private static String implementationVersion;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
InputStream specVersionStream = Version.class.
|
InputStream specVersionStream = Version.class.getResourceAsStream("specification-version.txt");
|
||||||
getResourceAsStream("specification-version.txt");
|
if (specVersionStream != null) {
|
||||||
if (specVersionStream != null) {
|
try {
|
||||||
try {
|
specificationVersion = FileUtils.getStreamAsString(specVersionStream, "UTF-8").trim();
|
||||||
specificationVersion = FileUtils.getStreamAsString(specVersionStream, "UTF-8").trim();
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
specificationVersion = "undeterminable";
|
||||||
specificationVersion = "undeterminable";
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
specificationVersion = "unknown";
|
||||||
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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Specification version */
|
InputStream implVersionStream = Version.class.getResourceAsStream("implementation-version.txt");
|
||||||
public static String specificationVersion() {
|
if (implVersionStream != null) {
|
||||||
return specificationVersion;
|
try {
|
||||||
}
|
implementationVersion = FileUtils.getStreamAsString(implVersionStream, "UTF-8").trim();
|
||||||
/** Implementation version */
|
} catch (IOException e) {
|
||||||
public static String implementationVersion() {
|
implementationVersion = "undeterminable";
|
||||||
return implementationVersion;
|
}
|
||||||
}
|
} else {
|
||||||
|
implementationVersion = "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Specification version */
|
||||||
|
public static String specificationVersion() {
|
||||||
|
return specificationVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Implementation version */
|
||||||
|
public static String implementationVersion() {
|
||||||
|
return implementationVersion;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,30 +20,27 @@
|
||||||
package marytts.exceptions;
|
package marytts.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A special type of expected error conditions
|
* A special type of expected error conditions This class represents error conditions for external scripts such as Exceptions at
|
||||||
* This class represents error conditions for external scripts
|
* runtime when processing fails
|
||||||
* such as Exceptions at runtime when processing fails
|
*
|
||||||
* @author sathish
|
* @author sathish
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ExecutionException extends Exception
|
public class ExecutionException extends Exception {
|
||||||
{
|
public ExecutionException() {
|
||||||
public ExecutionException()
|
super();
|
||||||
{
|
}
|
||||||
super();
|
|
||||||
}
|
public ExecutionException(String message) {
|
||||||
public ExecutionException(String message)
|
super(message);
|
||||||
{
|
}
|
||||||
super(message);
|
|
||||||
}
|
public ExecutionException(String message, Throwable cause) {
|
||||||
public ExecutionException(String message, Throwable cause)
|
super(message, cause);
|
||||||
{
|
}
|
||||||
super(message, cause);
|
|
||||||
}
|
public ExecutionException(Throwable cause) {
|
||||||
public ExecutionException(Throwable cause)
|
super(cause);
|
||||||
{
|
}
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,9 @@ package marytts.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An exception class representing cases where data provided to a processing unit does not match the specifications.
|
* An exception class representing cases where data provided to a processing unit does not match the specifications.
|
||||||
|
*
|
||||||
* @author marc
|
* @author marc
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class InvalidDataException extends RuntimeException {
|
public class InvalidDataException extends RuntimeException {
|
||||||
|
|
||||||
|
|
|
@ -20,40 +20,37 @@
|
||||||
package marytts.exceptions;
|
package marytts.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class representing severe expected error conditions,
|
* A class representing severe expected error conditions, such as wrong format of data files needed to set up the system.
|
||||||
* 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
|
||||||
* Typically a MaryConfigurationException means it is impossible to continue operating.
|
* is preferable to throw MaryConfigurationException during server startup, and to abort the startup if one is thrown.
|
||||||
* 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
|
* @author marc
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class MaryConfigurationException extends Exception
|
public class MaryConfigurationException extends Exception {
|
||||||
{
|
/**
|
||||||
/**
|
* Construct a MaryConfigurationException with only an error message. This constructor should only be used if our program code
|
||||||
* Construct a MaryConfigurationException with only an error message.
|
* has identified the error condition. In order to wrap another Exception into a MaryConfigurationException with a meaningful
|
||||||
* This constructor should only be used if our program code
|
* error message, use {@link #MaryConfigurationException(String, Throwable)}.
|
||||||
* has identified the error condition. In order to wrap
|
*
|
||||||
* another Exception into a MaryConfigurationException with a
|
* @param message
|
||||||
* meaningful error message, use {@link #MaryConfigurationException(String, Throwable)}.
|
* a meaningful error message describing the problem.
|
||||||
* @param message a meaningful error message describing the problem.
|
*/
|
||||||
*/
|
public MaryConfigurationException(String message) {
|
||||||
public MaryConfigurationException(String message)
|
super(message);
|
||||||
{
|
}
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a MaryConfigurationException with a message and a cause.
|
* Create a MaryConfigurationException with a message and a cause. Use this to wrap another Exception into a
|
||||||
* Use this to wrap another Exception into a MaryConfigurationException with a
|
* MaryConfigurationException with a meaningful error message.
|
||||||
* meaningful error message.
|
*
|
||||||
* @param message a meaningful error message describing the problem.
|
* @param message
|
||||||
* @param cause the exception or error that caused the problem.
|
* a meaningful error message describing the problem.
|
||||||
*/
|
* @param cause
|
||||||
public MaryConfigurationException(String message, Throwable cause)
|
* the exception or error that caused the problem.
|
||||||
{
|
*/
|
||||||
super(message, cause);
|
public MaryConfigurationException(String message, Throwable cause) {
|
||||||
}
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,16 +21,16 @@ package marytts.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Marc Schröder
|
* @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 class NoSuchPropertyException extends RuntimeException {
|
||||||
public NoSuchPropertyException(String message) {
|
public NoSuchPropertyException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
public NoSuchPropertyException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
public NoSuchPropertyException(String message, Throwable cause) {
|
||||||
}
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,24 +19,21 @@
|
||||||
*/
|
*/
|
||||||
package marytts.exceptions;
|
package marytts.exceptions;
|
||||||
|
|
||||||
public class SynthesisException extends Exception
|
public class SynthesisException extends Exception {
|
||||||
{
|
public SynthesisException() {
|
||||||
public SynthesisException()
|
super();
|
||||||
{
|
}
|
||||||
super();
|
|
||||||
}
|
public SynthesisException(String message) {
|
||||||
public SynthesisException(String message)
|
super(message);
|
||||||
{
|
}
|
||||||
super(message);
|
|
||||||
}
|
public SynthesisException(String message, Throwable cause) {
|
||||||
public SynthesisException(String message, Throwable cause)
|
super(message, cause);
|
||||||
{
|
}
|
||||||
super(message, cause);
|
|
||||||
}
|
public SynthesisException(Throwable cause) {
|
||||||
public SynthesisException(Throwable cause)
|
super(cause);
|
||||||
{
|
}
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,107 +29,111 @@ import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An implementation of a finite state transducer. This class does nothing but
|
* An implementation of a finite state transducer. This class does nothing but load and represent the FST. It is used by other
|
||||||
* load and represent the FST. It is used by other classes doing something
|
* classes doing something reasonable with it.
|
||||||
* reasonable with it.
|
*
|
||||||
* @author Andreas Eisele
|
* @author Andreas Eisele
|
||||||
*/
|
*/
|
||||||
public class FST
|
public class FST {
|
||||||
{
|
// The following variables are package-readable, so that they can be
|
||||||
// The following variables are package-readable, so that they can be
|
// directly accessed by all classes in this package.
|
||||||
// directly accessed by all classes in this package.
|
int[] targets;
|
||||||
int[] targets;
|
short[] labels;
|
||||||
short[] labels;
|
boolean[] isLast;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
short[] offsets;
|
||||||
* Load the fst from the given input stream. Assumes header.
|
byte[] bytes;
|
||||||
* @param inStream
|
int[] mapping;
|
||||||
* @throws IOException
|
ArrayList strings = new ArrayList();
|
||||||
*/
|
|
||||||
public FST(InputStream inStream) throws IOException {
|
|
||||||
load(inStream);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
* Load the fst from the given input stream. Assumes header.
|
||||||
* @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
|
* @param inStream
|
||||||
* or ISO-8859-1).
|
* @throws IOException
|
||||||
* @throws IOException if the FST cannot be loaded from the given file.
|
*/
|
||||||
* @throws UnsupportedEncodingException if the encoding is not supported.
|
public FST(InputStream inStream) throws IOException {
|
||||||
*/
|
load(inStream);
|
||||||
public FST(String fileName, String encoding)
|
}
|
||||||
throws IOException, UnsupportedEncodingException
|
|
||||||
{
|
|
||||||
this(fileName, encoding, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise the finite state transducer. This constructor will
|
* Initialise the finite state transducer. Loads from headerless legacy file format.
|
||||||
* assume that the file uses the system default encoding.
|
*
|
||||||
* @param fileName the name of the file from which to load the FST.
|
* @param fileName
|
||||||
* @param verbose whether to write a report to stderr after loading.
|
* the name of the file from which to load the FST.
|
||||||
* @throws IOException if the FST cannot be loaded from the given file.
|
* @param encoding
|
||||||
*/
|
* the name of the encoding used in the file (e.g., UTF-8 or ISO-8859-1).
|
||||||
public FST(String fileName, boolean verbose) throws IOException
|
* @throws IOException
|
||||||
{
|
* if the FST cannot be loaded from the given file.
|
||||||
this(fileName, null, verbose);
|
* @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.
|
* 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 encoding the name of the encoding used in the file (e.g., UTF-8
|
* @param fileName
|
||||||
* or ISO-8859-1).
|
* the name of the file from which to load the FST.
|
||||||
*
|
* @param verbose
|
||||||
* This constructor is to be used for old FST-files where the encoding was
|
* whether to write a report to stderr after loading.
|
||||||
* not yet specified in the header.
|
* @throws IOException
|
||||||
*
|
* if the FST cannot be loaded from the given file.
|
||||||
* @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 {
|
||||||
* @throws UnsupportedEncodingException if the encoding is not supported.
|
this(fileName, null, verbose);
|
||||||
*/
|
}
|
||||||
public FST(String fileName, String encoding, boolean verbose)
|
|
||||||
throws IOException, UnsupportedEncodingException
|
/**
|
||||||
{
|
* Initialise the finite state transducer.
|
||||||
FileInputStream fis = new FileInputStream(fileName);
|
*
|
||||||
try {
|
* @param fileName
|
||||||
loadHeaderless(fis, encoding, verbose);
|
* the name of the file from which to load the FST.
|
||||||
} finally {
|
* @param encoding
|
||||||
fis.close();
|
* 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
|
||||||
* Load the fst from the given input stream. Assumes headerless legacy file format.
|
* whether to write a report to stderr after loading.
|
||||||
* @param inStream
|
* @throws IOException
|
||||||
* @param encoding
|
* if the FST cannot be loaded from the given file.
|
||||||
* @throws IOException
|
* @throws UnsupportedEncodingException
|
||||||
* @throws UnsupportedEncodingException
|
* if the encoding is not supported.
|
||||||
*/
|
*/
|
||||||
public FST(InputStream inStream, String encoding) throws IOException, UnsupportedEncodingException {
|
public FST(String fileName, String encoding, boolean verbose) throws IOException, UnsupportedEncodingException {
|
||||||
loadHeaderless(inStream, encoding, false);
|
FileInputStream fis = new FileInputStream(fileName);
|
||||||
}
|
try {
|
||||||
|
loadHeaderless(fis, encoding, verbose);
|
||||||
private void load(InputStream inStream)
|
} 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
|
throws IOException, UnsupportedEncodingException
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -188,73 +192,64 @@ public class FST
|
||||||
createMapping(mapping, bytes, encoding);
|
createMapping(mapping, bytes, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadHeaderless(InputStream inStream, String encoding, boolean verbose)
|
private void loadHeaderless(InputStream inStream, String encoding, boolean verbose) throws IOException,
|
||||||
throws IOException, UnsupportedEncodingException
|
UnsupportedEncodingException {
|
||||||
{
|
int i;
|
||||||
int i;
|
DataInputStream in = new DataInputStream(new BufferedInputStream(inStream));
|
||||||
DataInputStream in = new DataInputStream(new BufferedInputStream(inStream));
|
// int fileSize= (int) f.length();
|
||||||
//int fileSize= (int) f.length();
|
int fileSize = in.available(); // TODO: how robust is this??
|
||||||
int fileSize = in.available(); // TODO: how robust is this??
|
int nArcs = in.readInt();
|
||||||
int nArcs=in.readInt();
|
// arcs = new int[nArcs];
|
||||||
// arcs = new int[nArcs];
|
|
||||||
|
targets = new int[nArcs];
|
||||||
targets = new int[nArcs];
|
labels = new short[nArcs];
|
||||||
labels = new short[nArcs];
|
isLast = new boolean[nArcs];
|
||||||
isLast = new boolean[nArcs];
|
|
||||||
|
for (i = 0; i < nArcs; i++) {
|
||||||
for(i=0; i<nArcs; i++) {
|
int thisArc = in.readInt();
|
||||||
int thisArc = in.readInt();
|
|
||||||
|
targets[i] = thisArc & 1048575;
|
||||||
targets[i]= thisArc&1048575;
|
labels[i] = (short) ((thisArc >> 20) & 2047);
|
||||||
labels[i]=(short)((thisArc>>20) & 2047);
|
isLast[i] = ((byte) (thisArc >> 31)) != 0;
|
||||||
isLast[i]=((byte)(thisArc >> 31))!=0;
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
int nPairs = in.readInt();
|
||||||
int nPairs=in.readInt();
|
offsets = new short[2 * nPairs];
|
||||||
offsets = new short[2*nPairs];
|
for (i = 0; i < 2 * nPairs; i++)
|
||||||
for(i=0; i<2*nPairs; i++)
|
offsets[i] = in.readShort();
|
||||||
offsets[i] = in.readShort();
|
int nBytes = fileSize - 8 - 4 * (nPairs + nArcs);
|
||||||
int nBytes = fileSize - 8 - 4 * (nPairs + nArcs);
|
mapping = new int[nBytes];
|
||||||
mapping=new int[nBytes];
|
bytes = new byte[nBytes];
|
||||||
bytes = new byte[nBytes];
|
in.readFully(bytes);
|
||||||
in.readFully(bytes);
|
if (verbose) {
|
||||||
if(verbose) {
|
System.err.println("FST (" + fileSize + " Bytes, " + nArcs + " Arcs, " + nPairs + " Labels)" + " loaded");
|
||||||
System.err.println("FST ("
|
}
|
||||||
+ fileSize + " Bytes, "
|
in.close();
|
||||||
+ nArcs + " Arcs, "
|
createMapping(mapping, bytes, encoding);
|
||||||
+ nPairs + " Labels)"
|
}
|
||||||
+ " loaded");
|
|
||||||
}
|
private void createMapping(int[] mapping, byte[] bytes, String encoding) throws UnsupportedEncodingException {
|
||||||
in.close();
|
mapping[0] = 0;
|
||||||
createMapping(mapping, bytes, encoding);
|
int last0 = -1;
|
||||||
}
|
String s;
|
||||||
|
int len;
|
||||||
private void createMapping(int[] mapping, byte[] bytes, String encoding)
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
throws UnsupportedEncodingException
|
if (bytes[i] == 0) {
|
||||||
{
|
len = i - last0 - 1;
|
||||||
mapping[0]=0;
|
if (len == 0)
|
||||||
int last0=-1;
|
strings.add("");
|
||||||
String s;
|
else {
|
||||||
int len;
|
String str;
|
||||||
for (int i=0;i<bytes.length;i++) {
|
if (encoding != null)
|
||||||
if (bytes[i]==0) {
|
str = new String(bytes, last0 + 1, len, encoding);
|
||||||
len=i-last0-1;
|
else
|
||||||
if (len==0) strings.add("");
|
str = new String(bytes, last0 + 1, len);
|
||||||
else {
|
strings.add(str);
|
||||||
String str;
|
}
|
||||||
if (encoding != null)
|
mapping[last0 + 1] = strings.size() - 1;
|
||||||
str = new String(bytes, last0+1, len, encoding);
|
last0 = i;
|
||||||
else
|
}
|
||||||
str = new String(bytes, last0+1, len);
|
}
|
||||||
strings.add(str);
|
}
|
||||||
}
|
|
||||||
mapping[last0+1]=strings.size()-1;
|
|
||||||
last0=i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -21,60 +21,57 @@ package marytts.fst;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Pair of Strings.
|
* A Pair of Strings.
|
||||||
|
*
|
||||||
* @author benjaminroth
|
* @author benjaminroth
|
||||||
*/
|
*/
|
||||||
public class StringPair{
|
public class StringPair {
|
||||||
|
|
||||||
//private boolean hasHash;
|
// private boolean hasHash;
|
||||||
//private int hash;
|
// private int hash;
|
||||||
private String string1;
|
private String string1;
|
||||||
private String string2;
|
private String string2;
|
||||||
|
|
||||||
public StringPair(String s1, String s2) {
|
public StringPair(String s1, String s2) {
|
||||||
this.string1 = s1;
|
this.string1 = s1;
|
||||||
this.string2 = s2;
|
this.string2 = s2;
|
||||||
//this.hasHash = false;
|
// this.hasHash = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setString1(String s1){
|
public void setString1(String s1) {
|
||||||
this.string1 = s1;
|
this.string1 = s1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setString2(String s2){
|
public void setString2(String s2) {
|
||||||
this.string2 = s2;
|
this.string2 = s2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
/*if (!hasHash){
|
/*
|
||||||
this.hash = 31 * string1.hashCode() + string2.hashCode();
|
* if (!hasHash){ this.hash = 31 * string1.hashCode() + string2.hashCode(); this.hasHash = true; }
|
||||||
this.hasHash = true;
|
*
|
||||||
}
|
* return this.hash;
|
||||||
|
*/
|
||||||
|
return 31 * string1.hashCode() + string2.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
return this.hash;*/
|
public boolean equals(Object o) {
|
||||||
return 31 * string1.hashCode() + string2.hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
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 &&
|
return false;
|
||||||
((StringPair) o).getString1().equals(string1) &&
|
}
|
||||||
((StringPair) o).getString2().equals(string2))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
public String getString1() {
|
||||||
}
|
return string1;
|
||||||
|
}
|
||||||
|
|
||||||
public String getString1() {
|
public String getString2() {
|
||||||
return string1;
|
return string2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getString2() {
|
public String toString() {
|
||||||
return string2;
|
return string1 + " " + string2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(){
|
|
||||||
return string1 + " " + string2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* Copyright 2007 DFKI GmbH.
|
* Copyright 2007 DFKI GmbH.
|
||||||
* All Rights Reserved. Use is subject to license terms.
|
* All Rights Reserved. Use is subject to license terms.
|
||||||
*
|
*
|
||||||
* This file is part of MARY TTS.
|
* This file is part of MARY TTS.
|
||||||
*
|
*
|
||||||
|
@ -17,491 +17,387 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -26,18 +26,17 @@ import java.util.TreeSet;
|
||||||
|
|
||||||
public class PrintSystemProperties {
|
public class PrintSystemProperties {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
Properties p = System.getProperties();
|
Properties p = System.getProperties();
|
||||||
SortedSet keys = new TreeSet(p.keySet());
|
SortedSet keys = new TreeSet(p.keySet());
|
||||||
for (Iterator it=keys.iterator(); it.hasNext(); ) {
|
for (Iterator it = keys.iterator(); it.hasNext();) {
|
||||||
String key = (String) it.next();
|
String key = (String) it.next();
|
||||||
System.out.println(key + " = " + p.getProperty(key));
|
System.out.println(key + " = " + p.getProperty(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,17 +24,15 @@ import java.io.Reader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A BufferedReader that ignores calls to close().
|
* A BufferedReader that ignores calls to close().
|
||||||
*
|
*
|
||||||
* @author Marc Schroeder
|
* @author Marc Schroeder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class UncloseableBufferedReader extends BufferedReader
|
public class UncloseableBufferedReader extends BufferedReader {
|
||||||
{
|
public UncloseableBufferedReader(Reader i) {
|
||||||
public UncloseableBufferedReader(Reader i) {
|
super(i);
|
||||||
super(i);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -43,80 +43,78 @@ import java.nio.ByteBuffer;
|
||||||
import marytts.exceptions.MaryConfigurationException;
|
import marytts.exceptions.MaryConfigurationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common helper class to read/write a standard Mary header to/from the various
|
* Common helper class to read/write a standard Mary header to/from the various Mary data files.
|
||||||
* Mary data files.
|
|
||||||
*
|
*
|
||||||
* @author sacha
|
* @author sacha
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class MaryHeader
|
public class MaryHeader {
|
||||||
{
|
/* Global constants */
|
||||||
/* Global constants */
|
private final static int MAGIC = 0x4d415259; // "MARY"
|
||||||
private final static int MAGIC = 0x4d415259; // "MARY"
|
private final static int VERSION = 40; // 4.0
|
||||||
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;
|
|
||||||
|
|
||||||
|
/* List of authorized file type identifier constants */
|
||||||
// STATIC CODE
|
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 */
|
||||||
* For the given file, look inside and determine the file type.
|
private int magic = MAGIC;
|
||||||
* @param fileName
|
private int version = VERSION;
|
||||||
* @return the file type, or -1 if the file does not have a valid MARY header.
|
private int type = UNKNOWN;
|
||||||
* @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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
// STATIC CODE
|
||||||
|
|
||||||
|
/**
|
||||||
/****************/
|
* For the given file, look inside and determine the file type.
|
||||||
/* CONSTRUCTORS */
|
*
|
||||||
/****************/
|
* @param fileName
|
||||||
|
* @return the file type, or -1 if the file does not have a valid MARY header.
|
||||||
/**
|
* @throws IOException
|
||||||
* Consruct a MaryHeader from scratch.
|
* if the file cannot be read
|
||||||
*
|
*/
|
||||||
* Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type.
|
public static int peekFileType(String fileName) throws IOException {
|
||||||
*
|
DataInputStream dis = null;
|
||||||
* @param newType The type of MaryHeader to create. See public final constants in this class.
|
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
|
||||||
*
|
/* Load the Mary header */
|
||||||
* @throws IllegalArgumentException if the input type is unknown.
|
try {
|
||||||
*/
|
MaryHeader hdr = new MaryHeader(dis);
|
||||||
public MaryHeader( int newType ) {
|
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) ) {
|
if ( (newType > TIMELINE) || (newType < UNKNOWN) ) {
|
||||||
throw new IllegalArgumentException( "Unauthorized Mary file type [" + type + "]." );
|
throw new IllegalArgumentException( "Unauthorized Mary file type [" + type + "]." );
|
||||||
}
|
}
|
||||||
|
@ -127,16 +125,18 @@ public class MaryHeader
|
||||||
assert hasLegalMagic();
|
assert hasLegalMagic();
|
||||||
assert hasLegalType();
|
assert hasLegalType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a MaryHeader by reading from a file.
|
* Construct a MaryHeader by reading from a file. Fundamental guarantee: after construction, the MaryHeader has a valid magic
|
||||||
* Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type.
|
* number and a valid type.
|
||||||
*
|
*
|
||||||
* @param input a DataInputStream or RandomAccessFile to read the header from.
|
* @param input
|
||||||
*
|
* a DataInputStream or RandomAccessFile to read the header from.
|
||||||
* @throws MaryConfigurationException if no mary header can be read from input.
|
*
|
||||||
*/
|
* @throws MaryConfigurationException
|
||||||
public MaryHeader( DataInput input ) throws MaryConfigurationException {
|
* if no mary header can be read from input.
|
||||||
|
*/
|
||||||
|
public MaryHeader( DataInput input ) throws MaryConfigurationException {
|
||||||
try {
|
try {
|
||||||
this.load( input );
|
this.load( input );
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -151,15 +151,17 @@ public class MaryHeader
|
||||||
assert hasLegalType();
|
assert hasLegalType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a MaryHeader by reading from a file.
|
* Construct a MaryHeader by reading from a file. Fundamental guarantee: after construction, the MaryHeader has a valid magic
|
||||||
* Fundamental guarantee: after construction, the MaryHeader has a valid magic number and a valid type.
|
* number and a valid type.
|
||||||
*
|
*
|
||||||
* @param input a byte buffer to read the header from.
|
* @param input
|
||||||
*
|
* a byte buffer to read the header from.
|
||||||
* @throws MaryConfigurationException if no mary header can be read from input.
|
*
|
||||||
*/
|
* @throws MaryConfigurationException
|
||||||
public MaryHeader( ByteBuffer input ) throws MaryConfigurationException {
|
* if no mary header can be read from input.
|
||||||
|
*/
|
||||||
|
public MaryHeader( ByteBuffer input ) throws MaryConfigurationException {
|
||||||
try {
|
try {
|
||||||
this.load( input );
|
this.load( input );
|
||||||
} catch (BufferUnderflowException e) {
|
} catch (BufferUnderflowException e) {
|
||||||
|
@ -174,19 +176,22 @@ public class MaryHeader
|
||||||
assert hasLegalType();
|
assert hasLegalType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************/
|
/*****************/
|
||||||
/* OTHER METHODS */
|
/* OTHER METHODS */
|
||||||
/*****************/
|
/*****************/
|
||||||
|
|
||||||
/** Mary header writer
|
/**
|
||||||
*
|
* Mary header writer
|
||||||
* @param output The DataOutputStream or RandomAccessFile to write to
|
*
|
||||||
*
|
* @param output
|
||||||
* @return the number of written bytes.
|
* The DataOutputStream or RandomAccessFile to write to
|
||||||
*
|
*
|
||||||
* @throws IOException if the file type is unknown.
|
* @return the number of written bytes.
|
||||||
*/
|
*
|
||||||
public long writeTo( DataOutput output ) throws IOException {
|
* @throws IOException
|
||||||
|
* if the file type is unknown.
|
||||||
|
*/
|
||||||
|
public long writeTo( DataOutput output ) throws IOException {
|
||||||
|
|
||||||
long nBytes = 0;
|
long nBytes = 0;
|
||||||
|
|
||||||
|
@ -198,44 +203,61 @@ public class MaryHeader
|
||||||
|
|
||||||
return( nBytes );
|
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 ); }
|
* Load a mary header.
|
||||||
private boolean hasLegalType() {
|
*
|
||||||
return (type <= TIMELINE) && (type > UNKNOWN) ;
|
* @param input
|
||||||
}
|
* The data input (DataInputStream or RandomAccessFile) to read from.
|
||||||
private boolean hasLegalMagic() {
|
*
|
||||||
return( magic == MAGIC );
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,63 +29,46 @@ import org.xml.sax.ErrorHandler;
|
||||||
import org.xml.sax.SAXParseException;
|
import org.xml.sax.SAXParseException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements an ErrorHandler for XML parsing
|
* Implements an ErrorHandler for XML parsing that provides error and warning messages to the log4j logger.
|
||||||
* that provides error and warning messages to the log4j logger.
|
*
|
||||||
*
|
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class LoggingErrorHandler implements ErrorHandler, ErrorListener
|
public class LoggingErrorHandler implements ErrorHandler, ErrorListener {
|
||||||
{
|
Logger logger;
|
||||||
Logger logger;
|
|
||||||
public LoggingErrorHandler(String name)
|
|
||||||
{
|
|
||||||
logger = MaryUtils.getLogger(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error(SAXParseException e)
|
public LoggingErrorHandler(String name) {
|
||||||
throws SAXParseException
|
logger = MaryUtils.getLogger(name);
|
||||||
{
|
}
|
||||||
logger.warn(e.getMessage());
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error(TransformerException e)
|
public void error(SAXParseException e) throws SAXParseException {
|
||||||
throws TransformerException
|
logger.warn(e.getMessage());
|
||||||
{
|
throw e;
|
||||||
logger.warn(e.getMessageAndLocation());
|
}
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void warning(SAXParseException e)
|
public void error(TransformerException e) throws TransformerException {
|
||||||
throws SAXParseException
|
logger.warn(e.getMessageAndLocation());
|
||||||
{
|
throw e;
|
||||||
logger.warn(e.getMessage());
|
}
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void warning(TransformerException e)
|
public void warning(SAXParseException e) throws SAXParseException {
|
||||||
throws TransformerException
|
logger.warn(e.getMessage());
|
||||||
{
|
throw e;
|
||||||
logger.warn(e.getMessageAndLocation());
|
}
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void fatalError(SAXParseException e)
|
public void warning(TransformerException e) throws TransformerException {
|
||||||
throws SAXParseException
|
logger.warn(e.getMessageAndLocation());
|
||||||
{
|
throw e;
|
||||||
logger.warn(e.getMessage());
|
}
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void fatalError(TransformerException e)
|
public void fatalError(SAXParseException e) throws SAXParseException {
|
||||||
throws TransformerException
|
logger.warn(e.getMessage());
|
||||||
{
|
throw e;
|
||||||
logger.warn(e.getMessageAndLocation());
|
}
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void fatalError(TransformerException e) throws TransformerException {
|
||||||
|
logger.warn(e.getMessageAndLocation());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,114 +46,113 @@ import org.apache.log4j.Logger;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper class for output of XML DOM trees in a Mary normalised way:
|
* 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
|
||||||
* One tag or text node per line, no indentation.
|
* only needed during the transition phase to "real" XML modules.
|
||||||
* This is only needed during the transition phase to "real" XML modules.
|
*
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class MaryNormalisedWriter {
|
public class MaryNormalisedWriter {
|
||||||
private static TransformerFactory tFactory = null;
|
private static TransformerFactory tFactory = null;
|
||||||
private static Templates stylesheet = 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 <code>startup()</code> if it has not been called before.
|
* Default constructor. Calls <code>startup()</code> if it has not been called before.
|
||||||
* @see #startup().
|
*
|
||||||
*/
|
* @see #startup().
|
||||||
public MaryNormalisedWriter()
|
*/
|
||||||
throws MaryConfigurationException
|
public MaryNormalisedWriter() throws MaryConfigurationException {
|
||||||
{
|
try {
|
||||||
try {
|
// startup every time:
|
||||||
// startup every time:
|
startup();
|
||||||
startup();
|
transformer = stylesheet.newTransformer();
|
||||||
transformer = stylesheet.newTransformer();
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
throw new MaryConfigurationException("Cannot initialise XML writing code", 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.
|
* 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 TransformerFactoryConfigurationError
|
||||||
* @exception FileNotFoundException
|
* if the TransformerFactory cannot be instanciated.
|
||||||
* if the stylesheet file cannot be found.
|
* @exception FileNotFoundException
|
||||||
* @exception TransformerConfigurationException
|
* if the stylesheet file cannot be found.
|
||||||
* if the templates stylesheet cannot be generated.
|
* @exception TransformerConfigurationException
|
||||||
*/
|
* if the templates stylesheet cannot be generated.
|
||||||
private static void startup()
|
*/
|
||||||
throws TransformerFactoryConfigurationError, TransformerConfigurationException
|
private static void startup() throws TransformerFactoryConfigurationError, TransformerConfigurationException {
|
||||||
{
|
// only start the stuff if it hasn't been started yet.
|
||||||
// only start the stuff if it hasn't been started yet.
|
if (tFactory == null) {
|
||||||
if (tFactory == null) {
|
tFactory = TransformerFactory.newInstance();
|
||||||
tFactory = TransformerFactory.newInstance();
|
}
|
||||||
}
|
if (stylesheet == null) {
|
||||||
if (stylesheet == null) {
|
StreamSource stylesheetStream = new StreamSource(
|
||||||
StreamSource stylesheetStream =
|
MaryNormalisedWriter.class.getResourceAsStream("normalise-maryxml.xsl"));
|
||||||
new StreamSource(
|
stylesheet = tFactory.newTemplates(stylesheetStream);
|
||||||
MaryNormalisedWriter.class.getResourceAsStream(
|
}
|
||||||
"normalise-maryxml.xsl"));
|
if (logger == null)
|
||||||
stylesheet = tFactory.newTemplates(stylesheetStream);
|
logger = MaryUtils.getLogger("MaryNormalisedWriter");
|
||||||
}
|
|
||||||
if (logger == null)
|
|
||||||
logger = MaryUtils.getLogger("MaryNormalisedWriter");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The actual output to stdout.
|
/**
|
||||||
* @param input a DOMSource, a SAXSource or a StreamSource.
|
* The actual output to stdout.
|
||||||
* @see javax.xml.transform.Transformer
|
*
|
||||||
* @exception TransformerException
|
* @param input
|
||||||
* if the transformation cannot be performed.
|
* a DOMSource, a SAXSource or a StreamSource.
|
||||||
*/
|
* @see javax.xml.transform.Transformer
|
||||||
public void output(Source input, Result destination) throws TransformerException {
|
* @exception TransformerException
|
||||||
//logger.debug("Before transform");
|
* if the transformation cannot be performed.
|
||||||
transformer.transform(input, destination);
|
*/
|
||||||
//logger.debug("After transform");
|
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)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Output a DOM node to stdout.
|
/**
|
||||||
* @see #output(Source)
|
* Output any Source to stdout.
|
||||||
*/
|
*/
|
||||||
public void output(Node input) throws TransformerException {
|
public void output(Source input) throws TransformerException {
|
||||||
output(new DOMSource(input));
|
output(input, new StreamResult(new PrintStream(System.out, true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output a DOM node to a specified destination
|
* Output a DOM node to stdout.
|
||||||
*/
|
*
|
||||||
public void output(Node input, OutputStream destination) throws TransformerException {
|
* @see #output(Source)
|
||||||
output(new DOMSource(input), new StreamResult(destination));
|
*/
|
||||||
}
|
public void output(Node input) throws TransformerException {
|
||||||
|
output(new DOMSource(input));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The simplest possible command line interface to the
|
* Output a DOM node to a specified destination
|
||||||
* MaryNormalisedWriter. Reads a "real" XML document from stdin,
|
*/
|
||||||
* and outputs it in the MaryNormalised form to stdout.
|
public void output(Node input, OutputStream destination) throws TransformerException {
|
||||||
*/
|
output(new DOMSource(input), new StreamResult(destination));
|
||||||
public static void main(String[] args) throws Throwable {
|
}
|
||||||
startup();
|
|
||||||
MaryNormalisedWriter writer = new MaryNormalisedWriter();
|
|
||||||
|
|
||||||
ReaderSplitter splitter = new ReaderSplitter(new InputStreamReader(System.in), "</maryxml>");
|
/**
|
||||||
|
* 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;
|
ReaderSplitter splitter = new ReaderSplitter(new InputStreamReader(System.in), "</maryxml>");
|
||||||
while ((oneXMLStructure = splitter.nextReader()) != null) {
|
|
||||||
writer.output(new StreamSource(oneXMLStructure));
|
Reader oneXMLStructure = null;
|
||||||
}
|
while ((oneXMLStructure = splitter.nextReader()) != null) {
|
||||||
}
|
writer.output(new StreamSource(oneXMLStructure));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,32 +24,27 @@ import java.util.regex.Pattern;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.traversal.NodeFilter;
|
import org.w3c.dom.traversal.NodeFilter;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A NodeFilter accepting only nodes with names matching
|
* A NodeFilter accepting only nodes with names matching a given regular expression.
|
||||||
* a given regular expression.
|
*
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class RENodeFilter implements NodeFilter
|
public class RENodeFilter implements NodeFilter {
|
||||||
{
|
private Pattern re;
|
||||||
private Pattern re;
|
|
||||||
public RENodeFilter(String reString)
|
|
||||||
{
|
|
||||||
this.re = Pattern.compile(reString);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RENodeFilter(Pattern re)
|
public RENodeFilter(String reString) {
|
||||||
{
|
this.re = Pattern.compile(reString);
|
||||||
this.re = re;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public short acceptNode(Node n)
|
public RENodeFilter(Pattern re) {
|
||||||
{
|
this.re = re;
|
||||||
if (re.matcher(n.getNodeName()).matches())
|
}
|
||||||
return NodeFilter.FILTER_ACCEPT;
|
|
||||||
else
|
public short acceptNode(Node n) {
|
||||||
return NodeFilter.FILTER_SKIP;
|
if (re.matcher(n.getNodeName()).matches())
|
||||||
}
|
return NodeFilter.FILTER_ACCEPT;
|
||||||
|
else
|
||||||
|
return NodeFilter.FILTER_SKIP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* Copyright 2007 DFKI GmbH.
|
* Copyright 2007 DFKI GmbH.
|
||||||
* All Rights Reserved. Use is subject to license terms.
|
* All Rights Reserved. Use is subject to license terms.
|
||||||
*
|
*
|
||||||
* This file is part of MARY TTS.
|
* This file is part of MARY TTS.
|
||||||
*
|
*
|
||||||
|
@ -17,83 +17,80 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.http;
|
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; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -44,331 +44,346 @@ import java.util.Arrays;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The BasenameList class produces and stores an alphabetically-sorted
|
* The BasenameList class produces and stores an alphabetically-sorted array of basenames issued from the .wav files present in a
|
||||||
* array of basenames issued from the .wav files present in a given directory.
|
* given directory.
|
||||||
*
|
*
|
||||||
* @author sacha
|
* @author sacha
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BasenameList
|
public class BasenameList {
|
||||||
{
|
private Vector bList = null;
|
||||||
private Vector bList = null;
|
private String fromDir = null;
|
||||||
private String fromDir = null;
|
private String fromExt = null;
|
||||||
private String fromExt = null;
|
private boolean hasChanged;
|
||||||
private boolean hasChanged;
|
private static final int DEFAULT_INCREMENT = 128;
|
||||||
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 .<extension> 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.
|
/* CONSTRUCTORS */
|
||||||
*/
|
/****************/
|
||||||
public void write( String fileName ) throws IOException {
|
|
||||||
write( new File( fileName ) );
|
/**
|
||||||
}
|
* Default constructor for an empty list.
|
||||||
|
*/
|
||||||
/**
|
public BasenameList() {
|
||||||
* Write the basenameList to a File.
|
fromDir = null;
|
||||||
*/
|
fromExt = null;
|
||||||
public void write( File file ) throws IOException {
|
bList = new Vector(DEFAULT_INCREMENT, DEFAULT_INCREMENT);
|
||||||
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ), true );
|
hasChanged = false;
|
||||||
if ( fromDir != null ) {
|
}
|
||||||
pw.println( "FROM: " + fromDir + "*" + fromExt );
|
|
||||||
}
|
/**
|
||||||
String str = null;
|
* Default constructor from an existing vector and fields.
|
||||||
for ( int i = 0; i < bList.size(); i++ ) {
|
*/
|
||||||
str = (String)(bList.elementAt(i));
|
public BasenameList(String setFromDir, String setFromExt, Vector setVec) {
|
||||||
pw.println( str );
|
fromDir = setFromDir;
|
||||||
}
|
fromExt = setFromExt;
|
||||||
}
|
bList = setVec;
|
||||||
|
hasChanged = false;
|
||||||
/**
|
}
|
||||||
* Read the basenameList from a file
|
|
||||||
*/
|
/**
|
||||||
public void load( String fileName ) throws IOException {
|
* Constructor from an array of strings.
|
||||||
/* Open the file */
|
*/
|
||||||
BufferedReader bfr = new BufferedReader( new InputStreamReader( new FileInputStream( fileName ), "UTF-8" ) );
|
public BasenameList(String[] str) {
|
||||||
/* Make the vector */
|
fromDir = null;
|
||||||
if ( bList == null ) bList = new Vector( DEFAULT_INCREMENT, DEFAULT_INCREMENT );
|
fromExt = null;
|
||||||
/* Check if the first line contains the origin information (directory+ext) */
|
bList = new Vector(DEFAULT_INCREMENT, DEFAULT_INCREMENT);
|
||||||
String line = bfr.readLine();
|
add(str);
|
||||||
if ( line.indexOf("FROM: ") != -1 ) {
|
hasChanged = false;
|
||||||
line = line.substring( 6 );
|
}
|
||||||
String[] parts = new String[2];
|
|
||||||
parts = line.split( "\\*", 2 );
|
/**
|
||||||
fromDir = parts[0];
|
* This constructor lists the .<extension> files from directory dir, and initializes an an array with their list of
|
||||||
fromExt = parts[1];
|
* alphabetically sorted basenames.
|
||||||
}
|
*
|
||||||
else if ( !(line.matches("^\\s*$")) ) add( line );
|
* @param dir
|
||||||
/* Add the lines to the vector, ignoring the blank ones. */
|
* The name of the directory to list the files from.
|
||||||
while ( (line = bfr.readLine()) != null ) {
|
* @param extension
|
||||||
if ( !(line.matches("^\\s*$")) ) add( line );
|
* The extension of the files to list.
|
||||||
}
|
*
|
||||||
}
|
*/
|
||||||
|
public BasenameList(String dirName, final String extension) {
|
||||||
|
fromDir = dirName;
|
||||||
/*****************/
|
if (extension.indexOf(".") != 0)
|
||||||
/* OTHER METHODS */
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,60 +1,56 @@
|
||||||
/**
|
/**
|
||||||
* Copyright 2007 DFKI GmbH.
|
* Copyright 2007 DFKI GmbH.
|
||||||
* All Rights Reserved. Use is subject to license terms.
|
* All Rights Reserved. Use is subject to license terms.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to use and distribute
|
* Permission is hereby granted, free of charge, to use and distribute
|
||||||
* this software and its documentation without restriction, including
|
* this software and its documentation without restriction, including
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
* distribute, sublicense, and/or sell copies of this work, and to
|
* distribute, sublicense, and/or sell copies of this work, and to
|
||||||
* permit persons to whom this work is furnished to do so, subject to
|
* permit persons to whom this work is furnished to do so, subject to
|
||||||
* the following conditions:
|
* the following conditions:
|
||||||
*
|
*
|
||||||
* 1. The code must retain the above copyright notice, this list of
|
* 1. The code must retain the above copyright notice, this list of
|
||||||
* conditions and the following disclaimer.
|
* conditions and the following disclaimer.
|
||||||
* 2. Any modifications must be clearly marked as such.
|
* 2. Any modifications must be clearly marked as such.
|
||||||
* 3. Original authors' names are not deleted.
|
* 3. Original authors' names are not deleted.
|
||||||
* 4. The authors' names are not used to endorse or promote products
|
* 4. The authors' names are not used to endorse or promote products
|
||||||
* derived from this software without specific prior written
|
* derived from this software without specific prior written
|
||||||
* permission.
|
* permission.
|
||||||
*
|
*
|
||||||
* DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
|
* DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
|
||||||
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
* MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
|
* MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
|
||||||
* CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
* CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
||||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||||
* THIS SOFTWARE.
|
* THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package marytts.util.io;
|
package marytts.util.io;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* @author oytun.turk
|
||||||
* @author oytun.turk
|
*
|
||||||
*
|
*/
|
||||||
*/
|
public class FileFilter implements FilenameFilter {
|
||||||
public class FileFilter implements FilenameFilter
|
private String extension;
|
||||||
{
|
|
||||||
private String extension;
|
public FileFilter(String ext) {
|
||||||
|
if (ext.startsWith(".") || ext.compareTo("*.*") == 0)
|
||||||
public FileFilter(String ext)
|
extension = ext;
|
||||||
{
|
else
|
||||||
if (ext.startsWith(".") || ext.compareTo("*.*")==0)
|
extension = "." + ext;
|
||||||
extension = ext;
|
}
|
||||||
else
|
|
||||||
extension = "." + ext;
|
public boolean accept(File dir, String name) {
|
||||||
}
|
if (extension.compareTo("*.*") == 0)
|
||||||
|
return true;
|
||||||
public boolean accept(File dir, String name)
|
else
|
||||||
{
|
return name.endsWith(extension);
|
||||||
if (extension.compareTo("*.*")==0)
|
}
|
||||||
return true;
|
|
||||||
else
|
}
|
||||||
return name.endsWith(extension);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -17,437 +17,409 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.io;
|
package marytts.util.io;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LEDataInputStream.java
|
* LEDataInputStream.java
|
||||||
* copyright (c) 1998-2007 Roedy Green, Canadian Mind* Products
|
* copyright (c) 1998-2007 Roedy Green, Canadian Mind* Products
|
||||||
* Very similar to DataInputStream except it reads
|
* Very similar to DataInputStream except it reads
|
||||||
* little-endian instead of big-endian binary data. We can't extend
|
* little-endian instead of big-endian binary data. We can't extend
|
||||||
* DataInputStream directly since it has only final methods, though
|
* DataInputStream directly since it has only final methods, though
|
||||||
* DataInputStream itself is not final. This forces us implement
|
* DataInputStream itself is not final. This forces us implement
|
||||||
* LEDataInputStream with a DataInputStream object, and use wrapper methods.
|
* LEDataInputStream with a DataInputStream object, and use wrapper methods.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.DataInput;
|
import java.io.DataInput;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reads little endian binary data .
|
* reads little endian binary data .
|
||||||
*/
|
*/
|
||||||
public class LEDataInputStream implements DataInput {
|
public class LEDataInputStream implements DataInput {
|
||||||
|
|
||||||
// ------------------------------ FIELDS ------------------------------
|
// ------------------------------ FIELDS ------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* undisplayed copyright notice.
|
* undisplayed copyright notice.
|
||||||
*
|
*
|
||||||
* @noinspection UnusedDeclaration
|
* @noinspection UnusedDeclaration
|
||||||
*/
|
*/
|
||||||
private static final String EMBEDDEDCOPYRIGHT =
|
private static final String EMBEDDEDCOPYRIGHT = "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com";
|
||||||
"copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com";
|
|
||||||
|
/**
|
||||||
/**
|
* to get at the big-Endian methods of a basic DataInputStream
|
||||||
* to get at the big-Endian methods of a basic DataInputStream
|
*
|
||||||
*
|
* @noinspection WeakerAccess
|
||||||
* @noinspection WeakerAccess
|
*/
|
||||||
*/
|
protected final DataInputStream dis;
|
||||||
protected final DataInputStream dis;
|
|
||||||
|
/**
|
||||||
/**
|
* to get at the a basic readBytes method.
|
||||||
* to get at the a basic readBytes method.
|
*
|
||||||
*
|
* @noinspection WeakerAccess
|
||||||
* @noinspection WeakerAccess
|
*/
|
||||||
*/
|
protected final InputStream is;
|
||||||
protected final InputStream is;
|
|
||||||
|
/**
|
||||||
/**
|
* work array for buffering input.
|
||||||
* work array for buffering input.
|
*
|
||||||
*
|
* @noinspection WeakerAccess
|
||||||
* @noinspection WeakerAccess
|
*/
|
||||||
*/
|
protected final byte[] work;
|
||||||
protected final byte[] work;
|
|
||||||
|
// -------------------------- PUBLIC STATIC METHODS --------------------------
|
||||||
// -------------------------- PUBLIC STATIC METHODS --------------------------
|
|
||||||
|
/**
|
||||||
/**
|
* Note. This is a STATIC method!
|
||||||
* Note. This is a STATIC method!
|
*
|
||||||
*
|
* @param in
|
||||||
* @param in stream to read UTF chars from (endian irrelevant)
|
* stream to read UTF chars from (endian irrelevant)
|
||||||
*
|
*
|
||||||
* @return string from stream
|
* @return string from stream
|
||||||
*
|
*
|
||||||
* @throws IOException if read fails.
|
* @throws IOException
|
||||||
*/
|
* if read fails.
|
||||||
public static String readUTF( DataInput in ) throws IOException
|
*/
|
||||||
{
|
public static String readUTF(DataInput in) throws IOException {
|
||||||
return DataInputStream.readUTF( in );
|
return DataInputStream.readUTF(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
|
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
|
||||||
/**
|
/**
|
||||||
* constructor.
|
* constructor.
|
||||||
*
|
*
|
||||||
* @param in binary inputstream of little-endian data.
|
* @param in
|
||||||
*/
|
* binary inputstream of little-endian data.
|
||||||
public LEDataInputStream( InputStream in )
|
*/
|
||||||
{
|
public LEDataInputStream(InputStream in) {
|
||||||
this.is = in;
|
this.is = in;
|
||||||
this.dis = new DataInputStream( in );
|
this.dis = new DataInputStream(in);
|
||||||
work = new byte[8];
|
work = new byte[8];
|
||||||
}
|
}
|
||||||
|
|
||||||
public LEDataInputStream( String filename) throws FileNotFoundException
|
public LEDataInputStream(String filename) throws FileNotFoundException {
|
||||||
{
|
this(new FileInputStream(filename));
|
||||||
this(new FileInputStream(filename));
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* close.
|
||||||
* close.
|
*
|
||||||
*
|
* @throws IOException
|
||||||
* @throws IOException if close fails.
|
* if close fails.
|
||||||
*/
|
*/
|
||||||
public final void close() throws IOException
|
public final void close() throws IOException {
|
||||||
{
|
dis.close();
|
||||||
dis.close();
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Read bytes. Watch out, read may return fewer bytes than requested.
|
||||||
* Read bytes. Watch out, read may return fewer bytes than requested.
|
*
|
||||||
*
|
* @param ba
|
||||||
* @param ba where the bytes go.
|
* where the bytes go.
|
||||||
* @param off offset in buffer, not offset in file.
|
* @param off
|
||||||
* @param len count of bytes to read.
|
* offset in buffer, not offset in file.
|
||||||
*
|
* @param len
|
||||||
* @return how many bytes read.
|
* count of bytes to read.
|
||||||
*
|
*
|
||||||
* @throws IOException if read fails.
|
* @return how many bytes read.
|
||||||
*/
|
*
|
||||||
public final int read( byte ba[], int off, int len ) throws IOException
|
* @throws IOException
|
||||||
{
|
* if read fails.
|
||||||
// For efficiency, we avoid one layer of wrapper
|
*/
|
||||||
return is.read( ba, off, len );
|
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.
|
* read only a one-byte boolean.
|
||||||
*
|
*
|
||||||
* @throws IOException if read fails.
|
* @return true or false.
|
||||||
* @see java.io.DataInput#readBoolean()
|
*
|
||||||
*/
|
* @throws IOException
|
||||||
public final boolean readBoolean() throws IOException
|
* if read fails.
|
||||||
{
|
* @see java.io.DataInput#readBoolean()
|
||||||
return dis.readBoolean();
|
*/
|
||||||
}
|
public final boolean readBoolean() throws IOException {
|
||||||
|
return dis.readBoolean();
|
||||||
public final boolean [] readBoolean(int len) throws IOException
|
}
|
||||||
{
|
|
||||||
boolean [] ret = new boolean[len];
|
public final boolean[] readBoolean(int len) throws IOException {
|
||||||
|
boolean[] ret = new boolean[len];
|
||||||
for (int i=0; i<len; i++)
|
|
||||||
ret[i] = readBoolean();
|
for (int i = 0; i < len; i++)
|
||||||
|
ret[i] = readBoolean();
|
||||||
return ret;
|
|
||||||
}
|
return ret;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* read byte.
|
/**
|
||||||
*
|
* read byte.
|
||||||
* @return the byte read.
|
*
|
||||||
*
|
* @return the byte read.
|
||||||
* @throws IOException if read fails.
|
*
|
||||||
* @see java.io.DataInput#readByte()
|
* @throws IOException
|
||||||
*/
|
* if read fails.
|
||||||
public final byte readByte() throws IOException
|
* @see java.io.DataInput#readByte()
|
||||||
{
|
*/
|
||||||
return dis.readByte();
|
public final byte readByte() throws IOException {
|
||||||
}
|
return dis.readByte();
|
||||||
|
}
|
||||||
public final byte [] readByte(int len) throws IOException
|
|
||||||
{
|
public final byte[] readByte(int len) throws IOException {
|
||||||
byte [] ret = new byte[len];
|
byte[] ret = new byte[len];
|
||||||
|
|
||||||
for (int i=0; i<len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
ret[i] = readByte();
|
ret[i] = readByte();
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read on char. like DataInputStream.readChar except little endian.
|
* Read on char. like DataInputStream.readChar except little endian.
|
||||||
*
|
*
|
||||||
* @return little endian 16-bit unicode char from the stream.
|
* @return little endian 16-bit unicode char from the stream.
|
||||||
*
|
*
|
||||||
* @throws IOException if read fails.
|
* @throws IOException
|
||||||
*/
|
* if read fails.
|
||||||
public final char readChar() throws IOException
|
*/
|
||||||
{
|
public final char readChar() throws IOException {
|
||||||
dis.readFully( work, 0, 2 );
|
dis.readFully(work, 0, 2);
|
||||||
return (char) ( ( work[ 1 ] & 0xff ) << 8 | ( work[ 0 ] & 0xff ) );
|
return (char) ((work[1] & 0xff) << 8 | (work[0] & 0xff));
|
||||||
}
|
}
|
||||||
|
|
||||||
public final char [] readChar(int len) throws IOException
|
public final char[] readChar(int len) throws IOException {
|
||||||
{
|
char[] ret = new char[len];
|
||||||
char [] ret = new char[len];
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
for (int i=0; i<len; i++)
|
ret[i] = readChar();
|
||||||
ret[i] = readChar();
|
|
||||||
|
return ret;
|
||||||
return ret;
|
}
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Read a double. like DataInputStream.readDouble except little endian.
|
* Read a double. like DataInputStream.readDouble except little endian.
|
||||||
*
|
*
|
||||||
* @return little endian IEEE double from the datastream.
|
* @return little endian IEEE double from the datastream.
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public final double readDouble() throws IOException
|
public final double readDouble() throws IOException {
|
||||||
{
|
return Double.longBitsToDouble(readLong());
|
||||||
return Double.longBitsToDouble( readLong() );
|
}
|
||||||
}
|
|
||||||
|
public final double[] readDouble(int len) throws IOException {
|
||||||
public final double [] readDouble(int len) throws IOException
|
double[] ret = new double[len];
|
||||||
{
|
|
||||||
double [] ret = new double[len];
|
for (int i = 0; i < len; i++)
|
||||||
|
ret[i] = readDouble();
|
||||||
for (int i=0; i<len; i++)
|
|
||||||
ret[i] = readDouble();
|
return ret;
|
||||||
|
}
|
||||||
return ret;
|
|
||||||
}
|
public final int[] readDoubleToInt(int len) throws IOException {
|
||||||
|
int[] ret = new int[len];
|
||||||
public final int [] readDoubleToInt(int len) throws IOException
|
|
||||||
{
|
for (int i = 0; i < len; i++)
|
||||||
int [] ret = new int[len];
|
ret[i] = (int) readDouble();
|
||||||
|
|
||||||
for (int i=0; i<len; i++)
|
return ret;
|
||||||
ret[i] = (int)readDouble();
|
}
|
||||||
|
|
||||||
return ret;
|
/**
|
||||||
}
|
* Read one float. Like DataInputStream.readFloat except little endian.
|
||||||
|
*
|
||||||
/**
|
* @return little endian IEEE float from the datastream.
|
||||||
* Read one float. Like DataInputStream.readFloat except little endian.
|
*
|
||||||
*
|
* @throws IOException
|
||||||
* @return little endian IEEE float from the datastream.
|
* if read fails.
|
||||||
*
|
*/
|
||||||
* @throws IOException if read fails.
|
public final float readFloat() throws IOException {
|
||||||
*/
|
return Float.intBitsToFloat(readInt());
|
||||||
public final float readFloat() throws IOException
|
}
|
||||||
{
|
|
||||||
return Float.intBitsToFloat( readInt() );
|
public final float[] readFloat(int len) throws IOException {
|
||||||
}
|
float[] ret = new float[len];
|
||||||
|
|
||||||
public final float [] readFloat(int len) throws IOException
|
for (int i = 0; i < len; i++)
|
||||||
{
|
ret[i] = readFloat();
|
||||||
float [] ret = new float[len];
|
|
||||||
|
return ret;
|
||||||
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[])
|
||||||
/**
|
*/
|
||||||
* Read bytes until the array is filled.
|
public final void readFully(byte ba[]) throws IOException {
|
||||||
*
|
dis.readFully(ba, 0, ba.length);
|
||||||
* @see java.io.DataInput#readFully(byte[])
|
}
|
||||||
*/
|
|
||||||
public final void readFully( byte ba[] ) throws IOException
|
/**
|
||||||
{
|
* Read bytes until the count is satisfied.
|
||||||
dis.readFully( ba, 0, ba.length );
|
*
|
||||||
}
|
* @throws IOException
|
||||||
|
* if read fails.
|
||||||
/**
|
* @see java.io.DataInput#readFully(byte[],int,int)
|
||||||
* Read bytes until the count is satisfied.
|
*/
|
||||||
*
|
public final void readFully(byte ba[], int off, int len) throws IOException {
|
||||||
* @throws IOException if read fails.
|
dis.readFully(ba, off, len);
|
||||||
* @see java.io.DataInput#readFully(byte[],int,int)
|
}
|
||||||
*/
|
|
||||||
public final void readFully( byte ba[],
|
/**
|
||||||
int off,
|
* Read an int, 32-bits. Like DataInputStream.readInt except little endian.
|
||||||
int len ) throws IOException
|
*
|
||||||
{
|
* @return little-endian binary int from the datastream
|
||||||
dis.readFully( ba, off, len );
|
*
|
||||||
}
|
* @throws IOException
|
||||||
|
* if read fails.
|
||||||
/**
|
*/
|
||||||
* Read an int, 32-bits. Like DataInputStream.readInt except little endian.
|
public final int readInt() throws IOException {
|
||||||
*
|
dis.readFully(work, 0, 4);
|
||||||
* @return little-endian binary int from the datastream
|
return (work[3]) << 24 | (work[2] & 0xff) << 16 | (work[1] & 0xff) << 8 | (work[0] & 0xff);
|
||||||
*
|
}
|
||||||
* @throws IOException if read fails.
|
|
||||||
*/
|
public final int[] readInt(int len) throws IOException {
|
||||||
public final int readInt() throws IOException
|
int[] ret = new int[len];
|
||||||
{
|
|
||||||
dis.readFully( work, 0, 4 );
|
for (int i = 0; i < len; i++)
|
||||||
return ( work[ 3 ] ) << 24
|
ret[i] = readInt();
|
||||||
| ( work[ 2 ] & 0xff ) << 16
|
|
||||||
| ( work[ 1 ] & 0xff ) << 8
|
return ret;
|
||||||
| ( work[ 0 ] & 0xff );
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
public final int [] readInt(int len) throws IOException
|
* Read a line.
|
||||||
{
|
*
|
||||||
int [] ret = new int[len];
|
* @return a rough approximation of the 8-bit stream as a 16-bit unicode string
|
||||||
|
*
|
||||||
for (int i=0; i<len; i++)
|
* @throws IOException
|
||||||
ret[i] = readInt();
|
* @noinspection deprecation
|
||||||
|
* @deprecated This method does not properly convert bytes to characters. Use a Reader instead with a little-endian encoding.
|
||||||
return ret;
|
*/
|
||||||
}
|
public final String readLine() throws IOException {
|
||||||
|
return dis.readLine();
|
||||||
/**
|
}
|
||||||
* Read a line.
|
|
||||||
*
|
/**
|
||||||
* @return a rough approximation of the 8-bit stream as a 16-bit unicode
|
* read a long, 64-bits. Like DataInputStream.readLong except little endian.
|
||||||
* string
|
*
|
||||||
*
|
* @return little-endian binary long from the datastream.
|
||||||
* @throws IOException
|
*
|
||||||
* @noinspection deprecation
|
* @throws IOException
|
||||||
* @deprecated This method does not properly convert bytes to characters.
|
*/
|
||||||
* Use a Reader instead with a little-endian encoding.
|
public final long readLong() throws IOException {
|
||||||
*/
|
dis.readFully(work, 0, 8);
|
||||||
public final String readLine() throws IOException
|
return (long) (work[7]) << 56 |
|
||||||
{
|
/* long cast needed or shift done modulo 32 */
|
||||||
return dis.readLine();
|
(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);
|
||||||
/**
|
}
|
||||||
* read a long, 64-bits. Like DataInputStream.readLong except little
|
|
||||||
* endian.
|
public final long[] readLong(int len) throws IOException {
|
||||||
*
|
long[] ret = new long[len];
|
||||||
* @return little-endian binary long from the datastream.
|
|
||||||
*
|
for (int i = 0; i < len; i++)
|
||||||
* @throws IOException
|
ret[i] = readLong();
|
||||||
*/
|
|
||||||
public final long readLong() throws IOException
|
return ret;
|
||||||
{
|
}
|
||||||
dis.readFully( work, 0, 8 );
|
|
||||||
return (long) ( work[ 7 ] ) << 56
|
/**
|
||||||
|
|
* Read short, 16-bits. Like DataInputStream.readShort except little endian.
|
||||||
/* long cast needed or shift done modulo 32 */
|
*
|
||||||
(long) ( work[ 6 ] & 0xff ) << 48
|
* @return little endian binary short from stream.
|
||||||
| (long) ( work[ 5 ] & 0xff ) << 40
|
*
|
||||||
| (long) ( work[ 4 ] & 0xff ) << 32
|
* @throws IOException
|
||||||
| (long) ( work[ 3 ] & 0xff ) << 24
|
* if read fails.
|
||||||
| (long) ( work[ 2 ] & 0xff ) << 16
|
*/
|
||||||
| (long) ( work[ 1 ] & 0xff ) << 8
|
public final short readShort() throws IOException {
|
||||||
| (long) ( work[ 0 ] & 0xff );
|
dis.readFully(work, 0, 2);
|
||||||
}
|
return (short) ((work[1] & 0xff) << 8 | (work[0] & 0xff));
|
||||||
|
}
|
||||||
public final long [] readLong(int len) throws IOException
|
|
||||||
{
|
public final short[] readShort(int len) throws IOException {
|
||||||
long [] ret = new long[len];
|
short[] ret = new short[len];
|
||||||
|
|
||||||
for (int i=0; i<len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
ret[i] = readLong();
|
ret[i] = readShort();
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read short, 16-bits. Like DataInputStream.readShort except little
|
* Read UTF counted string.
|
||||||
* endian.
|
*
|
||||||
*
|
* @return String read.
|
||||||
* @return little endian binary short from stream.
|
*/
|
||||||
*
|
public final String readUTF() throws IOException {
|
||||||
* @throws IOException if read fails.
|
return dis.readUTF();
|
||||||
*/
|
}
|
||||||
public final short readShort() throws IOException
|
|
||||||
{
|
/**
|
||||||
dis.readFully( work, 0, 2 );
|
* Read an unsigned byte. Note: returns an int, even though says Byte (non-Javadoc)
|
||||||
return (short) ( ( work[ 1 ] & 0xff ) << 8 | ( work[ 0 ] & 0xff ) );
|
*
|
||||||
}
|
* @throws IOException
|
||||||
|
* if read fails.
|
||||||
public final short [] readShort(int len) throws IOException
|
* @see java.io.DataInput#readUnsignedByte()
|
||||||
{
|
*/
|
||||||
short [] ret = new short[len];
|
public final int readUnsignedByte() throws IOException {
|
||||||
|
return dis.readUnsignedByte();
|
||||||
for (int i=0; i<len; i++)
|
}
|
||||||
ret[i] = readShort();
|
|
||||||
|
public final int[] readUnsignedByte(int len) throws IOException {
|
||||||
return ret;
|
int[] ret = new int[len];
|
||||||
}
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
/**
|
ret[i] = readUnsignedByte();
|
||||||
* Read UTF counted string.
|
|
||||||
*
|
return ret;
|
||||||
* @return String read.
|
}
|
||||||
*/
|
|
||||||
public final String readUTF() throws IOException
|
/**
|
||||||
{
|
* Read an unsigned short, 16 bits. Like DataInputStream.readUnsignedShort except little endian. Note, returns int even though
|
||||||
return dis.readUTF();
|
* it reads a short.
|
||||||
}
|
*
|
||||||
|
* @return little-endian int from the stream.
|
||||||
/**
|
*
|
||||||
* Read an unsigned byte. Note: returns an int, even though says Byte
|
* @throws IOException
|
||||||
* (non-Javadoc)
|
* if read fails.
|
||||||
*
|
*/
|
||||||
* @throws IOException if read fails.
|
public final int readUnsignedShort() throws IOException {
|
||||||
* @see java.io.DataInput#readUnsignedByte()
|
dis.readFully(work, 0, 2);
|
||||||
*/
|
return ((work[1] & 0xff) << 8 | (work[0] & 0xff));
|
||||||
public final int readUnsignedByte() throws IOException
|
}
|
||||||
{
|
|
||||||
return dis.readUnsignedByte();
|
public final int[] readUnsignedShort(int len) throws IOException {
|
||||||
}
|
int[] ret = new int[len];
|
||||||
|
|
||||||
public final int [] readUnsignedByte(int len) throws IOException
|
for (int i = 0; i < len; i++)
|
||||||
{
|
ret[i] = readUnsignedShort();
|
||||||
int [] ret = new int[len];
|
|
||||||
|
return ret;
|
||||||
for (int i=0; i<len; i++)
|
}
|
||||||
ret[i] = readUnsignedByte();
|
|
||||||
|
/**
|
||||||
return ret;
|
* Skip over bytes in the stream. See the general contract of the <code>skipBytes</code> method of <code>DataInput</code>.
|
||||||
}
|
* <p/>
|
||||||
|
* Bytes for this operation are read from the contained input stream.
|
||||||
/**
|
*
|
||||||
* Read an unsigned short, 16 bits. Like DataInputStream.readUnsignedShort
|
* @param n
|
||||||
* except little endian. Note, returns int even though it reads a short.
|
* the number of bytes to be skipped.
|
||||||
*
|
*
|
||||||
* @return little-endian int from the stream.
|
* @return the actual number of bytes skipped.
|
||||||
*
|
*
|
||||||
* @throws IOException if read fails.
|
* @throws IOException
|
||||||
*/
|
* if an I/O error occurs.
|
||||||
public final int readUnsignedShort() throws IOException
|
*/
|
||||||
{
|
public final int skipBytes(int n) throws IOException {
|
||||||
dis.readFully( work, 0, 2 );
|
return dis.skipBytes(n);
|
||||||
return ( ( work[ 1 ] & 0xff ) << 8 | ( work[ 0 ] & 0xff ) );
|
}
|
||||||
}
|
}// end class LEDataInputStream
|
||||||
|
|
||||||
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
|
|
||||||
* <code>skipBytes</code> method of <code>DataInput</code>.
|
|
||||||
* <p/>
|
|
||||||
* 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
|
|
||||||
|
|
||||||
|
|
|
@ -17,422 +17,412 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.io;
|
package marytts.util.io;
|
||||||
|
|
||||||
import java.io.DataOutput;
|
import java.io.DataOutput;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* LEDataOutputStream.java
|
* LEDataOutputStream.java
|
||||||
* <p/>
|
* <p/>
|
||||||
* copyright (c) 1998-2007 Roedy Green,
|
* copyright (c) 1998-2007 Roedy Green,
|
||||||
* Canadian Mind Products
|
* Canadian Mind Products
|
||||||
* #101 - 2536 Wark Street
|
* #101 - 2536 Wark Street
|
||||||
* Victoria, BC Canada V8T 4G8
|
* Victoria, BC Canada V8T 4G8
|
||||||
* hel: (250) 361-9093
|
* hel: (250) 361-9093
|
||||||
* mailto:roedyg@mindprod.com
|
* mailto:roedyg@mindprod.com
|
||||||
* http://mindprod.com
|
* http://mindprod.com
|
||||||
* <p/>
|
* <p/>
|
||||||
* Version 1.0 1998 January 6
|
* Version 1.0 1998 January 6
|
||||||
* <p/>
|
* <p/>
|
||||||
* 1.1 1998 January 7 -officially implements DataInput
|
* 1.1 1998 January 7 -officially implements DataInput
|
||||||
* <p/>
|
* <p/>
|
||||||
* 1.2 1998 January 9 - add LERandomAccessFile
|
* 1.2 1998 January 9 - add LERandomAccessFile
|
||||||
* <p/>
|
* <p/>
|
||||||
* 1.3 1998 August 28 1.4 1998 November 10 - add new address and phone.
|
* 1.3 1998 August 28 1.4 1998 November 10 - add new address and phone.
|
||||||
* <p/>
|
* <p/>
|
||||||
* 1.5 1999 October 8 - use com.mindprod.ledatastream
|
* 1.5 1999 October 8 - use com.mindprod.ledatastream
|
||||||
* package name. Very similar to DataOutputStream except it writes
|
* package name. Very similar to DataOutputStream except it writes
|
||||||
* little-endian
|
* little-endian
|
||||||
* instead of big-endian binary data. We can't extend DataOutputStream
|
* instead of big-endian binary data. We can't extend DataOutputStream
|
||||||
* directly
|
* directly
|
||||||
* since it has only final methods. This forces us implement
|
* since it has only final methods. This forces us implement
|
||||||
* LEDataOutputStream
|
* LEDataOutputStream
|
||||||
* with a DataOutputStream object, and use wrapper methods.
|
* with a DataOutputStream object, and use wrapper methods.
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @noinspection WeakerAccess
|
* @noinspection WeakerAccess
|
||||||
*/
|
*/
|
||||||
public class LEDataOutputStream implements DataOutput {
|
public class LEDataOutputStream implements DataOutput {
|
||||||
|
|
||||||
// ------------------------------ FIELDS ------------------------------
|
// ------------------------------ FIELDS ------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* undisplayed copyright notice.
|
* undisplayed copyright notice.
|
||||||
*
|
*
|
||||||
* @noinspection UnusedDeclaration
|
* @noinspection UnusedDeclaration
|
||||||
*/
|
*/
|
||||||
private static final String EMBEDDEDCOPYRIGHT =
|
private static final String EMBEDDEDCOPYRIGHT = "copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com";
|
||||||
"copyright (c) 1999-2007 Roedy Green, Canadian Mind Products, http://mindprod.com";
|
|
||||||
|
/**
|
||||||
/**
|
* to get at big-Endian write methods of DataOutPutStream.
|
||||||
* to get at big-Endian write methods of DataOutPutStream.
|
*
|
||||||
*
|
* @noinspection WeakerAccess
|
||||||
* @noinspection WeakerAccess
|
*/
|
||||||
*/
|
protected final DataOutputStream dis;
|
||||||
protected final DataOutputStream dis;
|
|
||||||
|
/**
|
||||||
/**
|
* work array for composing output.
|
||||||
* work array for composing output.
|
*
|
||||||
*
|
* @noinspection WeakerAccess
|
||||||
* @noinspection WeakerAccess
|
*/
|
||||||
*/
|
protected final byte[] work;
|
||||||
protected final byte[] work;
|
|
||||||
|
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
|
||||||
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
|
/**
|
||||||
/**
|
* constructor.
|
||||||
* constructor.
|
*
|
||||||
*
|
* @param out
|
||||||
* @param out the outputstream we write little endian binary data onto.
|
* the outputstream we write little endian binary data onto.
|
||||||
*/
|
*/
|
||||||
public LEDataOutputStream( OutputStream out )
|
public LEDataOutputStream(OutputStream out) {
|
||||||
{
|
this.dis = new DataOutputStream(out);
|
||||||
this.dis = new DataOutputStream( out );
|
work = new byte[8];// work array for composing output
|
||||||
work = new byte[8];// work array for composing output
|
}
|
||||||
}
|
|
||||||
|
public LEDataOutputStream(String filename) throws FileNotFoundException {
|
||||||
public LEDataOutputStream( String filename) throws FileNotFoundException
|
this(new FileOutputStream(filename));
|
||||||
{
|
}
|
||||||
this(new FileOutputStream(filename));
|
|
||||||
}
|
/**
|
||||||
|
* Close stream.
|
||||||
/**
|
*
|
||||||
* Close stream.
|
* @throws IOException
|
||||||
*
|
* if close fails.
|
||||||
* @throws IOException if close fails.
|
*/
|
||||||
*/
|
public final void close() throws IOException {
|
||||||
public final void close() throws IOException
|
dis.close();
|
||||||
{
|
}
|
||||||
dis.close();
|
|
||||||
}
|
/**
|
||||||
|
* Flush stream without closing.
|
||||||
/**
|
*
|
||||||
* Flush stream without closing.
|
* @throws IOException
|
||||||
*
|
* if flush fails.
|
||||||
* @throws IOException if flush fails.
|
*/
|
||||||
*/
|
public void flush() throws IOException {
|
||||||
public void flush() throws IOException
|
dis.flush();
|
||||||
{
|
}
|
||||||
dis.flush();
|
|
||||||
}
|
/**
|
||||||
|
* Get size of stream.
|
||||||
/**
|
*
|
||||||
* 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.
|
||||||
* @return bytes written so far in the stream. Note this is a int, not a
|
*/
|
||||||
* long as you would exect. This because the underlying
|
public final int size() {
|
||||||
* DataInputStream has a design flaw.
|
return dis.size();
|
||||||
*/
|
}
|
||||||
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.
|
||||||
* This method writes only one byte, even though it says int (non-Javadoc)
|
*
|
||||||
*
|
* @throws IOException
|
||||||
* @param ib the byte to write.
|
* if write fails.
|
||||||
*
|
* @see java.io.DataOutput#write(int)
|
||||||
* @throws IOException if write fails.
|
*/
|
||||||
* @see java.io.DataOutput#write(int)
|
public final synchronized void write(int ib) throws IOException {
|
||||||
*/
|
dis.write(ib);
|
||||||
public final synchronized void write( int ib ) throws IOException
|
}
|
||||||
{
|
|
||||||
dis.write( ib );
|
/**
|
||||||
}
|
* Write out an array of bytes.
|
||||||
|
*
|
||||||
/**
|
* @throws IOException
|
||||||
* Write out an array of bytes.
|
* if write fails.
|
||||||
*
|
* @see java.io.DataOutput#write(byte[])
|
||||||
* @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);
|
||||||
public final void write( byte ba[] ) throws IOException
|
}
|
||||||
{
|
|
||||||
dis.write( ba, 0, ba.length );
|
/**
|
||||||
}
|
* Writes out part of an array of bytes.
|
||||||
|
*
|
||||||
/**
|
* @throws IOException
|
||||||
* Writes out part of an array of bytes.
|
* if write fails.
|
||||||
*
|
* @see java.io.DataOutput#write(byte[],int,int)
|
||||||
* @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);
|
||||||
public final synchronized void write( byte ba[],
|
}
|
||||||
int off,
|
|
||||||
int len ) throws IOException
|
/**
|
||||||
{
|
* Write a booleans as one byte.
|
||||||
dis.write( ba, off, len );
|
*
|
||||||
}
|
* @param v
|
||||||
|
* boolean to write.
|
||||||
/**
|
*
|
||||||
* Write a booleans as one byte.
|
* @throws IOException
|
||||||
*
|
* if write fails.
|
||||||
* @param v boolean to write.
|
* @see java.io.DataOutput#writeBoolean(boolean)
|
||||||
*
|
*/
|
||||||
* @throws IOException if write fails.
|
/* Only writes one byte */
|
||||||
* @see java.io.DataOutput#writeBoolean(boolean)
|
public final void writeBoolean(boolean v) throws IOException {
|
||||||
*/
|
dis.writeBoolean(v);
|
||||||
/* Only writes one byte */
|
}
|
||||||
public final void writeBoolean( boolean v ) throws IOException
|
|
||||||
{
|
public final void writeBoolean( boolean [] v, int startPos, int len) throws IOException
|
||||||
dis.writeBoolean( v );
|
{
|
||||||
}
|
assert v.length<startPos+len;
|
||||||
|
|
||||||
public final void writeBoolean( boolean [] v, int startPos, int len) throws IOException
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
{
|
writeBoolean(v[i]);
|
||||||
assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
public final void writeBoolean(boolean[] v) throws IOException {
|
||||||
writeBoolean(v[i]);
|
writeBoolean(v, 0, v.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeBoolean( boolean [] v) throws IOException
|
/**
|
||||||
{
|
* write a byte.
|
||||||
writeBoolean(v, 0, v.length);
|
*
|
||||||
}
|
* @param v
|
||||||
|
* the byte to write.
|
||||||
/**
|
*
|
||||||
* write a byte.
|
* @throws IOException
|
||||||
*
|
* if write fails.
|
||||||
* @param v the byte to write.
|
* @see java.io.DataOutput#writeByte(int)
|
||||||
*
|
*/
|
||||||
* @throws IOException if write fails.
|
public final void writeByte(int v) throws IOException {
|
||||||
* @see java.io.DataOutput#writeByte(int)
|
dis.writeByte(v);
|
||||||
*/
|
}
|
||||||
public final void writeByte( int v ) throws IOException
|
|
||||||
{
|
public final void writeByte( byte [] v, int startPos, int len) throws IOException
|
||||||
dis.writeByte( v );
|
{
|
||||||
}
|
assert v.length<startPos+len;
|
||||||
|
|
||||||
public final void writeByte( byte [] v, int startPos, int len) throws IOException
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
{
|
writeByte(v[i]);
|
||||||
assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
public final void writeByte(byte[] v) throws IOException {
|
||||||
writeByte(v[i]);
|
writeByte(v, 0, v.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeByte( byte [] v) throws IOException
|
/**
|
||||||
{
|
* Write a string.
|
||||||
writeByte(v, 0, v.length);
|
*
|
||||||
}
|
* @param s
|
||||||
|
* the string to write.
|
||||||
/**
|
*
|
||||||
* Write a string.
|
* @throws IOException
|
||||||
*
|
* if write fails.
|
||||||
* @param s the string to write.
|
* @see java.io.DataOutput#writeBytes(java.lang.String)
|
||||||
*
|
*/
|
||||||
* @throws IOException if write fails.
|
public final void writeBytes(String s) throws IOException {
|
||||||
* @see java.io.DataOutput#writeBytes(java.lang.String)
|
dis.writeBytes(s);
|
||||||
*/
|
}
|
||||||
public final void writeBytes( String s ) throws IOException
|
|
||||||
{
|
/**
|
||||||
dis.writeBytes( s );
|
* Write a char. Like DataOutputStream.writeChar. Note the parm is an int even though this as a writeChar
|
||||||
}
|
*
|
||||||
|
* @param v
|
||||||
/**
|
* the char to write
|
||||||
* Write a char. Like DataOutputStream.writeChar. Note the parm is an int
|
*
|
||||||
* even though this as a writeChar
|
* @throws IOException
|
||||||
*
|
* if write fails.
|
||||||
* @param v the char to write
|
*/
|
||||||
*
|
public final void writeChar(int v) throws IOException {
|
||||||
* @throws IOException if write fails.
|
// same code as writeShort
|
||||||
*/
|
work[0] = (byte) v;
|
||||||
public final void writeChar( int v ) throws IOException
|
work[1] = (byte) (v >> 8);
|
||||||
{
|
dis.write(work, 0, 2);
|
||||||
// same code as writeShort
|
}
|
||||||
work[ 0 ] = (byte) v;
|
|
||||||
work[ 1 ] = (byte) ( v >> 8 );
|
public final void writeChar( char [] v, int startPos, int len) throws IOException
|
||||||
dis.write( work, 0, 2 );
|
{
|
||||||
}
|
assert v.length<startPos+len;
|
||||||
|
|
||||||
public final void writeChar( char [] v, int startPos, int len) throws IOException
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
{
|
writeChar(v[i]);
|
||||||
assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
public final void writeChar(char[] v) throws IOException {
|
||||||
writeChar(v[i]);
|
writeChar(v, 0, v.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeChar( char [] v) throws IOException
|
/**
|
||||||
{
|
* Write a string, not a char[]. Like DataOutputStream.writeChars, flip endianness of each char.
|
||||||
writeChar(v, 0, v.length);
|
*
|
||||||
}
|
* @throws IOException
|
||||||
|
* if write fails.
|
||||||
/**
|
*/
|
||||||
* Write a string, not a char[]. Like DataOutputStream.writeChars, flip
|
public final void writeChars(String s) throws IOException {
|
||||||
* endianness of each char.
|
int len = s.length();
|
||||||
*
|
for (int i = 0; i < len; i++) {
|
||||||
* @throws IOException if write fails.
|
writeChar(s.charAt(i));
|
||||||
*/
|
}
|
||||||
public final void writeChars( String s ) throws IOException
|
}// end writeChars
|
||||||
{
|
|
||||||
int len = s.length();
|
/**
|
||||||
for ( int i = 0; i < len; i++ )
|
* Write a double.
|
||||||
{
|
*
|
||||||
writeChar( s.charAt( i ) );
|
* @param v
|
||||||
}
|
* the double to write. Like DataOutputStream.writeDouble.
|
||||||
}// end writeChars
|
*
|
||||||
|
* @throws IOException
|
||||||
/**
|
* if write fails.
|
||||||
* Write a double.
|
*/
|
||||||
*
|
public final void writeDouble(double v) throws IOException {
|
||||||
* @param v the double to write. Like DataOutputStream.writeDouble.
|
writeLong(Double.doubleToLongBits(v));
|
||||||
*
|
}
|
||||||
* @throws IOException if write fails.
|
|
||||||
*/
|
public final void writeDouble(double[] v, int startPos, int len) throws IOException {
|
||||||
public final void writeDouble( double v ) throws IOException
|
for (int i = startPos; i < startPos + len; i++)
|
||||||
{
|
writeDouble(v[i]);
|
||||||
writeLong( Double.doubleToLongBits( v ) );
|
}
|
||||||
}
|
|
||||||
|
public final void writeDouble(double[] v) throws IOException {
|
||||||
public final void writeDouble( double [] v, int startPos, int len) throws IOException
|
writeDouble(v, 0, v.length);
|
||||||
{
|
}
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
|
||||||
writeDouble(v[i]);
|
/**
|
||||||
}
|
* Write a float. Like DataOutputStream.writeFloat.
|
||||||
|
*
|
||||||
public final void writeDouble( double [] v) throws IOException
|
* @param v
|
||||||
{
|
* the float to write.
|
||||||
writeDouble(v, 0, v.length);
|
*
|
||||||
}
|
* @throws IOException
|
||||||
|
* if write fails.
|
||||||
/**
|
*/
|
||||||
* Write a float. Like DataOutputStream.writeFloat.
|
public final void writeFloat(float v) throws IOException {
|
||||||
*
|
writeInt(Float.floatToIntBits(v));
|
||||||
* @param v the float to write.
|
}
|
||||||
*
|
|
||||||
* @throws IOException if write fails.
|
public final void writeFloat(float[] v, int startPos, int len) throws IOException {
|
||||||
*/
|
// this will always fire, since 0 + v.length never be > v.length!
|
||||||
public final void writeFloat( float v ) throws IOException
|
// TODO remove this assert:
|
||||||
{
|
// assert v.length<startPos+len;
|
||||||
writeInt( Float.floatToIntBits( v ) );
|
|
||||||
}
|
for (int i = startPos; i < startPos + len; i++)
|
||||||
|
writeFloat(v[i]);
|
||||||
public final void writeFloat( float [] v, int startPos, int len) throws IOException
|
}
|
||||||
{
|
|
||||||
// this will always fire, since 0 + v.length never be > v.length!
|
public final void writeFloat(float[] v) throws IOException {
|
||||||
// TODO remove this assert:
|
writeFloat(v, 0, v.length);
|
||||||
// assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
/**
|
||||||
writeFloat(v[i]);
|
* Write an int, 32-bits. Like DataOutputStream.writeInt.
|
||||||
}
|
*
|
||||||
|
* @param v
|
||||||
public final void writeFloat( float [] v) throws IOException
|
* the int to write
|
||||||
{
|
*
|
||||||
writeFloat(v, 0, v.length);
|
* @throws IOException
|
||||||
}
|
* if write fails.
|
||||||
|
*/
|
||||||
/**
|
public final void writeInt(int v) throws IOException {
|
||||||
* Write an int, 32-bits. Like DataOutputStream.writeInt.
|
work[0] = (byte) v;
|
||||||
*
|
work[1] = (byte) (v >> 8);
|
||||||
* @param v the int to write
|
work[2] = (byte) (v >> 16);
|
||||||
*
|
work[3] = (byte) (v >> 24);
|
||||||
* @throws IOException if write fails.
|
dis.write(work, 0, 4);
|
||||||
*/
|
}
|
||||||
public final void writeInt( int v ) throws IOException
|
|
||||||
{
|
public final void writeInt( int [] v, int startPos, int len) throws IOException
|
||||||
work[ 0 ] = (byte) v;
|
{
|
||||||
work[ 1 ] = (byte) ( v >> 8 );
|
assert v.length<startPos+len;
|
||||||
work[ 2 ] = (byte) ( v >> 16 );
|
|
||||||
work[ 3 ] = (byte) ( v >> 24 );
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
dis.write( work, 0, 4 );
|
writeInt(v[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeInt( int [] v, int startPos, int len) throws IOException
|
public final void writeInt(int[] v) throws IOException {
|
||||||
{
|
writeInt(v, 0, v.length);
|
||||||
assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
/**
|
||||||
writeInt(v[i]);
|
* Write a long, 64-bits. like DataOutputStream.writeLong.
|
||||||
}
|
*
|
||||||
|
* @param v
|
||||||
public final void writeInt( int [] v) throws IOException
|
* the long to write
|
||||||
{
|
*
|
||||||
writeInt(v, 0, v.length);
|
* @throws IOException
|
||||||
}
|
* if write fails.
|
||||||
|
*/
|
||||||
/**
|
public final void writeLong(long v) throws IOException {
|
||||||
* Write a long, 64-bits. like DataOutputStream.writeLong.
|
work[0] = (byte) v;
|
||||||
*
|
work[1] = (byte) (v >> 8);
|
||||||
* @param v the long to write
|
work[2] = (byte) (v >> 16);
|
||||||
*
|
work[3] = (byte) (v >> 24);
|
||||||
* @throws IOException if write fails.
|
work[4] = (byte) (v >> 32);
|
||||||
*/
|
work[5] = (byte) (v >> 40);
|
||||||
public final void writeLong( long v ) throws IOException
|
work[6] = (byte) (v >> 48);
|
||||||
{
|
work[7] = (byte) (v >> 56);
|
||||||
work[ 0 ] = (byte) v;
|
dis.write(work, 0, 8);
|
||||||
work[ 1 ] = (byte) ( v >> 8 );
|
}
|
||||||
work[ 2 ] = (byte) ( v >> 16 );
|
|
||||||
work[ 3 ] = (byte) ( v >> 24 );
|
public final void writeLong (long [] v, int startPos, int len) throws IOException
|
||||||
work[ 4 ] = (byte) ( v >> 32 );
|
{
|
||||||
work[ 5 ] = (byte) ( v >> 40 );
|
assert v.length<startPos+len;
|
||||||
work[ 6 ] = (byte) ( v >> 48 );
|
|
||||||
work[ 7 ] = (byte) ( v >> 56 );
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
dis.write( work, 0, 8 );
|
writeLong(v[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeLong (long [] v, int startPos, int len) throws IOException
|
public final void writeLong(long[] v) throws IOException {
|
||||||
{
|
writeLong(v, 0, v.length);
|
||||||
assert v.length<startPos+len;
|
}
|
||||||
|
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
/**
|
||||||
writeLong(v[i]);
|
* Write short, 16-bits. Like DataOutputStream.writeShort. also acts as a writeUnsignedShort
|
||||||
}
|
*
|
||||||
|
* @param v
|
||||||
public final void writeLong( long [] v) throws IOException
|
* the short you want written in little endian binary format
|
||||||
{
|
*
|
||||||
writeLong(v, 0, v.length);
|
* @throws IOException
|
||||||
}
|
* if write fails.
|
||||||
|
*/
|
||||||
/**
|
public final void writeShort(int v) throws IOException {
|
||||||
* Write short, 16-bits. Like DataOutputStream.writeShort. also acts as a
|
work[0] = (byte) v;
|
||||||
* writeUnsignedShort
|
work[1] = (byte) (v >> 8);
|
||||||
*
|
dis.write(work, 0, 2);
|
||||||
* @param v the short you want written in little endian binary format
|
}
|
||||||
*
|
|
||||||
* @throws IOException if write fails.
|
public final void writeShort( short [] v, int startPos, int len) throws IOException
|
||||||
*/
|
{
|
||||||
public final void writeShort( int v ) throws IOException
|
assert v.length<startPos+len;
|
||||||
{
|
|
||||||
work[ 0 ] = (byte) v;
|
for (int i=startPos; i<startPos+len; i++)
|
||||||
work[ 1 ] = (byte) ( v >> 8 );
|
writeShort(v[i]);
|
||||||
dis.write( work, 0, 2 );
|
}
|
||||||
}
|
|
||||||
|
public final void writeShort(short[] v) throws IOException {
|
||||||
public final void writeShort( short [] v, int startPos, int len) throws IOException
|
writeShort(v, 0, v.length);
|
||||||
{
|
}
|
||||||
assert v.length<startPos+len;
|
|
||||||
|
/**
|
||||||
for (int i=startPos; i<startPos+len; i++)
|
* Write a string as a UTF counted string.
|
||||||
writeShort(v[i]);
|
*
|
||||||
}
|
* @param s
|
||||||
|
* the string to write.
|
||||||
public final void writeShort( short [] v) throws IOException
|
*
|
||||||
{
|
* @throws IOException
|
||||||
writeShort(v, 0, v.length);
|
* if write fails.
|
||||||
}
|
* @see java.io.DataOutput#writeUTF(java.lang.String)
|
||||||
|
*/
|
||||||
/**
|
public final void writeUTF(String s) throws IOException {
|
||||||
* Write a string as a UTF counted string.
|
dis.writeUTF(s);
|
||||||
*
|
}
|
||||||
* @param s the string to write.
|
|
||||||
*
|
}// end LEDataOutputStream
|
||||||
* @throws IOException if write fails.
|
|
||||||
* @see java.io.DataOutput#writeUTF(java.lang.String)
|
|
||||||
*/
|
|
||||||
public final void writeUTF( String s ) throws IOException
|
|
||||||
{
|
|
||||||
dis.writeUTF( s );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}// end LEDataOutputStream
|
|
||||||
|
|
||||||
|
|
|
@ -17,105 +17,89 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.io;
|
package marytts.util.io;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -25,52 +25,45 @@ import java.io.Reader;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
public class LoggingReader extends FilterReader
|
public class LoggingReader extends FilterReader {
|
||||||
{
|
protected Logger logger;
|
||||||
protected Logger logger;
|
protected StringBuffer logText;
|
||||||
protected StringBuffer logText;
|
|
||||||
|
|
||||||
public LoggingReader(Reader in, Logger logger)
|
public LoggingReader(Reader in, Logger logger) {
|
||||||
{
|
super(in);
|
||||||
super(in);
|
this.logger = logger;
|
||||||
this.logger = logger;
|
logText = new StringBuffer();
|
||||||
logText = new StringBuffer();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public int read() throws IOException
|
public int read() throws IOException {
|
||||||
{
|
int c = super.read();
|
||||||
int c = super.read();
|
if (c == -1) {
|
||||||
if (c == -1) {
|
logRead();
|
||||||
logRead();
|
} else {
|
||||||
} else {
|
logText.append((char) c);
|
||||||
logText.append((char)c);
|
}
|
||||||
}
|
return c;
|
||||||
return c;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public int read(char[] cbuf, int off, int len) throws IOException
|
public int read(char[] cbuf, int off, int len) throws IOException {
|
||||||
{
|
int nr = super.read(cbuf, off, len);
|
||||||
int nr = super.read(cbuf, off, len);
|
if (nr == -1) {
|
||||||
if (nr == -1) {
|
logRead();
|
||||||
logRead();
|
} else {
|
||||||
} else {
|
logText.append(new String(cbuf, off, nr));
|
||||||
logText.append(new String(cbuf, off, nr));
|
}
|
||||||
}
|
return nr;
|
||||||
return nr;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void close() throws IOException
|
public void close() throws IOException {
|
||||||
{
|
super.close();
|
||||||
super.close();
|
logRead();
|
||||||
logRead();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void logRead()
|
public void logRead() {
|
||||||
{
|
if (logText.length() > 0) {
|
||||||
if (logText.length() > 0) {
|
logger.info("Read:\n" + logText.toString());
|
||||||
logger.info("Read:\n" + logText.toString());
|
logText.setLength(0);
|
||||||
logText.setLength(0);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -7,27 +7,26 @@ import java.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* extends properties class to allow trimming of trailing whitespace from input streams
|
* extends properties class to allow trimming of trailing whitespace from input streams
|
||||||
*
|
*
|
||||||
* @author Tristan
|
* @author Tristan
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class PropertiesTrimTrailingWhitespace extends Properties {
|
public class PropertiesTrimTrailingWhitespace extends Properties {
|
||||||
/**
|
/**
|
||||||
* removes trailing whitespace
|
* removes trailing whitespace
|
||||||
*/
|
*/
|
||||||
public void load(InputStream fis) throws IOException {
|
public void load(InputStream fis) throws IOException {
|
||||||
Scanner in = new Scanner(fis);
|
Scanner in = new Scanner(fis);
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
while(in.hasNext()) {
|
while (in.hasNext()) {
|
||||||
out.write(in.nextLine().trim().getBytes());
|
out.write(in.nextLine().trim().getBytes());
|
||||||
out.write("\n".getBytes());
|
out.write("\n".getBytes());
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
InputStream is = new ByteArrayInputStream(out.toByteArray());
|
InputStream is = new ByteArrayInputStream(out.toByteArray());
|
||||||
super.load(is);
|
super.load(is);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,47 +26,40 @@ import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class splitting a Reader into chunks.
|
* A class splitting a Reader into chunks. In a continuous input Reader, search for lines containing a specific "end-of-chunk"
|
||||||
* In a continuous input Reader, search for lines containing
|
* marking (e.g., an XML root end tag), and return individual readers, each of which will provide one chunk (including the line
|
||||||
* a specific "end-of-chunk" marking (e.g., an XML root end tag),
|
* containing the end-of-chunk marking).
|
||||||
* and return individual readers, each of which will provide
|
*
|
||||||
* one chunk (including the line containing the end-of-chunk marking).
|
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ReaderSplitter
|
public class ReaderSplitter {
|
||||||
{
|
private BufferedReader in;
|
||||||
private BufferedReader in;
|
private StringBuffer buf;
|
||||||
private StringBuffer buf;
|
private String endMarker;
|
||||||
private String endMarker;
|
|
||||||
|
|
||||||
public ReaderSplitter(Reader in, String endMarker)
|
public ReaderSplitter(Reader in, String endMarker) {
|
||||||
{
|
this.in = new BufferedReader(in);
|
||||||
this.in = new BufferedReader(in);
|
this.endMarker = endMarker;
|
||||||
this.endMarker = endMarker;
|
buf = new StringBuffer(1000);
|
||||||
buf = new StringBuffer(1000);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a reader from which one chunk can be read, followed by EOF.
|
* Return a reader from which one chunk can be read, followed by EOF. Chunks are delimited by start of file, lines containing
|
||||||
* Chunks are delimited by start of file, lines containing the end marker
|
* the end marker string (line is last line in chunk), and end of file. Returns null if nothing more can be read.
|
||||||
* string (line is last line in chunk), and end of file.
|
*/
|
||||||
* Returns null if nothing more can be read.
|
public Reader nextReader() throws IOException {
|
||||||
*/
|
String line = null;
|
||||||
public Reader nextReader()
|
buf.setLength(0); // start with an empty buffer
|
||||||
throws IOException
|
while ((line = in.readLine()) != null) {
|
||||||
{
|
buf.append(line);
|
||||||
String line = null;
|
buf.append(System.getProperty("line.separator"));
|
||||||
buf.setLength(0); // start with an empty buffer
|
if (line.indexOf(endMarker) != -1) { // found end marker in line
|
||||||
while ((line = in.readLine()) != null) {
|
break;
|
||||||
buf.append(line);
|
}
|
||||||
buf.append(System.getProperty("line.separator"));
|
}
|
||||||
if (line.indexOf(endMarker) != -1) { // found end marker in line
|
if (buf.length() == 0)
|
||||||
break;
|
return null; // nothing more to read.
|
||||||
}
|
return (Reader) new StringReader(buf.toString());
|
||||||
}
|
}
|
||||||
if (buf.length() == 0) return null; // nothing more to read.
|
|
||||||
return (Reader) new StringReader(buf.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,34 +25,36 @@ import javax.swing.filechooser.FileFilter;
|
||||||
|
|
||||||
import marytts.util.MaryUtils;
|
import marytts.util.MaryUtils;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple file filter accepting files with a given extension.
|
* A simple file filter accepting files with a given extension.
|
||||||
|
*
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
public class SimpleFileFilter extends FileFilter
|
public class SimpleFileFilter extends FileFilter {
|
||||||
{
|
String extension;
|
||||||
String extension;
|
String description;
|
||||||
String description;
|
|
||||||
public SimpleFileFilter(String extension, String description)
|
|
||||||
{
|
|
||||||
this.extension = extension;
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean accept(File f) {
|
public SimpleFileFilter(String extension, String description) {
|
||||||
if (f.isDirectory()) {
|
this.extension = extension;
|
||||||
return true;
|
this.description = description;
|
||||||
}
|
}
|
||||||
String ext = MaryUtils.getExtension(f);
|
|
||||||
if (ext != null) {
|
|
||||||
return ext.equals(extension);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() { return description; }
|
public boolean accept(File f) {
|
||||||
|
if (f.isDirectory()) {
|
||||||
public String getExtension() { return extension; }
|
return true;
|
||||||
|
}
|
||||||
|
String ext = MaryUtils.getExtension(f);
|
||||||
|
if (ext != null) {
|
||||||
|
return ext.equals(extension);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExtension() {
|
||||||
|
return extension;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,29 +8,24 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
public class StreamGobbler extends Thread
|
public class StreamGobbler extends Thread {
|
||||||
{
|
InputStream is;
|
||||||
InputStream is;
|
String type;
|
||||||
String type;
|
|
||||||
|
public StreamGobbler(InputStream is, String type) {
|
||||||
public StreamGobbler(InputStream is, String type)
|
this.is = is;
|
||||||
{
|
this.type = type;
|
||||||
this.is = is;
|
}
|
||||||
this.type = type;
|
|
||||||
}
|
public void run() {
|
||||||
|
try {
|
||||||
public void run()
|
InputStreamReader isr = new InputStreamReader(is);
|
||||||
{
|
BufferedReader br = new BufferedReader(isr);
|
||||||
try
|
String line = null;
|
||||||
{
|
while ((line = br.readLine()) != null)
|
||||||
InputStreamReader isr = new InputStreamReader(is);
|
System.out.println(type + ">" + line);
|
||||||
BufferedReader br = new BufferedReader(isr);
|
} catch (IOException ioe) {
|
||||||
String line=null;
|
ioe.printStackTrace();
|
||||||
while ( (line = br.readLine()) != null)
|
}
|
||||||
System.out.println(type + ">" + line);
|
}
|
||||||
} catch (IOException ioe)
|
|
||||||
{
|
|
||||||
ioe.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -31,73 +31,64 @@ import marytts.util.MaryUtils;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from a stream and log.
|
* Read from a stream and log.
|
||||||
|
*
|
||||||
* @author Marc Schröder
|
* @author Marc Schröder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class StreamLogger extends Thread
|
public class StreamLogger extends Thread {
|
||||||
{
|
private InputStream is;
|
||||||
private InputStream is;
|
private PrintStream ps;
|
||||||
private PrintStream ps;
|
private Logger logger;
|
||||||
private Logger logger;
|
private Pattern ignorePattern = null;
|
||||||
private Pattern ignorePattern = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from an input stream, logging to category <code>logCategory</code>,
|
* Read from an input stream, logging to category <code>logCategory</code>, ignoring lines matching the regular expression
|
||||||
* ignoring lines matching
|
* specified in <code>ignorePattern</code>. If <code>logCategory</code> is <code>null</code>, "unnamed" will be used. If
|
||||||
* the regular expression specified in <code>ignorePattern</code>.
|
* <code>ignorePattern</code> is <code>null</code>, no filtering will be performed. The thread will silently die when it
|
||||||
* If <code>logCategory</code> is <code>null</code>, "unnamed" will be used.
|
* reaches end-of-file from the input stream.
|
||||||
* If <code>ignorePattern</code> is <code>null</code>, no filtering will be
|
*/
|
||||||
* performed.
|
public StreamLogger(InputStream is, String logCategory, String ignorePattern) {
|
||||||
* The thread will silently die when it reaches end-of-file from the input
|
this.is = is;
|
||||||
* stream.
|
if (logCategory == null)
|
||||||
*/
|
logger = MaryUtils.getLogger("unnamed");
|
||||||
public StreamLogger(InputStream is, String logCategory, String ignorePattern)
|
else
|
||||||
{
|
logger = MaryUtils.getLogger(logCategory);
|
||||||
this.is = is;
|
if (ignorePattern != null) {
|
||||||
if (logCategory == null)
|
try {
|
||||||
logger = MaryUtils.getLogger("unnamed");
|
this.ignorePattern = Pattern.compile(ignorePattern);
|
||||||
else
|
} catch (PatternSyntaxException e) {
|
||||||
logger = MaryUtils.getLogger(logCategory);
|
logger.warn("Problem with regular expression pattern", e);
|
||||||
if (ignorePattern != null) {
|
this.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) {
|
public StreamLogger(InputStream is, PrintStream ps) {
|
||||||
this.is = is;
|
this.is = is;
|
||||||
this.ps = ps;
|
this.ps = ps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run()
|
public void run() {
|
||||||
{
|
String line = null;
|
||||||
String line = null;
|
try {
|
||||||
try {
|
BufferedReader b = new BufferedReader(new InputStreamReader(is));
|
||||||
BufferedReader b = new BufferedReader(new InputStreamReader(is));
|
while ((line = b.readLine()) != null) {
|
||||||
while ((line = b.readLine()) != null) {
|
if (ignorePattern != null && ignorePattern.matcher(line).matches())
|
||||||
if (ignorePattern != null && ignorePattern.matcher(line).matches())
|
continue; // do not log
|
||||||
continue; // do not log
|
if (ps != null) {
|
||||||
if (ps != null) {
|
ps.println(line);
|
||||||
ps.println(line);
|
} else {
|
||||||
} else {
|
logger.info(line);
|
||||||
logger.info(line);
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
try {
|
||||||
try {
|
logger.warn("Cannot read from stream", e);
|
||||||
logger.warn("Cannot read from stream", e);
|
} catch (NullPointerException npe) {
|
||||||
} catch (NullPointerException npe) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,24 +34,22 @@ import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author marc
|
* @author marc
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class StreamUtils {
|
public class StreamUtils {
|
||||||
|
|
||||||
public static double[] readDoubleArray(DataInput stream, int len)
|
public static double[] readDoubleArray(DataInput stream, int len) throws IOException {
|
||||||
throws IOException {
|
byte[] raw = new byte[len * Double.SIZE / 8];
|
||||||
byte[] raw = new byte[len*Double.SIZE/8];
|
stream.readFully(raw);
|
||||||
stream.readFully(raw);
|
DataInputStream in = new DataInputStream(new ByteArrayInputStream(raw));
|
||||||
DataInputStream in = new DataInputStream(new ByteArrayInputStream(raw));
|
double[] data = new double[len];
|
||||||
double[] data = new double[len];
|
for (int i = 0; i < len; i++) {
|
||||||
for (int i=0; i<len; i++) {
|
data[i] = in.readDouble();
|
||||||
data[i] = in.readDouble();
|
}
|
||||||
}
|
return data;
|
||||||
return data;
|
}
|
||||||
}
|
|
||||||
|
public static void writeDoubleArray(DataOutput stream, double[] data)
|
||||||
|
|
||||||
public static void writeDoubleArray(DataOutput stream, double[] data)
|
|
||||||
throws IOException {
|
throws IOException {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
DataOutputStream out = new DataOutputStream(baos);
|
DataOutputStream out = new DataOutputStream(baos);
|
||||||
|
@ -63,108 +61,103 @@ public class StreamUtils {
|
||||||
assert raw.length == data.length * Double.SIZE / 8;
|
assert raw.length == data.length * Double.SIZE / 8;
|
||||||
stream.write(raw);
|
stream.write(raw);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads from the
|
|
||||||
* bytebuffer <code>bb</code> a representation
|
|
||||||
* of a Unicode character string encoded in
|
|
||||||
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
|
|
||||||
* this string of characters is then returned as a <code>String</code>.
|
|
||||||
* The details of the modified UTF-8 representation
|
|
||||||
* are exactly the same as for the <code>readUTF</code>
|
|
||||||
* method of <code>DataInput</code>.
|
|
||||||
*
|
|
||||||
* @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;
|
* Reads from the bytebuffer <code>bb</code> a representation of a Unicode character string encoded in <a
|
||||||
int chararr_count=0;
|
* href="DataInput.html#modified-utf-8">modified UTF-8</a> format; this string of characters is then returned as a
|
||||||
|
* <code>String</code>. The details of the modified UTF-8 representation are exactly the same as for the <code>readUTF</code>
|
||||||
|
* method of <code>DataInput</code>.
|
||||||
|
*
|
||||||
|
* @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) {
|
bb.get(bytearr);
|
||||||
c = (int) bytearr[count] & 0xff;
|
|
||||||
if (c > 127) break;
|
|
||||||
count++;
|
|
||||||
chararr[chararr_count++]=(char)c;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (count < utflen) {
|
while (count < utflen) {
|
||||||
c = (int) bytearr[count] & 0xff;
|
c = (int) bytearr[count] & 0xff;
|
||||||
switch (c >> 4) {
|
if (c > 127)
|
||||||
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
break;
|
||||||
/* 0xxxxxxx*/
|
count++;
|
||||||
count++;
|
chararr[chararr_count++] = (char) c;
|
||||||
chararr[chararr_count++]=(char)c;
|
}
|
||||||
break;
|
|
||||||
case 12: case 13:
|
while (count < utflen) {
|
||||||
/* 110x xxxx 10xx xxxx*/
|
c = (int) bytearr[count] & 0xff;
|
||||||
count += 2;
|
switch (c >> 4) {
|
||||||
if (count > utflen)
|
case 0:
|
||||||
throw new UTFDataFormatException(
|
case 1:
|
||||||
"malformed input: partial character at end");
|
case 2:
|
||||||
char2 = (int) bytearr[count-1];
|
case 3:
|
||||||
if ((char2 & 0xC0) != 0x80)
|
case 4:
|
||||||
throw new UTFDataFormatException(
|
case 5:
|
||||||
"malformed input around byte " + count);
|
case 6:
|
||||||
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
|
case 7:
|
||||||
(char2 & 0x3F));
|
/* 0xxxxxxx */
|
||||||
break;
|
count++;
|
||||||
case 14:
|
chararr[chararr_count++] = (char) c;
|
||||||
/* 1110 xxxx 10xx xxxx 10xx xxxx */
|
break;
|
||||||
count += 3;
|
case 12:
|
||||||
if (count > utflen)
|
case 13:
|
||||||
throw new UTFDataFormatException(
|
/* 110x xxxx 10xx xxxx */
|
||||||
"malformed input: partial character at end");
|
count += 2;
|
||||||
char2 = (int) bytearr[count-2];
|
if (count > utflen)
|
||||||
char3 = (int) bytearr[count-1];
|
throw new UTFDataFormatException("malformed input: partial character at end");
|
||||||
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
|
char2 = (int) bytearr[count - 1];
|
||||||
throw new UTFDataFormatException(
|
if ((char2 & 0xC0) != 0x80)
|
||||||
"malformed input around byte " + (count-1));
|
throw new UTFDataFormatException("malformed input around byte " + count);
|
||||||
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
|
chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
|
||||||
((char2 & 0x3F) << 6) |
|
break;
|
||||||
((char3 & 0x3F) << 0));
|
case 14:
|
||||||
break;
|
/* 1110 xxxx 10xx xxxx 10xx xxxx */
|
||||||
default:
|
count += 3;
|
||||||
/* 10xx xxxx, 1111 xxxx */
|
if (count > utflen)
|
||||||
throw new UTFDataFormatException(
|
throw new UTFDataFormatException("malformed input: partial character at end");
|
||||||
"malformed input around byte " + count);
|
char2 = (int) bytearr[count - 2];
|
||||||
}
|
char3 = (int) bytearr[count - 1];
|
||||||
}
|
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
|
||||||
// The number of chars produced may be less than utflen
|
throw new UTFDataFormatException("malformed input around byte " + (count - 1));
|
||||||
return new String(chararr, 0, chararr_count);
|
chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
|
||||||
}
|
break;
|
||||||
|
default:
|
||||||
/**
|
/* 10xx xxxx, 1111 xxxx */
|
||||||
* See the general contract of the <code>readUnsignedShort</code>
|
throw new UTFDataFormatException("malformed input around byte " + count);
|
||||||
* method of <code>DataInput</code>.
|
}
|
||||||
* <p>
|
}
|
||||||
* Bytes
|
// The number of chars produced may be less than utflen
|
||||||
* for this operation are read from the given byte buffer
|
return new String(chararr, 0, chararr_count);
|
||||||
*
|
}
|
||||||
* @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
|
* See the general contract of the <code>readUnsignedShort</code> method of <code>DataInput</code>.
|
||||||
* reading two bytes.
|
* <p>
|
||||||
* @exception IOException the stream has been closed and the contained
|
* Bytes for this operation are read from the given byte buffer
|
||||||
* input stream does not support reading after close, or
|
*
|
||||||
* another I/O error occurs.
|
* @return the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.
|
||||||
* @see java.io.FilterInputStream#in
|
* @exception EOFException
|
||||||
*/
|
* if this input stream reaches the end before reading two bytes.
|
||||||
public static int readUnsignedShort(ByteBuffer bb) throws BufferUnderflowException {
|
* @exception IOException
|
||||||
int ch1 = bb.get() & 0xFF; // convert byte to unsigned byte
|
* the stream has been closed and the contained input stream does not support reading after close, or another
|
||||||
int ch2 = bb.get() & 0xFF; // convert byte to unsigned byte
|
* I/O error occurs.
|
||||||
return (ch1 << 8) + (ch2 << 0);
|
* @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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,95 +17,95 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.string;
|
package marytts.util.string;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A helper class converting between a given set of integers and strings.
|
* A helper class converting between a given set of integers and strings.
|
||||||
* @author schroed
|
* @author schroed
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class IntStringTranslator
|
public class IntStringTranslator
|
||||||
{
|
{
|
||||||
ArrayList<String> list;
|
ArrayList<String> list;
|
||||||
Map<String,Integer> map;
|
Map<String,Integer> map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize empty int-string two-way translator.
|
* Initialize empty int-string two-way translator.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public IntStringTranslator()
|
public IntStringTranslator()
|
||||||
{
|
{
|
||||||
list = new ArrayList<String>();
|
list = new ArrayList<String>();
|
||||||
map = new HashMap<String, Integer>();
|
map = new HashMap<String, Integer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntStringTranslator(int initialRange)
|
public IntStringTranslator(int initialRange)
|
||||||
{
|
{
|
||||||
list = new ArrayList<String>(initialRange);
|
list = new ArrayList<String>(initialRange);
|
||||||
map = new HashMap<String, Integer>();
|
map = new HashMap<String, Integer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a int-string two-way translator,
|
* Initialize a int-string two-way translator,
|
||||||
* setting int values according to the position of strings
|
* setting int values according to the position of strings
|
||||||
* in the array.
|
* in the array.
|
||||||
* @param strings
|
* @param strings
|
||||||
*/
|
*/
|
||||||
public IntStringTranslator(String[] strings)
|
public IntStringTranslator(String[] strings)
|
||||||
{
|
{
|
||||||
list = new ArrayList<String>(Arrays.asList(strings));
|
list = new ArrayList<String>(Arrays.asList(strings));
|
||||||
map = new HashMap<String, Integer>();
|
map = new HashMap<String, Integer>();
|
||||||
for (int i=0; i<strings.length; i++) {
|
for (int i=0; i<strings.length; i++) {
|
||||||
map.put(strings[i], i);
|
map.put(strings[i], i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(int i, String s)
|
public void set(int i, String s)
|
||||||
{
|
{
|
||||||
list.add(i, s);
|
list.add(i, s);
|
||||||
map.put(s, i);
|
map.put(s, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(String s)
|
public boolean contains(String s)
|
||||||
{
|
{
|
||||||
return map.containsKey(s);
|
return map.containsKey(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(int b)
|
public boolean contains(int b)
|
||||||
{
|
{
|
||||||
int index = b;
|
int index = b;
|
||||||
if (index < 0 || index >= list.size()) return false;
|
if (index < 0 || index >= list.size()) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int get(String s)
|
public int get(String s)
|
||||||
{
|
{
|
||||||
Integer index = map.get(s);
|
Integer index = map.get(s);
|
||||||
if (index == null)
|
if (index == null)
|
||||||
throw new IllegalArgumentException("No int value known for string ["+s+"]");
|
throw new IllegalArgumentException("No int value known for string ["+s+"]");
|
||||||
return index.intValue();
|
return index.intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(int i)
|
public String get(int i)
|
||||||
{
|
{
|
||||||
if (i < 0 || i >= list.size())
|
if (i < 0 || i >= list.size())
|
||||||
throw new IndexOutOfBoundsException("Int value out of range: "+i);
|
throw new IndexOutOfBoundsException("Int value out of range: "+i);
|
||||||
return list.get(i);
|
return list.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getStringValues()
|
public String[] getStringValues()
|
||||||
{
|
{
|
||||||
return list.toArray(new String[0]);
|
return list.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHighestValue()
|
public int getHighestValue()
|
||||||
{
|
{
|
||||||
return list.size();
|
return list.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,99 +17,99 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package marytts.util.string;
|
package marytts.util.string;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A helper class converting between a given set of shorts and strings.
|
* A helper class converting between a given set of shorts and strings.
|
||||||
* @author schroed
|
* @author schroed
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ShortStringTranslator
|
public class ShortStringTranslator
|
||||||
{
|
{
|
||||||
ArrayList<String> list;
|
ArrayList<String> list;
|
||||||
Map<String,Short> map;
|
Map<String,Short> map;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize empty short-string two-way translator.
|
* Initialize empty short-string two-way translator.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public ShortStringTranslator()
|
public ShortStringTranslator()
|
||||||
{
|
{
|
||||||
list = new ArrayList<String>();
|
list = new ArrayList<String>();
|
||||||
map = new HashMap<String, Short>();
|
map = new HashMap<String, Short>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShortStringTranslator(short initialRange)
|
public ShortStringTranslator(short initialRange)
|
||||||
{
|
{
|
||||||
list = new ArrayList<String>(initialRange);
|
list = new ArrayList<String>(initialRange);
|
||||||
map = new HashMap<String, Short>();
|
map = new HashMap<String, Short>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a short-string two-way translator,
|
* Initialize a short-string two-way translator,
|
||||||
* setting short values according to the position of strings
|
* setting short values according to the position of strings
|
||||||
* in the array.
|
* in the array.
|
||||||
* @param strings
|
* @param strings
|
||||||
*/
|
*/
|
||||||
public ShortStringTranslator(String[] strings)
|
public ShortStringTranslator(String[] strings)
|
||||||
{
|
{
|
||||||
if (strings.length > Short.MAX_VALUE) throw new IllegalArgumentException("Too many strings for a short-string translator");
|
if (strings.length > Short.MAX_VALUE) throw new IllegalArgumentException("Too many strings for a short-string translator");
|
||||||
list = new ArrayList<String>(Arrays.asList(strings));
|
list = new ArrayList<String>(Arrays.asList(strings));
|
||||||
map = new HashMap<String, Short>();
|
map = new HashMap<String, Short>();
|
||||||
for (int i=0; i<strings.length; i++) {
|
for (int i=0; i<strings.length; i++) {
|
||||||
map.put(strings[i], (short)i);
|
map.put(strings[i], (short)i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(short b, String s)
|
public void set(short b, String s)
|
||||||
{
|
{
|
||||||
list.add(b, s);
|
list.add(b, s);
|
||||||
map.put(s, b);
|
map.put(s, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(String s)
|
public boolean contains(String s)
|
||||||
{
|
{
|
||||||
return map.containsKey(s);
|
return map.containsKey(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(short b)
|
public boolean contains(short b)
|
||||||
{
|
{
|
||||||
int index = (int) b;
|
int index = (int) b;
|
||||||
if (index < 0 || index >= list.size()) return false;
|
if (index < 0 || index >= list.size()) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public short get(String s)
|
public short get(String s)
|
||||||
{
|
{
|
||||||
Short index = map.get(s);
|
Short index = map.get(s);
|
||||||
if (index == null)
|
if (index == null)
|
||||||
throw new IllegalArgumentException("No short value known for string ["+s+"]");
|
throw new IllegalArgumentException("No short value known for string ["+s+"]");
|
||||||
return index.shortValue();
|
return index.shortValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(short b)
|
public String get(short b)
|
||||||
{
|
{
|
||||||
int index = (int) b;
|
int index = (int) b;
|
||||||
if (index < 0 || index >= list.size())
|
if (index < 0 || index >= list.size())
|
||||||
throw new IndexOutOfBoundsException("Short value out of range: "+index);
|
throw new IndexOutOfBoundsException("Short value out of range: "+index);
|
||||||
return list.get(index);
|
return list.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getStringValues()
|
public String[] getStringValues()
|
||||||
{
|
{
|
||||||
return list.toArray(new String[0]);
|
return list.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getNumberOfValues()
|
public short getNumberOfValues()
|
||||||
{
|
{
|
||||||
return (short) list.size();
|
return (short) list.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче