001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.test.util.http;
020
021import java.io.ByteArrayOutputStream;
022import java.nio.Buffer;
023import java.nio.ByteBuffer;
024
025import org.eclipse.aether.spi.connector.transport.TransportListener;
026import org.eclipse.aether.transfer.TransferCancelledException;
027
028public class RecordingTransportListener extends TransportListener {
029
030    private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
031
032    private long dataOffset;
033
034    private long dataLength;
035
036    private int startedCount;
037
038    private int progressedCount;
039
040    private boolean cancelStart;
041
042    private boolean cancelProgress;
043
044    @Override
045    public void transportStarted(long dataOffset, long dataLength) throws TransferCancelledException {
046        startedCount++;
047        progressedCount = 0;
048        this.dataLength = dataLength;
049        this.dataOffset = dataOffset;
050        baos.reset();
051        if (cancelStart) {
052            throw new TransferCancelledException();
053        }
054    }
055
056    @Override
057    public void transportProgressed(ByteBuffer data) throws TransferCancelledException {
058        progressedCount++;
059        if (data.hasArray()) {
060            baos.write(data.array(), data.arrayOffset() + ((Buffer) data).position(), data.remaining());
061        } else {
062            byte[] arr = new byte[data.remaining()];
063            data.mark();
064            data.get(arr);
065            data.reset();
066        }
067        if (cancelProgress) {
068            throw new TransferCancelledException();
069        }
070    }
071
072    public ByteArrayOutputStream getBaos() {
073        return baos;
074    }
075
076    public long getDataOffset() {
077        return dataOffset;
078    }
079
080    public long getDataLength() {
081        return dataLength;
082    }
083
084    public int getStartedCount() {
085        return startedCount;
086    }
087
088    public int getProgressedCount() {
089        return progressedCount;
090    }
091
092    public boolean isCancelStart() {
093        return cancelStart;
094    }
095
096    public boolean isCancelProgress() {
097        return cancelProgress;
098    }
099
100    public void cancelStart() {
101        this.cancelStart = true;
102    }
103
104    public void cancelProgress() {
105        this.cancelProgress = true;
106    }
107}