[jboss-svn-commits] JBoss Common SVN: r3190 - in jboss-stdio/trunk: src and 5 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed May 20 18:14:10 EDT 2009
Author: david.lloyd at jboss.com
Date: 2009-05-20 18:14:09 -0400 (Wed, 20 May 2009)
New Revision: 3190
Added:
jboss-stdio/trunk/pom.xml
jboss-stdio/trunk/src/
jboss-stdio/trunk/src/main/
jboss-stdio/trunk/src/main/java/
jboss-stdio/trunk/src/main/java/org/
jboss-stdio/trunk/src/main/java/org/jboss/
jboss-stdio/trunk/src/main/java/org/jboss/stdio/
jboss-stdio/trunk/src/main/java/org/jboss/stdio/AbstractLoggingWriter.java
jboss-stdio/trunk/src/main/java/org/jboss/stdio/LoggingWriter.java
jboss-stdio/trunk/src/main/java/org/jboss/stdio/NullInputStream.java
jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContext.java
jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java
jboss-stdio/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java
Log:
Initial import: jboss-stdio
Added: jboss-stdio/trunk/pom.xml
===================================================================
--- jboss-stdio/trunk/pom.xml (rev 0)
+++ jboss-stdio/trunk/pom.xml 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,89 @@
+<!--
+ ~ JBoss, Home of Professional Open Source.
+ ~ Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ ~ as indicated by the @author tags. See the copyright.txt file in the
+ ~ distribution for a full listing of individual contributors.
+ ~
+ ~ This is free software; you can redistribute it and/or modify it
+ ~ under the terms of the GNU Lesser General Public License as
+ ~ published by the Free Software Foundation; either version 2.1 of
+ ~ the License, or (at your option) any later version.
+ ~
+ ~ This software is distributed in the hope that it will be useful,
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ ~ Lesser General Public License for more details.
+ ~
+ ~ You should have received a copy of the GNU Lesser General Public
+ ~ License along with this software; if not, write to the Free
+ ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.jboss.stdio</groupId>
+ <artifactId>jboss-stdio</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0.0.CR1</version>
+ <dependencies/>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-source-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <phase>verify</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <configuration>
+ <doclet>net.gleamynode.apiviz.APIviz</doclet>
+ <docletArtifact>
+ <groupId>org.jboss.apiviz</groupId>
+ <artifactId>apiviz</artifactId>
+ <version>1.2.5.GA</version>
+ </docletArtifact>
+ <doctitle><![CDATA[JBoss STDIO 1.0.0.CR1]]></doctitle>
+ <header><![CDATA[JBoss STDIO 1.0.0.CR1]]></header>
+ <footer><![CDATA[JBoss STDIO 1.0.0.CR1]]></footer>
+ <bottom><![CDATA[<i>Copyright © 2009 JBoss, a division of Red Hat, Inc.</i>]]></bottom>
+ <links>
+ <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
+ </links>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
+ <distributionManagement>
+ <repository>
+ <id>repository.jboss.org</id>
+ <name>JBoss Maven2 Repository</name>
+ <url>http://repository.jboss.org/repos/maven2</url>
+ </repository>
+ </distributionManagement>
+</project>
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/AbstractLoggingWriter.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/AbstractLoggingWriter.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/AbstractLoggingWriter.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.Writer;
+import java.io.IOException;
+
+/**
+ * Abstract base class for writers which log to a logger.
+ */
+public abstract class AbstractLoggingWriter extends Writer {
+
+ private final StringBuilder buffer = new StringBuilder();
+
+ /**
+ * Construct a new instance.
+ */
+ protected AbstractLoggingWriter() {
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void write(final int c) throws IOException {
+ final java.util.logging.Logger logger = getLogger();
+ if (logger == null) {
+ return;
+ }
+ synchronized (buffer) {
+ if (c == '\n') {
+ logger.log(getLevel(), buffer.toString());
+ buffer.setLength(0);
+ } else {
+ buffer.append((char) c);
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void write(final char[] cbuf, final int off, final int len) throws IOException {
+ final java.util.logging.Logger logger = getLogger();
+ if (logger == null) {
+ return;
+ }
+ synchronized (buffer) {
+ int mark = 0;
+ int i;
+ for (i = 0; i < len; i++) {
+ final char c = cbuf[off + i];
+ if (c == '\n') {
+ buffer.append(cbuf, mark + off, i - mark);
+ logger.log(getLevel(), buffer.toString());
+ buffer.setLength(0);
+ mark = i + 1;
+ }
+ }
+ buffer.append(cbuf, mark + off, i - mark);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void flush() throws IOException {
+ final java.util.logging.Logger logger = getLogger();
+ if (logger == null) {
+ return;
+ }
+ synchronized (buffer) {
+ if (buffer.length() > 0) {
+ buffer.append(" >>> FLUSH");
+ logger.log(getLevel(), buffer.toString());
+ buffer.setLength(0);
+ }
+ }
+ }
+
+ /**
+ * Get the logger to use.
+ *
+ * @return the logger
+ */
+ protected abstract java.util.logging.Logger getLogger();
+
+ /**
+ * Get the level at which to log.
+ *
+ * @return the level
+ */
+ protected abstract java.util.logging.Level getLevel();
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ // ignore
+ }
+}
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/LoggingWriter.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/LoggingWriter.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/LoggingWriter.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * A writer which sends its data to a logger.
+ */
+public final class LoggingWriter extends AbstractLoggingWriter {
+
+ @SuppressWarnings({ "NonConstantLogger" })
+ private final Logger log;
+ private final Level level;
+
+ /**
+ * Construct a new instance.
+ *
+ * @param category the log category to use
+ * @param level the level at which to log messages
+ */
+ public LoggingWriter(final String category, final Level level) {
+ this.level = level;
+ log = Logger.getLogger(category);
+ }
+
+ /**
+ * Construct a new instance.
+ *
+ * @param log the logger to use
+ * @param level the level at which to log messages
+ */
+ public LoggingWriter(final Logger log, final Level level) {
+ this.log = log;
+ this.level = level;
+ }
+
+ /** {@inheritDoc} */
+ protected Logger getLogger() {
+ return log;
+ }
+
+ /** {@inheritDoc} */
+ protected Level getLevel() {
+ return level;
+ }
+}
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/NullInputStream.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/NullInputStream.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/NullInputStream.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.InputStream;
+
+/**
+ * An input stream that is always in an EOF condition.
+ */
+public final class NullInputStream extends InputStream {
+
+ /**
+ * Read a byte. Always returns EOF.
+ *
+ * @return -1 always
+ */
+ public int read() {
+ return -1;
+ }
+}
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContext.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContext.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContext.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,356 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.IOException;
+import java.security.Permission;
+import java.util.Locale;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A context for console input and output.
+ */
+public final class StdioContext {
+ private static final StdioContext SYSTEM_STDIO_CONTEXT = new StdioContext(System.in, System.out, System.err);
+
+ private static final Permission CREATE_CONTEXT_PERMISSION = new RuntimePermission("createStdioContext", null);
+ private static final Permission SET_CONTEXT_SELECTOR_PERMISSION = new RuntimePermission("setStdioContextSelector", null);
+ private static final Permission INSTALL_PERMISSION = new RuntimePermission("installStdioContextSelector", null);
+
+ private enum State {
+ UNINSTALLED,
+ INSTALLING,
+ INSTALLED,
+ UNINSTALLING,
+ }
+
+ private static final AtomicReference<State> state = new AtomicReference<State>(State.UNINSTALLED);
+
+ private final InputStream in;
+ private final PrintStream out;
+ private final PrintStream err;
+
+ StdioContext(final InputStream in, final PrintStream out, final PrintStream err) {
+ this.in = in;
+ this.out = out;
+ this.err = err;
+ }
+
+ /**
+ * Create a console I/O context.
+ *
+ * @param in the input stream for this context
+ * @param out the output stream for this context
+ * @param err the error stream for this context
+ * @return the new context
+ * @throws SecurityException if the caller does not have the {@code createStdioContext} {@link RuntimePermission}
+ */
+ public static StdioContext create(final InputStream in, final PrintStream out, final PrintStream err) throws SecurityException {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(CREATE_CONTEXT_PERMISSION);
+ }
+ return new StdioContext(in, out, err);
+ }
+
+ /**
+ * Get the current console I/O context.
+ *
+ * @return the current context
+ */
+ public static StdioContext getStdioContext() {
+ return stdioContextSelector.getStdioContext();
+ }
+
+ /**
+ * Get the input stream for this context.
+ *
+ * @return the input stream
+ */
+ public InputStream getIn() {
+ return in;
+ }
+
+ /**
+ * Get the output stream for this context.
+ *
+ * @return the output stream
+ */
+ public PrintStream getOut() {
+ return out;
+ }
+
+ /**
+ * Get the error stream for this context.
+ *
+ * @return the error stream
+ */
+ public PrintStream getErr() {
+ return err;
+ }
+
+ private static volatile StdioContextSelector stdioContextSelector = new StdioContextSelector() {
+ public StdioContext getStdioContext() {
+ return SYSTEM_STDIO_CONTEXT;
+ }
+ };
+
+ /**
+ * Install the StdioContext streams.
+ *
+ * @throws SecurityException if the caller does not have the {@code installStdioContextSelector} {@link RuntimePermission}
+ * @throws IllegalStateException if the streams are already installed
+ */
+ public static void install() throws SecurityException, IllegalStateException {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(INSTALL_PERMISSION);
+ }
+ if (! state.compareAndSet(State.UNINSTALLED, State.INSTALLING)) {
+ throw new IllegalStateException("Already installed");
+ }
+ System.setOut(new DelegatingPrintStream() {
+ PrintStream getDelegate() {
+ return stdioContextSelector.getStdioContext().out;
+ }
+ });
+ System.setErr(new DelegatingPrintStream() {
+ PrintStream getDelegate() {
+ return stdioContextSelector.getStdioContext().err;
+ }
+ });
+ System.setIn(new DelegatingInputStream() {
+ InputStream getDelegate() {
+ return stdioContextSelector.getStdioContext().in;
+ }
+ });
+ state.set(State.INSTALLED);
+ }
+
+ /**
+ * Uninstall the StdioContext streams.
+ *
+ * @throws SecurityException if the caller does not have the {@code installStdioContextSelector} {@link RuntimePermission}
+ * @throws IllegalStateException if the streams are already uninstalled
+ */
+ public static void uninstall() throws SecurityException, IllegalStateException {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(INSTALL_PERMISSION);
+ }
+ if (! state.compareAndSet(State.INSTALLED, State.UNINSTALLING)) {
+ throw new IllegalStateException("Already uninstalled");
+ }
+ System.setOut(SYSTEM_STDIO_CONTEXT.out);
+ System.setErr(SYSTEM_STDIO_CONTEXT.err);
+ System.setIn(SYSTEM_STDIO_CONTEXT.in);
+ state.set(State.UNINSTALLED);
+ }
+
+ public static void setStdioContextSelector(final StdioContextSelector stdioContextSelector) {
+ if (stdioContextSelector == null) {
+ throw new NullPointerException("stdioContextSelector is null");
+ }
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(SET_CONTEXT_SELECTOR_PERMISSION);
+ }
+ StdioContext.stdioContextSelector = stdioContextSelector;
+ }
+
+ private static abstract class DelegatingPrintStream extends PrintStream {
+
+ protected DelegatingPrintStream() {
+ super((OutputStream) null);
+ }
+
+ abstract PrintStream getDelegate();
+
+ public void flush() {
+ getDelegate().flush();
+ }
+
+ public void close() {
+ getDelegate().close();
+ }
+
+ public boolean checkError() {
+ return getDelegate().checkError();
+ }
+
+ public void write(final int b) {
+ getDelegate().write(b);
+ }
+
+ public void write(final byte[] buf, final int off, final int len) {
+ getDelegate().write(buf, off, len);
+ }
+
+ public void print(final boolean b) {
+ getDelegate().print(b);
+ }
+
+ public void print(final char c) {
+ getDelegate().print(c);
+ }
+
+ public void print(final int i) {
+ getDelegate().print(i);
+ }
+
+ public void print(final long l) {
+ getDelegate().print(l);
+ }
+
+ public void print(final float f) {
+ getDelegate().print(f);
+ }
+
+ public void print(final double d) {
+ getDelegate().print(d);
+ }
+
+ public void print(final char[] s) {
+ getDelegate().print(s);
+ }
+
+ public void print(final String s) {
+ getDelegate().print(s);
+ }
+
+ public void print(final Object obj) {
+ getDelegate().print(obj);
+ }
+
+ public void println() {
+ getDelegate().println();
+ }
+
+ public void println(final boolean x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final char x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final int x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final long x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final float x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final double x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final char[] x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final String x) {
+ getDelegate().println(x);
+ }
+
+ public void println(final Object x) {
+ getDelegate().println(x);
+ }
+
+ public PrintStream printf(final String format, final Object... args) {
+ return getDelegate().printf(format, args);
+ }
+
+ public PrintStream printf(final Locale l, final String format, final Object... args) {
+ return getDelegate().printf(l, format, args);
+ }
+
+ public PrintStream format(final String format, final Object... args) {
+ return getDelegate().format(format, args);
+ }
+
+ public PrintStream format(final Locale l, final String format, final Object... args) {
+ return getDelegate().format(l, format, args);
+ }
+
+ public PrintStream append(final CharSequence csq) {
+ return getDelegate().append(csq);
+ }
+
+ public PrintStream append(final CharSequence csq, final int start, final int end) {
+ return getDelegate().append(csq, start, end);
+ }
+
+ public PrintStream append(final char c) {
+ return getDelegate().append(c);
+ }
+ }
+
+ private static abstract class DelegatingInputStream extends InputStream {
+ abstract InputStream getDelegate();
+
+ public int read() throws IOException {
+ return getDelegate().read();
+ }
+
+ public int read(final byte[] b) throws IOException {
+ return getDelegate().read(b);
+ }
+
+ public int read(final byte[] b, final int off, final int len) throws IOException {
+ return getDelegate().read(b, off, len);
+ }
+
+ public long skip(final long n) throws IOException {
+ return getDelegate().skip(n);
+ }
+
+ public int available() throws IOException {
+ return getDelegate().available();
+ }
+
+ public void close() throws IOException {
+ getDelegate().close();
+ }
+
+ public void mark(final int readlimit) {
+ getDelegate().mark(readlimit);
+ }
+
+ public void reset() throws IOException {
+ getDelegate().reset();
+ }
+
+ public boolean markSupported() {
+ return getDelegate().markSupported();
+ }
+ }
+}
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+/**
+ * A mechanism for determining what the current standard I/O context is. All I/O to {@link System#in}, {@link System#out},
+ * and {@link System#err} are handled by the current standard I/O context which is returned by the active instance
+ * of this interface.
+ *
+ * @see org.jboss.stdio.StdioContext#setStdioContextSelector(StdioContextSelector)
+ */
+public interface StdioContextSelector {
+
+ /**
+ * Get the current stdio context. Must not return {@code null}.
+ *
+ * @return the current stdio context
+ */
+ StdioContext getStdioContext();
+}
Added: jboss-stdio/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java
===================================================================
--- jboss-stdio/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java (rev 0)
+++ jboss-stdio/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java 2009-05-20 22:14:09 UTC (rev 3190)
@@ -0,0 +1,146 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.OutputStream;
+import java.io.IOException;
+import java.io.Writer;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.Charset;
+import java.nio.charset.CoderResult;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+
+/**
+ * An output stream which decodes into a writer.
+ */
+public final class WriterOutputStream extends OutputStream {
+
+ private final Writer writer;
+ private final CharsetDecoder decoder;
+ private final ByteBuffer inputBuffer;
+ private final CharBuffer outputBuffer;
+
+ /**
+ * Construct a new instance using the default charset.
+ *
+ * @param writer the writer to write to
+ */
+ public WriterOutputStream(final Writer writer) {
+ this(writer, Charset.defaultCharset());
+ }
+
+ /**
+ * Construct a new instance using the named charset.
+ *
+ * @param writer the writer to write to
+ * @param charsetName the charset name
+ */
+ public WriterOutputStream(final Writer writer, final String charsetName) {
+ this(writer, Charset.forName(charsetName));
+ }
+
+ /**
+ * Construct a new instance using the given charset.
+ *
+ * @param writer the writer to write to
+ * @param charset the charset
+ */
+ public WriterOutputStream(final Writer writer, final Charset charset) {
+ this(writer, charset.newDecoder());
+ }
+
+ /**
+ * Construct a new instance using the given charset decoder.
+ *
+ * @param writer the writer to write to
+ * @param decoder the charset decoder
+ */
+ public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) {
+ this.writer = writer;
+ this.decoder = decoder;
+ inputBuffer = ByteBuffer.allocate(256);
+ outputBuffer = CharBuffer.allocate(256);
+ }
+
+ /** {@inheritDoc} */
+ public void write(final int b) throws IOException {
+ synchronized (decoder) {
+ final ByteBuffer inputBuffer = this.inputBuffer;
+ inputBuffer.put((byte) b);
+ if (! inputBuffer.hasRemaining()) {
+ flush();
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void write(final byte[] b, int off, int len) throws IOException {
+ synchronized (decoder) {
+ for (;;) {
+ final ByteBuffer inputBuffer = this.inputBuffer;
+ int cnt = Math.min(inputBuffer.remaining(), len);
+ inputBuffer.put(b, off, cnt);
+ len -= cnt;
+ off += cnt;
+ if (len == 0) {
+ return;
+ }
+ flush();
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void flush() throws IOException {
+ synchronized (decoder) {
+ final CharBuffer outputBuffer = this.outputBuffer;
+ final ByteBuffer inputBuffer = this.inputBuffer;
+ inputBuffer.flip();
+ for (;;) {
+ final CoderResult coderResult = decoder.decode(inputBuffer, outputBuffer, false);
+ if (coderResult.isOverflow()) {
+ outputBuffer.flip();
+ boolean ok = false;
+ try {
+ writer.write(outputBuffer.array(), outputBuffer.arrayOffset(), outputBuffer.remaining());
+ ok = true;
+ } finally {
+ if (! ok) {
+ inputBuffer.clear();
+ }
+ outputBuffer.clear();
+ }
+ } else if (coderResult.isUnderflow()) {
+ inputBuffer.compact();
+ return;
+ }
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ flush();
+ }
+}
More information about the jboss-svn-commits
mailing list