001// Copyright (C) 1999-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
002// All rights reserved.  Use of this class is limited.
003// Please see the LICENSE for more information.
004
005package com.oreilly.servlet.multipart;
006
007import java.io.ByteArrayOutputStream;
008import java.io.IOException;
009import java.io.UnsupportedEncodingException;
010import javax.servlet.ServletInputStream;
011
012/**
013 * A <code>ParamPart</code> is an upload part which represents a normal 
014 * <code>INPUT</code> (for example a non <code>TYPE="file"</code>) form
015 * parameter.
016 * 
017 * @author Geoff Soutter
018 * @author Jason Hunter
019 * @version 1.1, 2002/04/30, added better encoding support, thanks to
020 *                           Changshin Lee
021 * @version 1.0, 2000/10/27, initial revision
022 */
023public class ParamPart extends Part {
024  
025  /** contents of the parameter */
026  private byte[] value;
027
028  private String encoding;
029
030  /**
031   * Constructs a parameter part; this is called by the parser.
032   * 
033   * @param name the name of the parameter.
034   * @param in the servlet input stream to read the parameter value from.
035   * @param boundary the MIME boundary that delimits the end of parameter value.
036   * @param encoding the byte-to-char encoding to use by default
037   * value.
038   */
039  ParamPart(String name, ServletInputStream in, 
040            String boundary, String encoding) throws IOException {
041    super(name);
042    this.encoding = encoding;
043
044    // Copy the part's contents into a byte array
045    PartInputStream pis = new PartInputStream(in, boundary);
046    ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
047    byte[] buf = new byte[128];
048    int read;
049    while ((read = pis.read(buf)) != -1) {
050      baos.write(buf, 0, read);
051    }
052    pis.close();
053    baos.close();
054    
055    // save it for later
056    value = baos.toByteArray();
057  }
058
059  /** 
060   * Returns the value of the parameter as an array of bytes or a zero length 
061   * array if the user entered no value for this parameter.
062   * 
063   * @return value of parameter as raw bytes
064   */
065  public byte[] getValue() {
066    return value;
067  }
068  
069  /** 
070   * Returns the value of the parameter in as a string (using the
071   * parser-specified encoding to convert from bytes) or the empty string
072   * if the user entered no value for this parameter.
073   * 
074   * @return value of parameter as a string.
075   */
076  public String getStringValue() 
077      throws UnsupportedEncodingException {
078    return getStringValue(encoding);
079  }
080  
081  /** 
082   * Returns the value of the parameter in the supplied encoding
083   * or empty string if the user entered no value for this parameter.
084   * 
085   * @return value of parameter as a string.
086   */
087  public String getStringValue(String encoding) 
088      throws UnsupportedEncodingException {
089    return new String(value, encoding);
090  }
091  
092  /**
093   * Returns <code>true</code> to indicate this part is a parameter.
094   * 
095   * @return true.
096   */
097  public boolean isParam() {
098    return true;
099  }
100}