本文整理汇总了Java中org.restlet.representation.Representation.UNKNOWN_SIZE属性的典型用法代码示例。如果您正苦于以下问题:Java Representation.UNKNOWN_SIZE属性的具体用法?Java Representation.UNKNOWN_SIZE怎么用?Java Representation.UNKNOWN_SIZE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.restlet.representation.Representation
的用法示例。
在下文中一共展示了Representation.UNKNOWN_SIZE属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getContentLength
/**
* Returns the content length of the request entity if know, {@link Representation#UNKNOWN_SIZE} otherwise.
*
* @return The request content length.
*/
public static long getContentLength(Series<Header> headers) {
long contentLength = Representation.UNKNOWN_SIZE;
if (headers != null) {
// Extract the content length header
for (Header header : headers) {
if (header.getName().equalsIgnoreCase(
HeaderConstants.HEADER_CONTENT_LENGTH)) {
try {
contentLength = Long.parseLong(header.getValue());
} catch (NumberFormatException e) {
contentLength = Representation.UNKNOWN_SIZE;
}
}
}
}
return contentLength;
}
示例2: getAvailableSize
/**
* Returns the size effectively available. This returns the same value as {@link Representation#getSize()} if no
* range is defined, otherwise it
* returns the size of the range using {@link Range#getSize()}.
*
* @param representation
* The representation to evaluate.
* @return The available size.
*/
public static long getAvailableSize(Representation representation) {
Range range = representation.getRange();
if (range == null || !isBytesRange(range)) {
return representation.getSize();
} else if (range.getSize() != Range.SIZE_MAX) {
if (representation.hasKnownSize()) {
return Math.min(range.getIndex() + range.getSize(),
representation.getSize()) - range.getIndex();
} else {
return Representation.UNKNOWN_SIZE;
}
} else if (representation.hasKnownSize()) {
if (range.getIndex() != Range.INDEX_LAST) {
return representation.getSize() - range.getIndex();
}
return representation.getSize();
}
return Representation.UNKNOWN_SIZE;
}
示例3: getResponseEntity
/**
* Returns the response entity if available. Note that no metadata is
* associated by default, you have to manually set them from your headers.
*
* @param response
* the Response to get the entity from
* @return The response entity if available.
*/
public Representation getResponseEntity(Response response) {
Representation result = null;
// boolean available = false;
long size = Representation.UNKNOWN_SIZE;
// Compute the content length
Series<Header> responseHeaders = getResponseHeaders();
String transferEncoding = responseHeaders.getFirstValue(
HeaderConstants.HEADER_TRANSFER_ENCODING, true);
if ((transferEncoding != null)
&& !Encoding.IDENTITY.getName().equalsIgnoreCase(
transferEncoding)) {
size = Representation.UNKNOWN_SIZE;
} else {
size = getContentLength();
}
if (!getMethod().equals(Method.HEAD.getName())
&& !response.getStatus().isInformational()
&& !response.getStatus()
.equals(Status.REDIRECTION_NOT_MODIFIED)
&& !response.getStatus().equals(Status.SUCCESS_NO_CONTENT)
&& !response.getStatus().equals(Status.SUCCESS_RESET_CONTENT)) {
// Make sure that an InputRepresentation will not be instantiated
// while the stream is closed.
InputStream stream = getUnClosedResponseEntityStream(getResponseEntityStream(size));
if (stream != null) {
result = getRepresentation(stream);
}
}
if (result != null) {
result.setSize(size);
// Informs that the size has not been specified in the header.
if (size == Representation.UNKNOWN_SIZE) {
getLogger()
.debug("The length of the message body is unknown. The entity must be handled carefully and consumed entirely in order to surely release the connection.");
}
}
result = HeaderUtils.extractEntityHeaders(responseHeaders, result);
return result;
}
示例4: addEntityHeaders
/**
* Adds the entity headers based on the {@link Representation} to the {@link Series}.
*
* @param entity
* The source entity {@link Representation}.
* @param headers
* The target headers {@link Series}.
*/
public static void addEntityHeaders(Representation entity,
Series<Header> headers) {
if (entity == null || !entity.isAvailable()) {
addHeader(HeaderConstants.HEADER_CONTENT_LENGTH, "0", headers);
} else if (entity.getAvailableSize() != Representation.UNKNOWN_SIZE) {
addHeader(HeaderConstants.HEADER_CONTENT_LENGTH,
Long.toString(entity.getAvailableSize()), headers);
}
if (entity != null) {
addHeader(HeaderConstants.HEADER_CONTENT_ENCODING,
EncodingWriter.write(entity.getEncodings()), headers);
addHeader(HeaderConstants.HEADER_CONTENT_LANGUAGE,
LanguageWriter.write(entity.getLanguages()), headers);
if (entity.getLocationRef() != null) {
addHeader(HeaderConstants.HEADER_CONTENT_LOCATION, entity
.getLocationRef().getTargetRef().toString(), headers);
}
if (entity.getDigest() != null
&& Digest.ALGORITHM_MD5.equals(entity.getDigest()
.getAlgorithm())) {
addHeader(
HeaderConstants.HEADER_CONTENT_MD5,
new String(org.restlet.engine.util.Base64.encode(entity
.getDigest().getValue(), false)),
headers);
}
if (entity.getRange() != null) {
Range range = entity.getRange();
if (isBytesRange(range)) {
addHeader(HeaderConstants.HEADER_CONTENT_RANGE,
RangeWriter.write(range, entity.getSize()),
headers);
} else {
addHeader(HeaderConstants.HEADER_CONTENT_RANGE,
RangeWriter.write(range, range.getInstanceSize()),
headers);
}
}
if (entity.getMediaType() != null) {
addHeader(HeaderConstants.HEADER_CONTENT_TYPE,
ContentType.writeHeader(entity), headers);
}
if (entity.getExpirationDate() != null) {
addHeader(HeaderConstants.HEADER_EXPIRES,
DateWriter.write(entity.getExpirationDate()), headers);
}
if (entity.getModificationDate() != null) {
addHeader(HeaderConstants.HEADER_LAST_MODIFIED,
DateWriter.write(entity.getModificationDate()), headers);
}
if (entity.getTag() != null) {
addHeader(HeaderConstants.HEADER_ETAG,
TagWriter.write(entity.getTag()), headers);
}
if (entity.getDisposition() != null
&& !Disposition.TYPE_NONE.equals(entity.getDisposition()
.getType())) {
addHeader(HeaderConstants.HEADER_CONTENT_DISPOSITION,
DispositionWriter.write(entity.getDisposition()),
headers);
}
}
}
示例5: write
/**
* Formats {@code range} as a Content-Range header value.
*
* @param range
* Range to format
* @param size
* Total size of the entity
* @return {@code range} formatted
*/
public static String write(Range range, long size) {
StringBuilder b = new StringBuilder(range.getUnitName() + " ");
if (range.getIndex() >= Range.INDEX_FIRST) {
b.append(range.getIndex());
b.append("-");
if (range.getSize() != Range.SIZE_MAX) {
b.append(range.getIndex() + range.getSize() - 1);
} else {
if (size != Representation.UNKNOWN_SIZE) {
b.append(size - 1);
} else {
throw new IllegalArgumentException(
"The entity has an unknown size, can't determine the last byte position.");
}
}
} else if (range.getIndex() == Range.INDEX_LAST) {
if (range.getSize() != Range.SIZE_MAX) {
if (size != Representation.UNKNOWN_SIZE) {
if (range.getSize() <= size) {
b.append(size - range.getSize());
b.append("-");
b.append(size - 1);
} else {
throw new IllegalArgumentException(
"The size of the range ("
+ range.getSize()
+ ") is higher than the size of the entity ("
+ size + ").");
}
} else {
throw new IllegalArgumentException(
"The entity has an unknown size, can't determine the last byte position.");
}
} else {
// This is not a valid range.
throw new IllegalArgumentException(
"The range provides no index and no size, it is invalid.");
}
}
if (size != Representation.UNKNOWN_SIZE) {
b.append("/").append(size);
} else {
b.append("/*");
}
return b.toString();
}
示例6: RangeInputStream
/**
* Constructs a stream exposing only a range of a given source stream.
*
* @param in
* The source input stream.
* @param totalSize
* The total size of the source stream.
* @param range
* The range to satisfy.
*/
public RangeInputStream(InputStream in, long totalSize, Range range) {
super(in);
this.range = range;
this.position = 0;
this.totalSize = totalSize;
this.availableSize = (int) range.getSize();
if (totalSize == Representation.UNKNOWN_SIZE) {
if (range.getIndex() == Range.INDEX_LAST) {
if (range.getSize() == Range.SIZE_MAX) {
// Read the whole stream
this.startIndex = -1;
this.endIndex = -1;
} else {
throw new IllegalArgumentException(
"Can't determine the start and end index.");
}
} else {
if (range.getSize() == Range.SIZE_MAX) {
this.startIndex = range.getIndex();
this.endIndex = -1;
} else {
this.startIndex = range.getIndex();
this.endIndex = range.getIndex() + range.getSize() - 1;
}
}
} else {
if (range.getIndex() == Range.INDEX_LAST) {
if (range.getSize() == Range.SIZE_MAX) {
this.startIndex = -1;
this.endIndex = -1;
} else {
this.startIndex = totalSize - range.getSize();
this.endIndex = -1;
}
} else {
if (range.getSize() == Range.SIZE_MAX) {
this.startIndex = range.getIndex();
this.endIndex = -1;
} else {
this.startIndex = range.getIndex();
this.endIndex = range.getIndex() + range.getSize() - 1;
}
}
}
}
示例7: bufferEntity
/**
* If the entity is transient or its size unknown in advance but available,
* then the entity is wrapped with a
* {@link org.restlet.representation.BufferingRepresentation}.<br>
* <br>
* Be careful as this method could create potentially very large byte
* buffers in memory that could impact your application performance.
*
* @see org.restlet.representation.BufferingRepresentation
* @see ClientResource#setRequestEntityBuffering(boolean)
* @see ClientResource#setResponseEntityBuffering(boolean)
*/
public void bufferEntity() {
if ((getEntity() != null)
&& (getEntity().isTransient() || (getEntity().getSize() == Representation.UNKNOWN_SIZE))
&& getEntity().isAvailable()) {
setEntity(new org.restlet.representation.BufferingRepresentation(
getEntity()));
}
}