1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: XStreamInput.java,v 1.9 2005/09/10 11:26:47 mat007 Exp $ 29 */ 30 31 package jtge.engine.io; 32 33 import java.io.EOFException; 34 import java.io.InputStream; 35 import java.io.InputStreamReader; 36 import java.io.ObjectInputStream; 37 import jtge.engine.command.ICommand; 38 import com.thoughtworks.xstream.XStream; 39 import com.thoughtworks.xstream.io.StreamException; 40 41 /*** 42 * Implements an input implementation based on the XStream library. 43 * 44 * @see <a href="http://xstream.codehaus.org">XStream</a> 45 * @author Mathieu Champlon 46 * @version $Revision: 1.9 $ $Date: 2005/09/10 11:26:47 $ 47 */ 48 public class XStreamInput implements IInputImp 49 { 50 private final XStream xstream; 51 private final InputStreamReader reader; 52 private ObjectInputStream input; 53 private boolean isClosed; 54 55 /*** 56 * Create an input. 57 * 58 * @param stream the stream to read data from 59 */ 60 public XStreamInput( final InputStream stream ) 61 { 62 xstream = new XStream(); 63 reader = new InputStreamReader( stream ); 64 isClosed = false; 65 } 66 67 /*** 68 * {@inheritDoc} 69 */ 70 public final ICommand read() 71 { 72 try 73 { 74 if( input == null ) 75 input = xstream.createObjectInputStream( reader ); 76 return (ICommand)input.readObject(); 77 } 78 catch( StreamException e ) 79 { 80 /*** 81 * FIXME : checks that XStream does not throw a specific Exception when there is nothing to read from.<br> 82 * We are forced to check the String message returned by the StreamException to detect the so called EOF 83 * Exception. 84 * <p> 85 * http://jira.codehaus.org/secure/BrowseProject.jspa?id=10230 86 */ 87 if( e.getMessage().endsWith( "input contained no data" ) || e.getMessage().endsWith( "Connection reset" ) ) 88 { 89 close(); 90 return null; 91 } 92 throw new InputOutputException( e ); 93 } 94 catch( EOFException e ) 95 { 96 close(); 97 return null; 98 } 99 catch( Exception e ) 100 { 101 throw new InputOutputException( e ); 102 } 103 } 104 105 /*** 106 * {@inheritDoc} 107 */ 108 public final void close() 109 { 110 isClosed = true; 111 try 112 { 113 if( input != null ) 114 input.close(); 115 reader.close(); 116 } 117 catch( Exception e ) 118 { 119 throw new InputOutputException( e ); 120 } 121 } 122 123 /*** 124 * {@inheritDoc} 125 */ 126 public final boolean isClosed() 127 { 128 return isClosed; 129 } 130 }