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 java.io.ByteArrayOutputStream;
019import java.io.IOException;
020import java.io.UnsupportedEncodingException;
021
022import jakarta.servlet.ServletInputStream;
023
024import org.opengion.fukurou.system.Closer ;
025
026/**
027 * ファイルアップロード時のマルチパート処理のパラメータパート部品です。
028 *
029 * パラメータ情報を取り扱います。
030 *
031 * @og.group その他機能
032 *
033 * @version  4.0
034 * @author   Kazuhiko Hasegawa
035 * @since    JDK5.0,
036 */
037// 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない) class を final にすれば、警告は消える。
038// public class ParamPart extends Part {
039public final class ParamPart extends Part {
040        // 8.5.4.2 (2024/01/12) PMD 7.0.0 ImmutableField 対応
041        private final byte[] value;
042        private final String encoding;
043
044        /**
045         * パラメータパート部品 オブジェクトを構築する、コンストラクター
046         *
047         * @param       name            パラメータの名前
048         * @param       in                      ServletInputStreamオブジェクト
049         * @param       boundary        境界文字
050         * @param       encoding        エンコード
051         * @throws IOException 入出力エラーが発生したとき
052         */
053        /* default */ ParamPart( final String name, final ServletInputStream in,
054                                final String boundary, final String encoding) throws IOException {
055                super(name);
056                this.encoding = encoding;
057
058                // Copy the part's contents into a byte array
059
060                PartInputStream pis = null;
061                ByteArrayOutputStream baos = null;
062                try {
063                        pis  = new PartInputStream(in, boundary);
064                        baos = new ByteArrayOutputStream(512);
065                        final byte[] buf = new byte[128];
066                        int read;
067                        while( (read = pis.read(buf)) != -1 ) {
068                                baos.write(buf, 0, read);
069                        }
070                        value = baos.toByteArray();
071                }
072                finally {
073                        Closer.ioClose( pis );          // 4.0.0 (2006/01/31) close 処理時の IOException を無視
074                        Closer.ioClose( baos );         // 4.0.0 (2006/01/31) close 処理時の IOException を無視
075                }
076        }
077
078        /**
079         * 値をバイト配列で返します。
080         *
081         * @return  値のバイト配列
082         * @og.rtnNotNull
083         */
084        public byte[] getValue() {
085                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
086                // 反転注意
087                return value == null ? new byte[0] : value.clone();
088        }
089
090        /**
091         * 値を文字列で返します。
092         *
093         * @return      このクラスの初期エンコードに対応した文字列
094         * @og.rtnNotNull
095         * @throws UnsupportedEncodingException コンストラクタで指定した エンコード がサポートされていない場合。
096         */
097        public String getStringValue() throws UnsupportedEncodingException {
098                return getStringValue( encoding );
099        }
100
101        /**
102         * エンコードを与えて、値を文字列に変換して返します。
103         *
104         * @param       encoding        エンコード
105         *
106         * @return      エンコードに対応した文字列
107         * @og.rtnNotNull
108         * @throws UnsupportedEncodingException 引数のエンコード がサポートされていない場合。
109         */
110        public String getStringValue( final String encoding ) throws UnsupportedEncodingException {
111                return new String( value, encoding );
112        }
113
114        /**
115         * パラメーターかどうか。
116         *
117         * @return      (常に true)
118         */
119        @Override
120        public boolean isParam() {
121                return true;
122        }
123}