|
| 1 | +/* |
| 2 | + * **************************************************************************** |
| 3 | + * Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | + * specific language governing permissions and limitations under the License. |
| 13 | + * **************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +package com.spectralogic.ds3client.utils; |
| 17 | + |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.nio.channels.SeekableByteChannel; |
| 24 | + |
| 25 | +public class LoggingSeekableByteChannel implements SeekableByteChannel { |
| 26 | + |
| 27 | + private static final Logger LOG = LoggerFactory.getLogger(LoggingSeekableByteChannel.class); |
| 28 | + |
| 29 | + private final SeekableByteChannel wrappedChannel; |
| 30 | + private final String channelName; |
| 31 | + |
| 32 | + public LoggingSeekableByteChannel(final SeekableByteChannel wrappedChannel, final String channelName) { |
| 33 | + this.wrappedChannel = wrappedChannel; |
| 34 | + this.channelName = channelName; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int read(final ByteBuffer dst) throws IOException { |
| 39 | + final int readCount = wrappedChannel.read(dst); |
| 40 | + LOG.info("Read {} bytes from {}", readCount, channelName); |
| 41 | + return readCount; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int write(final ByteBuffer src) throws IOException { |
| 46 | + final int writeCount = wrappedChannel.write(src); |
| 47 | + LOG.info("Wrote {} bytes to {}", writeCount, channelName); |
| 48 | + return writeCount; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public long position() throws IOException { |
| 53 | + return wrappedChannel.position(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public SeekableByteChannel position(final long newPosition) throws IOException { |
| 58 | + return wrappedChannel.position(newPosition); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public long size() throws IOException { |
| 63 | + return wrappedChannel.size(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public SeekableByteChannel truncate(final long size) throws IOException { |
| 68 | + return wrappedChannel.truncate(size); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isOpen() { |
| 73 | + return wrappedChannel.isOpen(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void close() throws IOException { |
| 78 | + wrappedChannel.close(); |
| 79 | + } |
| 80 | +} |
0 commit comments