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; 024import java.util.Map; 025 026import org.eclipse.aether.spi.connector.transport.TransportListener; 027import org.eclipse.aether.transfer.TransferCancelledException; 028import org.eclipse.aether.transfer.TransferEvent; 029import org.eclipse.aether.transfer.TransferEvent.TransportPropertyKey; 030 031public class RecordingTransportListener extends TransportListener { 032 033 private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 034 035 private long dataOffset; 036 037 private long dataLength; 038 039 private int startedCount; 040 041 private int progressedCount; 042 043 private boolean cancelStart; 044 045 private boolean cancelProgress; 046 047 private Map<TransferEvent.TransportPropertyKey, Object> transportProperties; 048 049 @Override 050 public void transportStarted(long dataOffset, long dataLength) throws TransferCancelledException { 051 startedCount++; 052 progressedCount = 0; 053 this.dataLength = dataLength; 054 this.dataOffset = dataOffset; 055 baos.reset(); 056 if (cancelStart) { 057 throw new TransferCancelledException(); 058 } 059 } 060 061 @Override 062 public void transportProgressed(ByteBuffer data) throws TransferCancelledException { 063 progressedCount++; 064 if (data.hasArray()) { 065 baos.write(data.array(), data.arrayOffset() + ((Buffer) data).position(), data.remaining()); 066 } else { 067 byte[] arr = new byte[data.remaining()]; 068 data.mark(); 069 data.get(arr); 070 data.reset(); 071 } 072 if (cancelProgress) { 073 throw new TransferCancelledException(); 074 } 075 } 076 077 @Override 078 public void transportPropertiesAvailable(Map<TransportPropertyKey, Object> transportProperties) 079 throws TransferCancelledException { 080 this.transportProperties = transportProperties; 081 } 082 083 public ByteArrayOutputStream getBaos() { 084 return baos; 085 } 086 087 public long getDataOffset() { 088 return dataOffset; 089 } 090 091 public long getDataLength() { 092 return dataLength; 093 } 094 095 public int getStartedCount() { 096 return startedCount; 097 } 098 099 public int getProgressedCount() { 100 return progressedCount; 101 } 102 103 public boolean isCancelStart() { 104 return cancelStart; 105 } 106 107 public boolean isCancelProgress() { 108 return cancelProgress; 109 } 110 111 public void cancelStart() { 112 this.cancelStart = true; 113 } 114 115 public void cancelProgress() { 116 this.cancelProgress = true; 117 } 118 119 public Map<TransferEvent.TransportPropertyKey, Object> getTransportProperties() { 120 return transportProperties; 121 } 122}