View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.transfer;
21  
22  public class DefaultSegment implements Segment {
23  
24  	private int availability;
25  	private long start;
26  	private long end;
27  	private long transferred;
28  	private long total;
29  
30  
31  	/***
32  	 *
33  	 * @param start the start offset of the segment in byte
34  	 * @param end the end offset of the segment in byte
35  	 * @param transferred the number of bytes transferred
36  	 */
37  	public DefaultSegment(long total, long start, long end, long transferred,
38  						  int availability) 
39  	{
40  		this.total = total;
41  		this.start = start;
42  		this.end = end;
43  		this.transferred = transferred;
44  		this.availability = availability;
45  	}
46  
47  	/***
48  	 *
49  	 * @param start the start offset of the segment in byte
50  	 * @param end the end offset of the segment in byte
51  	 * @param transferred the number of bytes transferred
52  	 */
53  	public DefaultSegment(long start, long end, long transferred, 
54  						  int availability) 
55  	{
56  		this(0, start, end, transferred, availability);
57  	}
58  
59  	public DefaultSegment(long total)
60  	{
61  		this(total, 0, 0, 0, 0);
62  	}
63  
64  	public void commit(int transferred)
65  	{
66  		if (start + this.transferred + transferred > end) {
67  			this.transferred = end - start;
68  		}
69  		else {
70  			this.transferred += transferred;
71  		}
72  	}
73  
74  	public int getAvailability()
75  	{
76  		return availability;
77  	}
78  
79  	/***
80  	 * @return the end
81  	 */
82  	public long getEnd() 
83  	{
84  		return end;
85  	}
86  
87  	/***
88  	 * @return the start
89  	 */
90  	public long getStart() 
91  	{
92  		return start;
93  	}
94  
95  	/***
96  	 * @return the total byte
97  	 */
98  	public long getTotal() 
99  	{
100 		return total;
101 	}
102 
103 	/***
104 	 * @return transferred bytes
105 	 */
106 	public long getTransferred() 
107 	{
108 		return transferred;
109 	}
110 
111 	public void setAvailability(int avail)
112 	{
113 		availability = avail;
114 	}
115 
116 	/***
117 	 * @param l
118 	 */
119 	public void setEnd(long l) 
120 	{
121 		end = l;
122 	}
123 
124 	/***
125 	 * @param l
126 	 */
127 	public void setStart(long l) 
128 	{
129 		start = l;
130 	}
131 
132 	public void setTotal(long l)
133 	{
134 		total = l;
135 	}
136 
137 	/***
138 	 * @param l
139 	 */
140 	public void setTransferred(long l) 
141 	{
142 		transferred = l;
143 	}
144 
145 }