001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.servlet.multipart;
017    
018    import org.opengion.fukurou.util.Closer ;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.UnsupportedEncodingException;
023    import javax.servlet.ServletInputStream;
024    
025    /**
026     * ファイルア??ロード時のマルチパート???パラメータパ?ト部品です?
027     *
028     * パラメータ??を取り扱?す?
029     *
030     * @og.group そ?他機?
031     *
032     * @version  4.0
033     * @author   Kazuhiko Hasegawa
034     * @since    JDK5.0,
035     */
036    public class ParamPart extends Part {
037            private byte[] value;
038            private final String encoding;
039    
040            /**
041             * パラメータパ?ト部?オブジェクトを構築する?コンストラクター
042             *
043             * @param       name            パラメータの名前
044             * @param       in                      ServletInputStreamオブジェク?
045             * @param       boundary        ???
046             * @param       encoding        エンコー?
047             * @throws IOException
048             */
049            ParamPart( final String name, final ServletInputStream in,
050                                    final String boundary, final String encoding) throws IOException {
051                    super(name);
052                    this.encoding = encoding;
053    
054                    // Copy the part's contents into a byte array
055    
056                    PartInputStream pis = null;
057                    ByteArrayOutputStream baos = null;
058                    try {
059                            pis = new PartInputStream(in, boundary);
060                            baos = new ByteArrayOutputStream(512);
061                            byte[] buf = new byte[128];
062                            int read;
063                            while ((read = pis.read(buf)) != -1) {
064                                    baos.write(buf, 0, read);
065                            }
066                            value = baos.toByteArray();
067                    }
068                    finally {
069                            Closer.ioClose( pis );          // 4.0.0 (2006/01/31) close 処?の IOException を無?
070                            Closer.ioClose( baos );         // 4.0.0 (2006/01/31) close 処?の IOException を無?
071                    }
072            }
073    
074            /**
075             * 値をバイト?列で返します?
076             *
077             * @return  値のバイト??
078             */
079            public byte[] getValue() {
080                    if( value != null ) {
081                            return value.clone();
082                    }
083                    else {
084                            return new byte[0];             // 3.6.0.0 (2004/09/22)
085                    }
086            }
087    
088            /**
089             * 値を文字?で返します?
090             *
091             * @return      こ?クラスの初期エンコードに対応した文字?
092             * @throws UnsupportedEncodingException
093             */
094            public String getStringValue() throws UnsupportedEncodingException {
095                    return getStringValue(encoding);
096            }
097    
098            /**
099             * エンコードを与えて、?を文字?に変換して返します?
100             *
101             * @param       encoding        エンコー?
102             *
103             * @return      エンコードに対応した文字?
104             * @throws UnsupportedEncodingException
105             */
106            public String getStringValue( final String encoding ) throws UnsupportedEncodingException {
107                    return new String(value, encoding);
108            }
109    
110            /**
111             * パラメーターかど?
112             *
113             * @return      (常に true)
114             */
115            @Override
116            public boolean isParam() {
117                    return true;
118            }
119    }