001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.downloader.impl;
038:
039: import org.netbeans.installer.downloader.Pumping.Section;
040:
041: import java.util.Collections;
042: import java.util.List;
043: import org.netbeans.installer.utils.helper.Pair;
044: import org.netbeans.installer.utils.xml.DomExternalizable;
045: import org.netbeans.installer.utils.xml.DomUtil;
046: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
047: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050:
051: /**
052: * @author Danila_Dugurov
053: */
054: public class SectionImpl implements Section, DomExternalizable {
055:
056: /////////////////////////////////////////////////////////////////////////////////
057: // Instance
058: protected long start;
059: protected long length;
060: protected long offset;
061: private PumpingImpl owner;
062:
063: protected SectionImpl(PumpingImpl owner, long start, long length) {
064: this .owner = owner;
065: this .start = start;
066: this .length = length;
067: this .offset = start;
068: }
069:
070: //before readXML
071: protected SectionImpl(PumpingImpl owner) {
072: this .owner = owner;
073: }
074:
075: public Pair<Long, Long> getRange() {
076: return Pair.create(start, start + length);
077: }
078:
079: public long offset() {
080: return offset;
081: }
082:
083: public long length() {
084: return length;
085: }
086:
087: public long start() {
088: return start;
089: }
090:
091: public void shiftOffset(long delta) {
092: offset += delta;
093: owner.fireChanges("pumpingUpdate");
094: }
095:
096: public List<Pair<String, String>> headers() {
097: if (owner.acceptBytes) {
098: if (length > 0) {
099: final long end = start + length - 1;
100: return Collections.singletonList(Pair.create("Range",
101: "bytes=" + offset + "-" + end));
102: } else if (length == -1) {
103: return Collections.singletonList(Pair.create("Range",
104: "bytes=" + offset + "-"));
105: }
106: } else {
107: offset = start;
108: }
109: return Collections.emptyList();
110: }
111:
112: public void readXML(Element element) {
113: final DomVisitor visitor = new RecursiveDomVisitor() {
114: public void visit(Element element) {
115: final String name = element.getNodeName();
116: if ("start".equals(name)) {
117: start = Long.valueOf(element.getTextContent());
118: } else if ("length".equals(name)) {
119: length = Long.valueOf(element.getTextContent());
120: } else if ("offset".equals(name)) {
121: offset = Long.valueOf(element.getTextContent());
122: } else
123: super .visit(element);
124: }
125: };
126: visitor.visit(element);
127: }
128:
129: public Element writeXML(Document document) {
130: final Element root = document.createElement("section");
131: DomUtil.addElement(root, "start", String.valueOf(start));
132: DomUtil.addElement(root, "length", String.valueOf(length));
133: DomUtil.addElement(root, "offset", String.valueOf(offset));
134: return root;
135: }
136: }
|