티스토리 뷰
728x90
스프링 프레임워크 5.1부터 추가된 org.springframework.DataSize 클래스
DataSize를 표현하는데 사용하는 클래스이다.
웹개발 하다보면 spring.servlet.multipart.max-file-size=200MB 이런 설정을 볼 수 있는데 그때 사용되는 클래스이다.
스프링부트에서는 컨버터를 지원한다.
- StringToDataSizeConverter
- NumberToDataSizeConverter
1MB 같은 설정을 String으로 설정해줬으면 Byte로 1,048,576 으로 바꿔주는 역할을 한다. 말그대로 Converter
스프링부트가 실행되면서 autoconfiguration 으로 자동으로 다 등록해준다..
org.springframework.DataSize 클래스
@SuppressWarnings("serial")
public final class DataSize implements Comparable<DataSize>, Serializable {
/**
* The pattern for parsing.
*/
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
/**
* Bytes per Kilobyte.
*/
private static final long BYTES_PER_KB = 1024;
/**
* Bytes per Megabyte.
*/
private static final long BYTES_PER_MB = BYTES_PER_KB * 1024;
/**
* Bytes per Gigabyte.
*/
private static final long BYTES_PER_GB = BYTES_PER_MB * 1024;
/**
* Bytes per Terabyte.
*/
private static final long BYTES_PER_TB = BYTES_PER_GB * 1024;
private final long bytes;
private DataSize(long bytes) {
this.bytes = bytes;
}
/**
* Obtain a {@link DataSize} representing the specified number of bytes.
* @param bytes the number of bytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofBytes(long bytes) {
return new DataSize(bytes);
}
/**
* Obtain a {@link DataSize} representing the specified number of kilobytes.
* @param kilobytes the number of kilobytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofKilobytes(long kilobytes) {
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
}
/**
* Obtain a {@link DataSize} representing the specified number of megabytes.
* @param megabytes the number of megabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofMegabytes(long megabytes) {
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
}
/**
* Obtain a {@link DataSize} representing the specified number of gigabytes.
* @param gigabytes the number of gigabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofGigabytes(long gigabytes) {
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
}
/**
* Obtain a {@link DataSize} representing the specified number of terabytes.
* @param terabytes the number of terabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofTerabytes(long terabytes) {
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
}
/**
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
* @param amount the amount of the size, measured in terms of the unit,
* positive or negative
* @return a corresponding {@link DataSize}
*/
public static DataSize of(long amount, DataUnit unit) {
Assert.notNull(unit, "Unit must not be null");
return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
}
/**
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
* {@link DataUnit#BYTES} if no unit is specified.
* <p>
* Examples:
* <pre>
* "12KB" -- parses as "12 kilobytes"
* "5MB" -- parses as "5 megabytes"
* "20" -- parses as "20 bytes"
* </pre>
* @param text the text to parse
* @return the parsed {@link DataSize}
* @see #parse(CharSequence, DataUnit)
*/
public static DataSize parse(CharSequence text) {
return parse(text, null);
}
/**
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
* the specified default {@link DataUnit} if no unit is specified.
* <p>
* The string starts with a number followed optionally by a unit matching one of the
* supported {@linkplain DataUnit suffixes}.
* <p>
* Examples:
* <pre>
* "12KB" -- parses as "12 kilobytes"
* "5MB" -- parses as "5 megabytes"
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES})
* </pre>
* @param text the text to parse
* @return the parsed {@link DataSize}
*/
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
Assert.notNull(text, "Text must not be null");
try {
Matcher matcher = PATTERN.matcher(text);
Assert.state(matcher.matches(), "Does not match data size pattern");
DataUnit unit = determineDataUnit(matcher.group(2), defaultUnit);
long amount = Long.parseLong(matcher.group(1));
return DataSize.of(amount, unit);
}
catch (Exception ex) {
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
}
}
private static DataUnit determineDataUnit(String suffix, @Nullable DataUnit defaultUnit) {
DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit.BYTES);
return (StringUtils.hasLength(suffix) ? DataUnit.fromSuffix(suffix) : defaultUnitToUse);
}
/**
* Checks if this size is negative, excluding zero.
* @return true if this size has a size less than zero bytes
*/
public boolean isNegative() {
return this.bytes < 0;
}
/**
* Return the number of bytes in this instance.
* @return the number of bytes
*/
public long toBytes() {
return this.bytes;
}
/**
* Return the number of kilobytes in this instance.
* @return the number of kilobytes
*/
public long toKilobytes() {
return this.bytes / BYTES_PER_KB;
}
/**
* Return the number of megabytes in this instance.
* @return the number of megabytes
*/
public long toMegabytes() {
return this.bytes / BYTES_PER_MB;
}
/**
* Return the number of gigabytes in this instance.
* @return the number of gigabytes
*/
public long toGigabytes() {
return this.bytes / BYTES_PER_GB;
}
/**
* Return the number of terabytes in this instance.
* @return the number of terabytes
*/
public long toTerabytes() {
return this.bytes / BYTES_PER_TB;
}
@Override
public int compareTo(DataSize other) {
return Long.compare(this.bytes, other.bytes);
}
@Override
public String toString() {
return String.format("%dB", this.bytes);
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
DataSize otherSize = (DataSize) other;
return (this.bytes == otherSize.bytes);
}
@Override
public int hashCode() {
return Long.hashCode(this.bytes);
}
}
StringToDataSizeConverter
final class StringToDataSizeConverter implements GenericConverter {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, DataSize.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (ObjectUtils.isEmpty(source)) {
return null;
}
return convert(source.toString(), getDataUnit(targetType));
}
private DataUnit getDataUnit(TypeDescriptor targetType) {
DataSizeUnit annotation = targetType.getAnnotation(DataSizeUnit.class);
return (annotation != null) ? annotation.value() : null;
}
private DataSize convert(String source, DataUnit unit) {
return DataSize.parse(source, unit);
}
}
NumberToDataSizeConverter
final class NumberToDataSizeConverter implements GenericConverter {
private final StringToDataSizeConverter delegate = new StringToDataSizeConverter();
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Number.class, DataSize.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.delegate.convert((source != null) ? source.toString() : null, TypeDescriptor.valueOf(String.class),
targetType);
}
}
참고
https://futurecreator.github.io/2018/11/02/spring-boot-2-1-0-release/
728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- oracle
- Spring Security
- Github Status
- Linux
- maven
- LocalDate
- 베리 심플
- Bash tab
- LocalDateTime
- docker
- Spring
- localtime
- mybatis
- config-location
- elasticsearch
- intellij
- mybatis config
- JavaScript
- springboot
- jQuery
- rocky
- 북리뷰
- Mac
- 오라클
- Kotlin
- svn
- Java
- window
- k8s
- input
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함