umagesh 2002/07/19 10:48:36
Modified: . WHATSNEW
docs/manual/CoreTasks loadproperties.html
src/main/org/apache/tools/ant/filters StringInputStream.java
src/main/org/apache/tools/ant/taskdefs LoadProperties.java
Log:
* Add encoding attribute to <loadproperties>
* Remove dependency of <loadproperties> on StringInputStream
* Modify StringInputStream to support encoding
* Note that StringInputStream is not being used by any other Ant classes -
the sole consumer was LoadProperties which no longer uses it.
Revision Changes Path
1.278 +2 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.277
retrieving revision 1.278
diff -u -r1.277 -r1.278
--- WHATSNEW 17 Jul 2002 07:36:51 -0000 1.277
+++ WHATSNEW 19 Jul 2002 17:48:36 -0000 1.278
@@ -24,6 +24,8 @@
Other changes:
--------------
+* <loadproperties> has a new encoding attribute.
+
* <echoproperties> can now create XML output.
* <echoproperties> has a new srcfile attribute that can make it read
1.2 +5 -0 jakarta-ant/docs/manual/CoreTasks/loadproperties.html
Index: loadproperties.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/loadproperties.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- loadproperties.html 6 Mar 2002 03:25:49 -0000 1.1
+++ loadproperties.html 19 Jul 2002 17:48:36 -0000 1.2
@@ -28,6 +28,11 @@
<td valign="top">source file</td>
<td valign="top" align="center">Yes</td>
</tr>
+ <tr>
+ <td valign="top">encoding</td>
+ <td valign="top">encoding to use when loading the file</td>
+ <td align="center" valign="top">No</td>
+ </tr>
</table>
<p>
The LoadProperties task supports nested <a
href="../CoreTypes/filterchain.html">
1.8 +112 -25
jakarta-ant/src/main/org/apache/tools/ant/filters/StringInputStream.java
Index: StringInputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/filters/StringInputStream.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- StringInputStream.java 30 Apr 2002 10:32:01 -0000 1.7
+++ StringInputStream.java 19 Jul 2002 17:48:36 -0000 1.8
@@ -58,8 +58,7 @@
import java.io.StringReader;
/**
- * Wraps a String as an InputStream. Note that data will be lost for
- * characters not in ISO Latin 1, as a simple char->byte mapping is assumed.
+ * Wraps a String as an InputStream.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Magesh Umasankar</a>
*/
@@ -68,9 +67,15 @@
/** Source string, stored as a StringReader */
private StringReader in;
+ private String encoding;
+
+ private byte[] slack;
+
+ private int begin;
+
/**
* Composes a stream from a String
- *
+ *
* @param source The string to read from. Must not be <code>null</code>.
*/
public StringInputStream(String source) {
@@ -78,32 +83,88 @@
}
/**
- * Reads from the Stringreader, returning the same value. Note that
- * data will be lost for characters not in ISO Latin 1. Clients
- * assuming a return value in the range -1 to 255 may even fail on
- * such input.
- *
- * @return the value of the next character in the StringReader
- *
- * @exception IOException if the original StringReader fails to be read
+ * Composes a stream from a String with the specified encoding
+ *
+ * @param source The string to read from. Must not be <code>null</code>.
+ * @param encoding The encoding scheme.
*/
- public int read() throws IOException {
- return in.read();
+ public StringInputStream(String source, String encoding) {
+ in = new StringReader(source);
+ this.encoding = encoding;
}
/**
- * Closes the Stringreader.
- *
- * @exception IOException if the original StringReader fails to be closed
+ * Reads from the Stringreader, returning the same value.
+ *
+ * @return the value of the next character in the StringReader
+ *
+ * @exception IOException if the original StringReader fails to be read
*/
- public void close() throws IOException {
- in.close();
+ public synchronized int read() throws IOException {
+ if (in == null) {
+ throw new IOException("Stream Closed");
+ }
+
+ byte result;
+ if (slack != null && begin < slack.length) {
+ result = slack[begin];
+ if (++begin == slack.length) {
+ slack = null;
+ }
+ } else {
+ byte[] buf = new byte[1];
+ if (read(buf, 0, 1) <= 0) {
+ return -1;
+ }
+ result = buf[0];
+ }
+ if (result < 0) {
+ return 256 + result;
+ } else {
+ return result;
+ }
+ }
+
+ public synchronized int read(byte[] b, int off, int len)
+ throws IOException {
+
+ if (in == null) {
+ throw new IOException("Stream Closed");
+ }
+
+ while (slack == null) {
+ char[] buf = new char[len]; // might read too much
+ int n = in.read(buf);
+ if (n == -1) {
+ return -1;
+ }
+ if (n > 0) {
+ String s = new String(buf, 0, n);
+ if (encoding == null) {
+ slack = s.getBytes();
+ } else {
+ slack = s.getBytes(encoding);
+ }
+ begin = 0;
+ }
+ }
+
+ if (len > slack.length - begin) {
+ len = slack.length - begin;
+ }
+
+ System.arraycopy(slack, begin, b, off, len);
+
+ if ((begin += len) >= slack.length) {
+ slack = null;
+ }
+ return len;
}
/**
* Marks the read limit of the StringReader.
- *
- * @param limit the maximum limit of bytes that can be read before the
+ *
+ * @param limit the maximum limit of bytes that can be read before the
* mark position becomes invalid
*/
public synchronized void mark(final int limit) {
@@ -114,20 +175,46 @@
}
}
+
+ public synchronized int available() throws IOException {
+ if (in == null) {
+ throw new IOException("Stream Closed");
+ }
+ if (slack != null) {
+ return slack.length - begin;
+ }
+ if (in.ready()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public boolean markSupported () {
+ return false; // would be imprecise
+ }
+
/**
* Resets the StringReader.
- *
+ *
* @exception IOException if the StringReader fails to be reset
*/
public synchronized void reset() throws IOException {
+ if (in == null) {
+ throw new IOException("Stream Closed");
+ }
+ slack = null;
in.reset();
}
/**
- * @see InputStream#markSupported
+ * Closes the Stringreader.
+ *
+ * @exception IOException if the original StringReader fails to be closed
*/
- public boolean markSupported() {
- return in.markSupported();
+ public synchronized void close() throws IOException {
+ in.close();
+ slack = null;
+ in = null;
}
}
-
1.8 +35 -5
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
Index: LoadProperties.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- LoadProperties.java 22 Jun 2002 23:38:32 -0000 1.7
+++ LoadProperties.java 19 Jul 2002 17:48:36 -0000 1.8
@@ -56,12 +56,12 @@
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.FilterChain;
-import org.apache.tools.ant.filters.StringInputStream;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
@@ -89,6 +89,12 @@
private final Vector filterChains = new Vector();
/**
+ * Encoding to use for filenames, defaults to the platform's default
+ * encoding.
+ */
+ private String encoding = null;
+
+ /**
* Sets the file to load.
*
* @param srcFile The new SrcFile value
@@ -98,6 +104,21 @@
}
/**
+ * Encoding to use for input, defaults to the platform's default
+ * encoding. <p>
+ *
+ * For a list of possible values see <a
href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
+ *
http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
+ * </a>.</p>
+ *
+ * @param encoding The new Encoding value
+ */
+
+ public final void setEncoding(final String encoding) {
+ this.encoding = encoding;
+ }
+
+ /**
* read in a source file's contents and load them up as Ant properties
*
* @exception BuildException if something goes wrong with the build
@@ -127,7 +148,11 @@
//open up the file
fis = new FileInputStream(srcFile);
bis = new BufferedInputStream(fis);
- instream = new InputStreamReader(bis);
+ if (encoding == null) {
+ instream = new InputStreamReader(bis);
+ } else {
+ instream = new InputStreamReader(bis, encoding);
+ }
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(size);
@@ -143,9 +168,14 @@
text = text + "\n";
}
- final StringInputStream sis = new StringInputStream(text);
+ ByteArrayInputStream tis = null;
+ if ( encoding == null ) {
+ tis = new ByteArrayInputStream(text.getBytes());
+ } else {
+ tis = new ByteArrayInputStream(text.getBytes(encoding));
+ }
final Properties props = new Properties();
- props.load(sis);
+ props.load(tis);
final Enumeration e = props.keys();
while (e.hasMoreElements()) {
final String key = (String) e.nextElement();
@@ -155,7 +185,7 @@
project.setNewProperty(key, value);
}
}
- sis.close();
+ tis.close();
}
} catch (final IOException ioe) {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>