View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.util;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.io.ObjectOutputStream;
21  import java.nio.charset.Charset;
22  
23  /**
24   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
25   *
26   * A holder for a key/value pair that represents a file to upload.
27   *
28   * @author Brad Clarke
29   * @author David D. Kilzer
30   * @author Mike Bowler
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   */
34  public class KeyDataPair extends NameValuePair {
35  
36      private final File fileObject_;
37      private final String fileName_;
38      private final String mimeType_;
39      private transient Charset charset_;
40      private byte[] data_;
41  
42      /**
43       * Creates an instance.
44       *
45       * @param key the key
46       * @param file the file
47       * @param fileName the name of the file
48       * @param mimeType the MIME type
49       * @param charset the charset encoding
50       */
51      public KeyDataPair(final String key, final File file, final String fileName,
52              final String mimeType, final String charset) {
53          this(key, file, fileName, mimeType, Charset.forName(charset));
54      }
55  
56      /**
57       * Creates an instance.
58       *
59       * @param key the key
60       * @param file the file
61       * @param fileName the name of the file
62       * @param mimeType the MIME type
63       * @param charset the charset encoding
64       */
65      public KeyDataPair(final String key, final File file, final String fileName,
66              final String mimeType, final Charset charset) {
67  
68          super(key, (file == null) ? "" : file.getName());
69  
70          if (file != null && file.exists()) {
71              fileObject_ = file;
72          }
73          else {
74              fileObject_ = null;
75          }
76          fileName_ = fileName;
77  
78          mimeType_ = mimeType;
79          charset_ = charset;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public boolean equals(final Object object) {
87          // this is overwritten to make FindBugs happy
88          // and to make it clear, that we really want to have
89          // the same equals semantic like our parent class
90          return super.equals(object);
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public int hashCode() {
98          // this is overwritten to make FindBugs happy
99          // and to make it clear, that we really want to have
100         // the same hashCode like our parent class
101         return super.hashCode();
102     }
103 
104     /**
105      * @return the {@link File} object if the file exists, else {@code null}
106      */
107     public File getFile() {
108         return fileObject_;
109     }
110 
111     /**
112      * @return the fileName
113      */
114     public String getFileName() {
115         return fileName_;
116     }
117 
118     /**
119      * Gets the charset encoding for this file upload.
120      * @return the charset
121      */
122     public Charset getCharset() {
123         return charset_;
124     }
125 
126     /**
127      * Gets the MIME type for this file upload.
128      * @return the MIME type
129      */
130     public String getMimeType() {
131         return mimeType_;
132     }
133 
134     /**
135      * Gets in-memory data assigned to file value.
136      * @return {@code null} if the file content should be used.
137      */
138     public byte[] getData() {
139         return data_;
140     }
141 
142     /**
143      * Sets file value data. If nothing is set, the file content will be used.
144      * @param data byte array with file data.
145      */
146     public void setData(final byte[] data) {
147         data_ = data;
148     }
149 
150     private void writeObject(final ObjectOutputStream oos) throws IOException {
151         oos.defaultWriteObject();
152         oos.writeObject(charset_ == null ? null : charset_.name());
153     }
154 
155     private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
156         ois.defaultReadObject();
157         final String charsetName = (String) ois.readObject();
158         if (charsetName != null) {
159             charset_ = Charset.forName(charsetName);
160         }
161     }
162 }