001/* An actor that converts a string to an array of bytes. 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.Token; 033import ptolemy.data.UnsignedByteToken; 034import ptolemy.data.type.ArrayType; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040/////////////////////////////////////////////////////////////////// 041/// StringToUnsignedByteArray 042 043/** 044 Convert a string to an array of unsigned byte. The conversion is performed 045 using the default character set, returned by the system property 046 "file.encoding". 047 048 @author Winthrop Williams, Steve Neuendorffer 049 @version $Id$ 050 @since Ptolemy II 2.0 051 @Pt.ProposedRating Red (winthrop) 052 @Pt.AcceptedRating Red (winthrop) 053 */ 054public class StringToUnsignedByteArray extends Converter { 055 /** Construct an actor with the given container and name. 056 * @param container The container. 057 * @param name The name of this actor. 058 * @exception IllegalActionException If the actor cannot be contained 059 * by the proposed container. 060 * @exception NameDuplicationException If the container already has an 061 * actor with this name. 062 */ 063 public StringToUnsignedByteArray(CompositeEntity container, String name) 064 throws NameDuplicationException, IllegalActionException { 065 super(container, name); 066 067 input.setTypeEquals(BaseType.STRING); 068 069 output.setTypeEquals(new ArrayType(BaseType.UNSIGNED_BYTE)); 070 } 071 072 /////////////////////////////////////////////////////////////////// 073 //// public methods //// 074 075 /** Consume one string token on the input port and output a new array 076 * token of integer tokens on the output port. The low byte of each 077 * integer is the byte form of one of the characters. The other 078 * three bytes of each integer may be 0x000000 or 0xFFFFFF. The 079 * first character of the string is copied to the first element of 080 * the array, and so on. NOTE: Assumes an 8-bit character set is 081 * the default setting for this implementation of Java. 082 * 083 * @exception IllegalActionException If there is no director. 084 * FIXME: Does this method actually check if there is a director? 085 */ 086 @Override 087 public void fire() throws IllegalActionException { 088 super.fire(); 089 String inputValue = ((StringToken) input.get(0)).stringValue(); 090 091 byte[] dataBytes = inputValue.getBytes(); 092 093 int bytesAvailable = dataBytes.length; 094 Token[] dataTokens = new Token[bytesAvailable]; 095 096 for (int j = 0; j < bytesAvailable; j++) { 097 dataTokens[j] = new UnsignedByteToken(dataBytes[j]); 098 } 099 100 output.send(0, new ArrayToken(BaseType.UNSIGNED_BYTE, dataTokens)); 101 } 102 103 /** Return false if the input port has no token, otherwise return 104 * what the superclass returns (presumably true). 105 * @exception IllegalActionException If there is no director. 106 */ 107 @Override 108 public boolean prefire() throws IllegalActionException { 109 if (!input.hasToken(0)) { 110 return false; 111 } 112 113 return super.prefire(); 114 } 115}