Java Code Examples for org.springframework.util.unit.DataSize#toBytes()

The following examples show how to use org.springframework.util.unit.DataSize#toBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MaxDataSizeValidator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(DataSize value, ConstraintValidatorContext context) {
	// null values are valid
	if (value == null) {
		return true;
	}
	return value.toBytes() <= maxValue;
}
 
Example 2
Source File: JobMonitorServiceImpl.java    From genie with Apache License 2.0 4 votes vote down vote up
private void checkFilesSize(final Path jobDirectory) {
    final DirectoryManifest manifest;
    try {
        manifest = this.manifestCreatorService.getDirectoryManifest(jobDirectory);
    } catch (IOException e) {
        log.warn("Failed to obtain manifest: {}" + e.getMessage());
        return;
    }

    final int files = manifest.getNumFiles();
    final int maxFiles = this.properties.getMaxFiles();
    if (files > maxFiles) {
        log.error("Limit exceeded, too many files: {}/{}", files, maxFiles);
        this.killService.kill(KillService.KillSource.FILES_LIMIT);
        return;
    }

    final DataSize totalSize = DataSize.ofBytes(manifest.getTotalSizeOfFiles());
    final DataSize maxTotalSize = this.properties.getMaxTotalSize();
    if (totalSize.toBytes() > maxTotalSize.toBytes()) {
        log.error("Limit exceeded, job directory too large: {}/{}", totalSize, maxTotalSize);
        this.killService.kill(KillService.KillSource.FILES_LIMIT);
        return;
    }

    final Optional<DirectoryManifest.ManifestEntry> largestFile = manifest.getFiles()
        .stream()
        .max(Comparator.comparing(DirectoryManifest.ManifestEntry::getSize));

    if (largestFile.isPresent()) {
        final DataSize largestFileSize = DataSize.ofBytes(largestFile.get().getSize());
        final DataSize maxFileSize = this.properties.getMaxFileSize();
        if (largestFileSize.toBytes() > maxFileSize.toBytes()) {
            log.error(
                "Limit exceeded, file too large: {}/{} ({})",
                largestFileSize,
                maxFileSize,
                largestFile.get().getPath()
            );
            this.killService.kill(KillService.KillSource.FILES_LIMIT);
            return;
        }
    }

    log.debug("No files limit exceeded");
}
 
Example 3
Source File: GrpcServerProperties.java    From grpc-spring-boot-starter with MIT License 3 votes vote down vote up
/**
 * Sets the maximum message size allowed to be received by the server. If not set ({@code null}) then it will
 * default to {@link GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE gRPC's default}. If set to {@code -1} then it will use the
 * highest possible limit (not recommended).
 *
 * @param maxInboundMessageSize The new maximum size allowed for incoming messages. {@code -1} for max possible.
 *        Null to use the gRPC's default.
 *
 * @see ServerBuilder#maxInboundMessageSize(int)
 */
public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
    if (maxInboundMessageSize == null || maxInboundMessageSize.toBytes() >= 0) {
        this.maxInboundMessageSize = maxInboundMessageSize;
    } else if (maxInboundMessageSize.toBytes() == -1) {
        this.maxInboundMessageSize = DataSize.ofBytes(Integer.MAX_VALUE);
    } else {
        throw new IllegalArgumentException("Unsupported maxInboundMessageSize: " + maxInboundMessageSize);
    }
}
 
Example 4
Source File: GrpcChannelProperties.java    From grpc-spring-boot-starter with MIT License 3 votes vote down vote up
/**
 * Sets the maximum message size in bytes allowed to be received by the channel. If not set ({@code null}) then it
 * will default to {@link GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE gRPC's default}. If set to {@code -1} then it will use
 * the highest possible limit (not recommended).
 *
 * @param maxInboundMessageSize The new maximum size in bytes allowed for incoming messages. {@code -1} for max
 *        possible. Null to use the gRPC's default.
 *
 * @see ManagedChannelBuilder#maxInboundMessageSize(int)
 */
public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
    if (maxInboundMessageSize == null || maxInboundMessageSize.toBytes() >= 0) {
        this.maxInboundMessageSize = maxInboundMessageSize;
    } else if (maxInboundMessageSize.toBytes() == -1) {
        this.maxInboundMessageSize = DataSize.ofBytes(Integer.MAX_VALUE);
    } else {
        throw new IllegalArgumentException("Unsupported maxInboundMessageSize: " + maxInboundMessageSize);
    }
}
 
Example 5
Source File: GrpcServerProperties.java    From grpc-spring-boot-starter with MIT License 3 votes vote down vote up
/**
 * Sets the maximum message size allowed to be received by the server. If not set ({@code null}) then it will
 * default to {@link GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE gRPC's default}. If set to {@code -1} then it will use the
 * highest possible limit (not recommended).
 *
 * @param maxInboundMessageSize The new maximum size allowed for incoming messages. {@code -1} for max possible.
 *        Null to use the gRPC's default.
 *
 * @see ServerBuilder#maxInboundMessageSize(int)
 */
public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
    if (maxInboundMessageSize == null || maxInboundMessageSize.toBytes() >= 0) {
        this.maxInboundMessageSize = maxInboundMessageSize;
    } else if (maxInboundMessageSize.toBytes() == -1) {
        this.maxInboundMessageSize = DataSize.ofBytes(Integer.MAX_VALUE);
    } else {
        throw new IllegalArgumentException("Unsupported maxInboundMessageSize: " + maxInboundMessageSize);
    }
}
 
Example 6
Source File: GrpcChannelProperties.java    From grpc-spring-boot-starter with MIT License 3 votes vote down vote up
/**
 * Sets the maximum message size in bytes allowed to be received by the channel. If not set ({@code null}) then it
 * will default to {@link GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE gRPC's default}. If set to {@code -1} then it will use
 * the highest possible limit (not recommended).
 *
 * @param maxInboundMessageSize The new maximum size in bytes allowed for incoming messages. {@code -1} for max
 *        possible. Null to use the gRPC's default.
 *
 * @see ManagedChannelBuilder#maxInboundMessageSize(int)
 */
public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) {
    if (maxInboundMessageSize == null || maxInboundMessageSize.toBytes() >= 0) {
        this.maxInboundMessageSize = maxInboundMessageSize;
    } else if (maxInboundMessageSize.toBytes() == -1) {
        this.maxInboundMessageSize = DataSize.ofBytes(Integer.MAX_VALUE);
    } else {
        throw new IllegalArgumentException("Unsupported maxInboundMessageSize: " + maxInboundMessageSize);
    }
}