View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.conversion;
2   
3   import java.nio.charset.Charset;
4   
5   import com.github.mygreen.supercsv.util.ArgUtils;
6   
7   /**
8    * 文字数をバイトサイズでカウントして、パディングする。
9    * <p>バイト数は、エンコードによって変わるため、環境によってこのクラスを継承しクラスを使用してください。</p>
10   *
11   * @since 2.1
12   * @author T.TSUCHIE
13   *
14   */
15  public abstract class ByteSizePaddingProcessor extends AbstractPaddingOperator {
16      
17      private final Charset charset;
18      
19      /**
20       * 文字コードを指定するコンストラクタ
21       * @param charset 文字コード
22       * @throws NullPointerException {@literal charset is null.}
23       */
24      public ByteSizePaddingProcessor(final Charset charset) {
25          ArgUtils.notNull(charset, "charset");
26          this.charset = charset;
27      }
28      
29      @Override
30      public int count(int codePoint) {
31          return count(String.valueOf(Character.toChars(codePoint)));
32      }
33      
34      @Override
35      public int count(final String text) {
36          ArgUtils.notNull(text, "text");
37          
38          return text.getBytes(charset).length;
39      }
40      
41      /**
42       * UTF-8でエンコードしたバイト数をカウントして、パディングする。
43       */
44      public static class Utf8 extends ByteSizePaddingProcessor {
45          
46          public Utf8() {
47              super(Charset.forName("UTF-8"));
48          }
49          
50      };
51      
52      /**
53       * Windows-31j(CP932)でエンコードしたバイト数をカウントして、パディングする。
54       */
55      public static class Windows31j extends ByteSizePaddingProcessor {
56          
57          public Windows31j() {
58              super(Charset.forName("Windows-31j"));
59          }
60          
61      };
62      
63      /**
64       * EUC-JPでエンコードしたバイト数をカウントして、パディングする。
65       */
66      public static class EucJp extends ByteSizePaddingProcessor {
67          
68          public EucJp() {
69              super(Charset.forName("EUC-JP"));
70          }
71          
72      };
73  
74  }