2017-06-30 15:03:13 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-05-06 22:06:13 +02:00
|
|
|
* Copyright © 2017 Paul Schaub, 2019 Florian Schmaus
|
2017-06-30 15:03:13 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.jingle_filetransfer.element;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.NamedElement;
|
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
2019-05-06 22:06:13 +02:00
|
|
|
|
2017-06-30 15:03:13 +02:00
|
|
|
import org.jivesoftware.smackx.hashes.element.HashElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RangeElement which specifies, which range of a file shall be transferred.
|
|
|
|
*/
|
|
|
|
public class Range implements NamedElement {
|
|
|
|
|
|
|
|
public static final String ELEMENT = "range";
|
|
|
|
public static final String ATTR_OFFSET = "offset";
|
|
|
|
public static final String ATTR_LENGTH = "length";
|
|
|
|
|
2019-05-06 22:06:13 +02:00
|
|
|
private final Integer offset, length;
|
2017-06-30 15:03:13 +02:00
|
|
|
private final HashElement hash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Range element with default values.
|
|
|
|
*/
|
|
|
|
public Range() {
|
2019-05-06 22:06:13 +02:00
|
|
|
this(null, null, null);
|
2017-06-30 15:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Range element with specified length.
|
|
|
|
* @param length length of the transmitted data in bytes.
|
|
|
|
*/
|
|
|
|
public Range(int length) {
|
2019-05-06 22:06:13 +02:00
|
|
|
this(null, length, null);
|
2017-06-30 15:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Range element with specified offset and length.
|
|
|
|
* @param offset offset in bytes from the beginning of the transmitted data.
|
|
|
|
* @param length number of bytes that shall be transferred.
|
|
|
|
*/
|
|
|
|
public Range(int offset, int length) {
|
|
|
|
this(offset, length, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Range element with specified offset, length and hash.
|
|
|
|
* @param offset offset in bytes from the beginning of the transmitted data.
|
|
|
|
* @param length number of bytes that shall be transferred.
|
|
|
|
* @param hash hash of the bytes in the specified range.
|
|
|
|
*/
|
2019-05-06 22:06:13 +02:00
|
|
|
public Range(Integer offset, Integer length, HashElement hash) {
|
2017-06-30 15:03:13 +02:00
|
|
|
this.offset = offset;
|
|
|
|
this.length = length;
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the index of the offset.
|
|
|
|
* This marks the begin of the specified range.
|
|
|
|
* @return offset
|
|
|
|
*/
|
|
|
|
public int getOffset() {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the length of the range.
|
|
|
|
* @return length
|
|
|
|
*/
|
|
|
|
public int getLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the hash element that contains a checksum of the bytes specified in the range.
|
|
|
|
* @return hash element
|
|
|
|
*/
|
|
|
|
public HashElement getHash() {
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getElementName() {
|
|
|
|
return ELEMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-04 13:27:41 +01:00
|
|
|
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
|
2017-06-30 15:03:13 +02:00
|
|
|
XmlStringBuilder sb = new XmlStringBuilder(this);
|
|
|
|
|
2019-05-06 22:06:13 +02:00
|
|
|
// TODO: (Introduce and) use XmlStringBuilder.optXmlAttribute(name, Number).
|
|
|
|
if (offset != null) {
|
2017-06-30 15:03:13 +02:00
|
|
|
sb.attribute(ATTR_OFFSET, offset);
|
|
|
|
}
|
2019-05-06 22:06:13 +02:00
|
|
|
if (length != null) {
|
2017-06-30 15:03:13 +02:00
|
|
|
sb.attribute(ATTR_LENGTH, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash != null) {
|
|
|
|
sb.rightAngleBracket();
|
|
|
|
sb.element(hash);
|
|
|
|
sb.closeElement(this);
|
|
|
|
} else {
|
|
|
|
sb.closeEmptyElement();
|
|
|
|
}
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (other == null || !(other instanceof Range)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:06:31 +01:00
|
|
|
Range otherRange = (Range) other;
|
|
|
|
return this.getOffset() == otherRange.getOffset() &&
|
|
|
|
this.getLength() == otherRange.getLength() &&
|
|
|
|
this.getHash().equals(otherRange.getHash());
|
2017-06-30 15:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2019-02-04 13:27:41 +01:00
|
|
|
return toXML().toString().hashCode();
|
2017-06-30 15:03:13 +02:00
|
|
|
}
|
|
|
|
}
|