View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.vfs2.provider.zip;
19  
20  import java.nio.charset.Charset;
21  import java.nio.charset.StandardCharsets;
22  
23  import org.apache.commons.vfs2.FileSystem;
24  import org.apache.commons.vfs2.FileSystemConfigBuilder;
25  import org.apache.commons.vfs2.FileSystemOptions;
26  
27  /**
28   * Configures {@link FileSystemOptions}s.
29   */
30  public class ZipFileSystemConfigBuilder extends FileSystemConfigBuilder {
31  
32      private static final String PREFIX = ZipFileSystemConfigBuilder.class.getName();
33      private static final ZipFileSystemConfigBuilder INSTANCE = new ZipFileSystemConfigBuilder();
34      private static final String KEY_CHARSET = PREFIX + ".charset";
35  
36      /**
37       * Gets the singleton instance.
38       *
39       * @return the singleton instance.
40       */
41      public static final ZipFileSystemConfigBuilder getInstance() {
42          return INSTANCE;
43      }
44  
45      /**
46       * Constructs a new instance.
47       */
48      private ZipFileSystemConfigBuilder() {
49          super("zip.");
50      }
51  
52      /**
53       * Gets the Charset from the FileSystemOptions or {@link StandardCharsets#UTF_8} if absent.
54       *
55       * @param fileSystemOptions The source FileSystemOptions.
56       * @return the Charset from the FileSystemOptions.
57       */
58      public Charset getCharset(final FileSystemOptions fileSystemOptions) {
59          return getParamOrDefault(fileSystemOptions, KEY_CHARSET, StandardCharsets.UTF_8);
60      }
61  
62      @Override
63      protected Class<? extends FileSystem> getConfigClass() {
64          return ZipFileSystem.class;
65      }
66  
67      /**
68       * Sets the Charset in the FileSystemOptions.
69       *
70       * @param fileSystemOptions The target FileSystemOptions.
71       * @param charset The Charset to set.
72       */
73      public void setCharset(final FileSystemOptions fileSystemOptions, final Charset charset) {
74          setParam(fileSystemOptions, KEY_CHARSET, charset);
75      }
76  
77  }