/*
This file is part of Airsonic.
Airsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Airsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see
null
if the syntax
* of the given range is not supported.
*/
public static HttpRange valueOf(String range) {
if (range == null) {
return null;
}
Matcher matcher = PATTERN.matcher(range);
if (matcher.matches()) {
String firstString = matcher.group(1);
String lastString = StringUtils.trimToNull(matcher.group(2));
long first = Long.parseLong(firstString);
Long last = lastString == null ? null : Long.parseLong(lastString);
if (last != null && first > last) {
return null;
}
return new HttpRange(first, last);
}
return null;
}
public HttpRange(long firstBytePos, Long lastBytePos) {
this.firstBytePos = firstBytePos;
this.lastBytePos = lastBytePos;
}
/**
* @return The first byte position (inclusive) in the range. Never {@code null}.
*/
public Long getFirstBytePos() {
return firstBytePos;
}
/**
* @return The last byte position (inclusive) in the range. Can be {@code null}.
*/
public Long getLastBytePos() {
return lastBytePos;
}
/**
* @return Whether this is a closed range (both first and last byte position specified).
*/
public boolean isClosed() {
return firstBytePos != null && lastBytePos != null;
}
/**
* @return The size in bytes if the range is closed, -1 otherwise.
*/
public long size() {
return isClosed() ? (lastBytePos - firstBytePos + 1) : -1;
}
/**
* @return Returns whether the given byte position is within this range.
*/
public boolean contains(long pos) {
if (pos < firstBytePos) {
return false;
}
return lastBytePos == null || pos <= lastBytePos;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(firstBytePos).append('-');
if (lastBytePos != null) {
builder.append(lastBytePos);
}
return builder.toString();
}
}