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.system.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 final 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 * @og.rtnNotNull 079 */ 080 public byte[] getValue() { 081 // 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 082 // 反転注意 083 return value == null ? new byte[0] : value.clone(); 084 085 } 086 087 /** 088 * 値を文字列で返します。 089 * 090 * @return このクラスの初期エンコードに対応した文字列 091 * @og.rtnNotNull 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 * @og.rtnNotNull 105 * @throws UnsupportedEncodingException 引数のエンコード がサポートされていない場合。 106 */ 107 public String getStringValue( final String encoding ) throws UnsupportedEncodingException { 108 return new String( value, encoding ); 109 } 110 111 /** 112 * パラメーターかどうか。 113 * 114 * @return (常に true) 115 */ 116 @Override 117 public boolean isParam() { 118 return true; 119 } 120}