001/** 002 * 003 * Copyright © 2017 Paul Schaub 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingle_filetransfer.element; 018 019import org.jivesoftware.smack.packet.NamedElement; 020import org.jivesoftware.smack.util.XmlStringBuilder; 021import org.jivesoftware.smackx.hashes.element.HashElement; 022 023/** 024 * RangeElement which specifies, which range of a file shall be transferred. 025 */ 026public class Range implements NamedElement { 027 028 public static final String ELEMENT = "range"; 029 public static final String ATTR_OFFSET = "offset"; 030 public static final String ATTR_LENGTH = "length"; 031 032 private final int offset, length; 033 private final HashElement hash; 034 035 /** 036 * Create a Range element with default values. 037 */ 038 public Range() { 039 this(0, -1, null); 040 } 041 042 /** 043 * Create a Range element with specified length. 044 * @param length length of the transmitted data in bytes. 045 */ 046 public Range(int length) { 047 this(0, length, null); 048 } 049 050 /** 051 * Create a Range element with specified offset and length. 052 * @param offset offset in bytes from the beginning of the transmitted data. 053 * @param length number of bytes that shall be transferred. 054 */ 055 public Range(int offset, int length) { 056 this(offset, length, null); 057 } 058 059 /** 060 * Create a Range element with specified offset, length and hash. 061 * @param offset offset in bytes from the beginning of the transmitted data. 062 * @param length number of bytes that shall be transferred. 063 * @param hash hash of the bytes in the specified range. 064 */ 065 public Range(int offset, int length, HashElement hash) { 066 this.offset = offset; 067 this.length = length; 068 this.hash = hash; 069 } 070 071 /** 072 * Return the index of the offset. 073 * This marks the begin of the specified range. 074 * @return offset 075 */ 076 public int getOffset() { 077 return offset; 078 } 079 080 /** 081 * Return the length of the range. 082 * @return length 083 */ 084 public int getLength() { 085 return length; 086 } 087 088 /** 089 * Return the hash element that contains a checksum of the bytes specified in the range. 090 * @return hash element 091 */ 092 public HashElement getHash() { 093 return hash; 094 } 095 096 @Override 097 public String getElementName() { 098 return ELEMENT; 099 } 100 101 @Override 102 public CharSequence toXML(String enclosingNamespace) { 103 XmlStringBuilder sb = new XmlStringBuilder(this); 104 105 if (offset > 0) { 106 sb.attribute(ATTR_OFFSET, offset); 107 } 108 if (length > 0) { 109 sb.attribute(ATTR_LENGTH, length); 110 } 111 112 if (hash != null) { 113 sb.rightAngleBracket(); 114 sb.element(hash); 115 sb.closeElement(this); 116 } else { 117 sb.closeEmptyElement(); 118 } 119 return sb; 120 } 121 122 @Override 123 public boolean equals(Object other) { 124 if (other == null || !(other instanceof Range)) { 125 return false; 126 } 127 128 return this.hashCode() == other.hashCode(); 129 } 130 131 @Override 132 public int hashCode() { 133 return toXML(null).toString().hashCode(); 134 } 135}