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 */
016package org.opengion.hayabusa.servlet.multipart;
017
018import org.opengion.fukurou.util.Closer ;
019
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.UnsupportedEncodingException;
023import 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 */
036public 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}