| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.InputStream; |
| 18 |
import java.nio.charset.Charset; |
| 19 |
|
| 20 |
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE; |
| 21 |
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE; |
| 22 |
import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT; |
| 23 |
import static io.airlift.slice.SizeOf.SIZE_OF_INT; |
| 24 |
import static io.airlift.slice.SizeOf.SIZE_OF_LONG; |
| 25 |
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT; |
| 26 |
|
| 27 |
public class DynamicSliceOutput |
| 28 |
extends SliceOutput |
| 29 |
{ |
| 30 |
private Slice slice; |
| 31 |
private int size; |
| 32 |
|
| 33 |
public DynamicSliceOutput(int estimatedSize) |
| 34 |
{ |
| 35 |
this.slice = Slices.allocate(estimatedSize); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void reset() |
| 40 |
{ |
| 41 |
size = 0; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public int size() |
| 46 |
{ |
| 47 |
return size; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public boolean isWritable() |
| 52 |
{ |
| 53 |
return writableBytes() > 0; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public int writableBytes() |
| 58 |
{ |
| 59 |
return slice.length() - size; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void writeByte(int value) |
| 64 |
{ |
| 65 |
slice = Slices.ensureSize(slice, size + SIZE_OF_BYTE); |
| 66 |
slice.setByte(size, value); |
| 67 |
size += SIZE_OF_BYTE; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void writeShort(int value) |
| 72 |
{ |
| 73 |
slice = Slices.ensureSize(slice, size + SIZE_OF_SHORT); |
| 74 |
slice.setShort(size, value); |
| 75 |
size += SIZE_OF_SHORT; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void writeInt(int value) |
| 80 |
{ |
| 81 |
slice = Slices.ensureSize(slice, size + SIZE_OF_INT); |
| 82 |
slice.setInt(size, value); |
| 83 |
size += SIZE_OF_INT; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void writeLong(long value) |
| 88 |
{ |
| 89 |
slice = Slices.ensureSize(slice, size + SIZE_OF_LONG); |
| 90 |
slice.setLong(size, value); |
| 91 |
size += SIZE_OF_LONG; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void writeFloat(float value) |
| 96 |
{ |
| 97 |
slice = Slices.ensureSize(slice, size + SIZE_OF_FLOAT); |
| 98 |
slice.setFloat(size, value); |
| 99 |
size += SIZE_OF_FLOAT; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void writeDouble(double value) |
| 104 |
{ |
| 105 |
slice = Slices.ensureSize(slice, size + SIZE_OF_DOUBLE); |
| 106 |
slice.setDouble(size, value); |
| 107 |
size += SIZE_OF_DOUBLE; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void writeBytes(byte[] source) |
| 112 |
{ |
| 113 |
writeBytes(source, 0, source.length); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void writeBytes(byte[] source, int sourceIndex, int length) |
| 118 |
{ |
| 119 |
slice = Slices.ensureSize(slice, size + length); |
| 120 |
slice.setBytes(size, source, sourceIndex, length); |
| 121 |
size += length; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void writeBytes(Slice source) |
| 126 |
{ |
| 127 |
writeBytes(source, 0, source.length()); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void writeBytes(Slice source, int sourceIndex, int length) |
| 132 |
{ |
| 133 |
slice = Slices.ensureSize(slice, size + length); |
| 134 |
slice.setBytes(size, source, sourceIndex, length); |
| 135 |
size += length; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void writeBytes(InputStream in, int length) |
| 140 |
throws IOException |
| 141 |
{ |
| 142 |
slice = Slices.ensureSize(slice, size + length); |
| 143 |
slice.setBytes(size, in, length); |
| 144 |
size += length; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void writeZero(int length) |
| 149 |
{ |
| 150 |
slice = Slices.ensureSize(slice, size + length); |
| 151 |
super.writeZero(length); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public DynamicSliceOutput appendLong(long value) |
| 156 |
{ |
| 157 |
writeLong(value); |
| 158 |
return this; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public DynamicSliceOutput appendDouble(double value) |
| 163 |
{ |
| 164 |
writeDouble(value); |
| 165 |
return this; |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public DynamicSliceOutput appendInt(int value) |
| 170 |
{ |
| 171 |
writeInt(value); |
| 172 |
return this; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public DynamicSliceOutput appendShort(int value) |
| 177 |
{ |
| 178 |
writeShort(value); |
| 179 |
return this; |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public DynamicSliceOutput appendByte(int value) |
| 184 |
{ |
| 185 |
writeByte(value); |
| 186 |
return this; |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public DynamicSliceOutput appendBytes(byte[] source, int sourceIndex, int length) |
| 191 |
{ |
| 192 |
write(source, sourceIndex, length); |
| 193 |
return this; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public DynamicSliceOutput appendBytes(byte[] source) |
| 198 |
{ |
| 199 |
writeBytes(source); |
| 200 |
return this; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public DynamicSliceOutput appendBytes(Slice slice) |
| 205 |
{ |
| 206 |
writeBytes(slice); |
| 207 |
return this; |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public Slice slice() |
| 212 |
{ |
| 213 |
return slice.slice(0, size); |
| 214 |
} |
| 215 |
|
| 216 |
public Slice copySlice() |
| 217 |
{ |
| 218 |
Slice copy = Slices.allocate(size); |
| 219 |
slice.getBytes(0, copy); |
| 220 |
return copy; |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public Slice getUnderlyingSlice() |
| 225 |
{ |
| 226 |
return slice; |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public String toString() |
| 231 |
{ |
| 232 |
StringBuilder builder = new StringBuilder("BasicSliceOutput{"); |
| 233 |
builder.append("size=").append(size); |
| 234 |
builder.append(", capacity=").append(slice.length()); |
| 235 |
builder.append('}'); |
| 236 |
return builder.toString(); |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public String toString(Charset charset) |
| 241 |
{ |
| 242 |
return slice.toString(0, size, charset); |
| 243 |
} |
| 244 |
} |