Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / SlicedBuffer.java
1 /*
2  * Copyright 2015-present Open Networking Foundation
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 package io.atomix.storage.buffer;
17
18 /**
19  * Sliced buffer.
20  * <p>
21  * The sliced buffer provides a view of a subset of an underlying buffer. This buffer operates directly on the {@link Bytes}
22  * underlying the child {@link Buffer} instance.
23  *
24  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
25  */
26 public class SlicedBuffer extends AbstractBuffer {
27   private final Buffer root;
28
29   public SlicedBuffer(Buffer root, Bytes bytes, int offset, int initialCapacity, int maxCapacity) {
30     super(bytes, offset, initialCapacity, maxCapacity, null);
31     this.root = root;
32     root.acquire();
33   }
34
35   /**
36    * Returns the root buffer.
37    *
38    * @return The root buffer.
39    */
40   public Buffer root() {
41     return root;
42   }
43
44   @Override
45   public boolean isDirect() {
46     return root.isDirect();
47   }
48
49   @Override
50   protected void compact(int from, int to, int length) {
51     if (root instanceof AbstractBuffer) {
52       ((AbstractBuffer) root).compact(from, to, length);
53     }
54   }
55
56   @Override
57   public boolean isFile() {
58     return root.isFile();
59   }
60
61   @Override
62   public boolean isReadOnly() {
63     return root.isReadOnly();
64   }
65
66   @Override
67   public Buffer compact() {
68     return null;
69   }
70
71   @Override
72   public Buffer duplicate() {
73     return new SlicedBuffer(root, bytes, offset(), capacity(), maxCapacity());
74   }
75
76   @Override
77   public Buffer acquire() {
78     root.acquire();
79     return this;
80   }
81
82   @Override
83   public boolean release() {
84     return root.release();
85   }
86
87   @Override
88   public void close() {
89     root.release();
90   }
91
92 }