• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.jimfs;
18 
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.channels.SeekableByteChannel;
22 
23 /** @author Colin Decker */
24 public class ByteBufferChannel implements SeekableByteChannel {
25 
26   private final ByteBuffer buffer;
27 
ByteBufferChannel(byte[] bytes)28   public ByteBufferChannel(byte[] bytes) {
29     this.buffer = ByteBuffer.wrap(bytes);
30   }
31 
ByteBufferChannel(byte[] bytes, int offset, int length)32   public ByteBufferChannel(byte[] bytes, int offset, int length) {
33     this.buffer = ByteBuffer.wrap(bytes, offset, length);
34   }
35 
ByteBufferChannel(int capacity)36   public ByteBufferChannel(int capacity) {
37     this.buffer = ByteBuffer.allocate(capacity);
38   }
39 
ByteBufferChannel(ByteBuffer buffer)40   public ByteBufferChannel(ByteBuffer buffer) {
41     this.buffer = buffer;
42   }
43 
buffer()44   public ByteBuffer buffer() {
45     return buffer;
46   }
47 
48   @Override
read(ByteBuffer dst)49   public int read(ByteBuffer dst) throws IOException {
50     if (buffer.remaining() == 0) {
51       return -1;
52     }
53     int length = Math.min(dst.remaining(), buffer.remaining());
54     for (int i = 0; i < length; i++) {
55       dst.put(buffer.get());
56     }
57     return length;
58   }
59 
60   @Override
write(ByteBuffer src)61   public int write(ByteBuffer src) throws IOException {
62     int length = Math.min(src.remaining(), buffer.remaining());
63     for (int i = 0; i < length; i++) {
64       buffer.put(src.get());
65     }
66     return length;
67   }
68 
69   @Override
position()70   public long position() throws IOException {
71     return buffer.position();
72   }
73 
74   @Override
position(long newPosition)75   public SeekableByteChannel position(long newPosition) throws IOException {
76     buffer.position((int) newPosition);
77     return this;
78   }
79 
80   @Override
size()81   public long size() throws IOException {
82     return buffer.limit();
83   }
84 
85   @Override
truncate(long size)86   public SeekableByteChannel truncate(long size) throws IOException {
87     buffer.limit((int) size);
88     return this;
89   }
90 
91   @Override
isOpen()92   public boolean isOpen() {
93     return true;
94   }
95 
96   @Override
close()97   public void close() throws IOException {}
98 }
99