001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.build; 019 020import java.nio.charset.Charset; 021 022import org.apache.commons.io.Charsets; 023import org.apache.commons.io.IOUtils; 024 025/** 026 * Abstracts building a typed instance of {@code T}. 027 * 028 * @param <T> the type of instances to build. 029 * @param <B> the type of builder subclass. 030 * @since 2.12.0 031 */ 032public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T, B>> extends AbstractOriginSupplier<T, B> { 033 034 /** 035 * The buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 036 */ 037 private int bufferSize = IOUtils.DEFAULT_BUFFER_SIZE; 038 039 /** 040 * The buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 041 */ 042 private int bufferSizeDefault = IOUtils.DEFAULT_BUFFER_SIZE; 043 044 /** 045 * The Charset, defaults to {@link Charset#defaultCharset()}. 046 */ 047 private Charset charset = Charset.defaultCharset(); 048 049 /** 050 * The Charset, defaults to {@link Charset#defaultCharset()}. 051 */ 052 private Charset charsetDefault = Charset.defaultCharset(); 053 054 /** 055 * Gets the buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 056 * 057 * @return the buffer size, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 058 */ 059 protected int getBufferSize() { 060 return bufferSize; 061 } 062 063 /** 064 * Gets the buffer size default, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 065 * 066 * @return the buffer size default, defaults to {@link IOUtils#DEFAULT_BUFFER_SIZE} ({@value IOUtils#DEFAULT_BUFFER_SIZE}). 067 */ 068 protected int getBufferSizeDefault() { 069 return bufferSizeDefault; 070 } 071 072 /** 073 * Gets the Charset, defaults to {@link Charset#defaultCharset()}. 074 * 075 * @return the Charset, defaults to {@link Charset#defaultCharset()}. 076 */ 077 protected Charset getCharset() { 078 return charset; 079 } 080 081 /** 082 * Gets the Charset default, defaults to {@link Charset#defaultCharset()}. 083 * 084 * @return the Charset default, defaults to {@link Charset#defaultCharset()}. 085 */ 086 protected Charset getCharsetDefault() { 087 return charsetDefault; 088 } 089 090 /** 091 * Sets the buffer size. 092 * <p> 093 * Subclasses may ignore this setting. 094 * </p> 095 * 096 * @param bufferSize the buffer size. 097 * @return this. 098 */ 099 public B setBufferSize(final int bufferSize) { 100 this.bufferSize = bufferSize >= 0 ? bufferSize : bufferSizeDefault; 101 return asThis(); 102 } 103 104 /** 105 * Sets the buffer size. 106 * <p> 107 * Subclasses may ignore this setting. 108 * </p> 109 * 110 * @param bufferSize the buffer size, null resets to the default. 111 * @return this. 112 */ 113 public B setBufferSize(final Integer bufferSize) { 114 setBufferSize(bufferSize != null ? bufferSize : bufferSizeDefault); 115 return asThis(); 116 } 117 118 /** 119 * Sets the buffer size for subclasses to initialize. 120 * <p> 121 * Subclasses may ignore this setting. 122 * </p> 123 * 124 * @param bufferSizeDefault the buffer size, null resets to the default. 125 * @return this. 126 */ 127 protected B setBufferSizeDefault(final int bufferSizeDefault) { 128 this.bufferSizeDefault = bufferSizeDefault; 129 return asThis(); 130 } 131 132 /** 133 * Sets the Charset. 134 * <p> 135 * Subclasses may ignore this setting. 136 * </p> 137 * 138 * @param charset the Charset, null resets to the default. 139 * @return this. 140 */ 141 public B setCharset(final Charset charset) { 142 this.charset = Charsets.toCharset(charset, charsetDefault); 143 return asThis(); 144 } 145 146 /** 147 * Sets the Charset. 148 * <p> 149 * Subclasses may ignore this setting. 150 * </p> 151 * 152 * @param charset the Charset name, null resets to the default. 153 * @return this. 154 */ 155 public B setCharset(final String charset) { 156 return setCharset(Charsets.toCharset(charset, charsetDefault)); 157 } 158 159 /** 160 * Sets the Charset default for subclasses to initialize. 161 * <p> 162 * Subclasses may ignore this setting. 163 * </p> 164 * 165 * @param defaultCharset the Charset name, null resets to the default. 166 * @return this. 167 */ 168 protected B setCharsetDefault(final Charset defaultCharset) { 169 this.charsetDefault = defaultCharset; 170 return asThis(); 171 } 172}