001/* An actor that converts an array of unsigned bytes into a string.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib.conversions;
029
030import ptolemy.data.ArrayToken;
031import ptolemy.data.StringToken;
032import ptolemy.data.UnsignedByteToken;
033import ptolemy.data.type.ArrayType;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040/// UnsignedByteArrayToString
041
042/**
043 Convert an array of bytes into a string.  The conversion is performed
044 using the default character set, returned by the system property
045 "file.encoding".  The output is a string assembled from these bytes.
046
047 @author Winthrop Williams, Steve Neuendorffer
048 @version $Id$
049 @since Ptolemy II 2.0
050 @Pt.ProposedRating Red (winthrop)
051 @Pt.AcceptedRating Red (winthrop)
052 */
053public class UnsignedByteArrayToString extends Converter {
054    /** Construct an actor with the given container and name.
055     *  @param container The container.
056     *  @param name The name of this actor.
057     *  @exception IllegalActionException If the actor cannot be contained
058     *   by the proposed container.
059     *  @exception NameDuplicationException If the container already has an
060     *   actor with this name.
061     */
062    public UnsignedByteArrayToString(CompositeEntity container, String name)
063            throws NameDuplicationException, IllegalActionException {
064        super(container, name);
065
066        input.setTypeEquals(new ArrayType(BaseType.UNSIGNED_BYTE));
067
068        output.setTypeEquals(BaseType.STRING);
069    }
070
071    ///////////////////////////////////////////////////////////////////
072    ////                         public methods                    ////
073
074    /** Consume one array token of integer tokens on the input port
075     *  and output a new string token on the output port.  The least
076     *  significant byte of the first integer generates the first
077     *  character in the string, etc.  NOTE: Java has many options
078     *  regarding its character set.  This actor relies on the default
079     *  setting on the platform on which it is run.  However, it
080     *  assumes that this character set is an 8-bit character set.
081     *
082     *  @exception IllegalActionException If there is no director.
083     *  FIXME: Either verify that it does check for the director,
084     *  or remove this statement.  This statement occurs in other
085     *  conversion actor(s) as well.
086     */
087    @Override
088    public void fire() throws IllegalActionException {
089        super.fire();
090        ArrayToken dataArrayToken = (ArrayToken) input.get(0);
091        byte[] dataBytes = new byte[dataArrayToken.length()];
092
093        for (int j = 0; j < dataArrayToken.length(); j++) {
094            UnsignedByteToken dataToken = (UnsignedByteToken) dataArrayToken
095                    .getElement(j);
096            dataBytes[j] = dataToken.byteValue();
097        }
098
099        // Convert using the default character encoding.
100        String outputString = new String(dataBytes);
101        output.send(0, new StringToken(outputString));
102    }
103
104    /** Return false if the input port has no token, otherwise return
105     *  what the superclass returns (presumably true).
106     *  @exception IllegalActionException If there is no director.
107     */
108    @Override
109    public boolean prefire() throws IllegalActionException {
110        if (!input.hasToken(0)) {
111            return false;
112        }
113
114        return super.prefire();
115    }
116}