001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.io.support;
018
019import org.springframework.core.io.Resource;
020import org.springframework.util.Assert;
021
022/**
023 * Region of a {@link Resource} implementation, materialized by a {@code position}
024 * within the {@link Resource} and a byte {@code count} for the length of that region.
025 *
026 * @author Arjen Poutsma
027 * @since 4.3
028 */
029public class ResourceRegion {
030
031        private final Resource resource;
032
033        private final long position;
034
035        private final long count;
036
037
038        /**
039         * Create a new {@code ResourceRegion} from a given {@link Resource}.
040         * This region of a resource is represented by a start {@code position}
041         * and a byte {@code count} within the given {@code Resource}.
042         * @param resource a Resource
043         * @param position the start position of the region in that resource
044         * @param count the byte count of the region in that resource
045         */
046        public ResourceRegion(Resource resource, long position, long count) {
047                Assert.notNull(resource, "Resource must not be null");
048                Assert.isTrue(position >= 0, "'position' must be larger than or equal to 0");
049                Assert.isTrue(count >= 0, "'count' must be larger than or equal to 0");
050                this.resource = resource;
051                this.position = position;
052                this.count = count;
053        }
054
055
056        /**
057         * Return the underlying {@link Resource} for this {@code ResourceRegion}.
058         */
059        public Resource getResource() {
060                return this.resource;
061        }
062
063        /**
064         * Return the start position of this region in the underlying {@link Resource}.
065         */
066        public long getPosition() {
067                return this.position;
068        }
069
070        /**
071         * Return the byte count of this region in the underlying {@link Resource}.
072         */
073        public long getCount() {
074                return this.count;
075        }
076
077}