| 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.io.OutputStream; |
| 19 |
import java.nio.charset.Charset; |
| 20 |
|
| 21 |
import static io.airlift.slice.Preconditions.checkArgument; |
| 22 |
import static io.airlift.slice.Preconditions.checkNotNull; |
| 23 |
|
| 24 |
public class OutputStreamSliceOutput |
| 25 |
extends SliceOutput |
| 26 |
{ |
| 27 |
private final CountingOutputStream countingOutputStream; // Used only to track byte usage |
| 28 |
private final LittleEndianDataOutputStream dataOutputStream; |
| 29 |
|
| 30 |
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") |
| 31 |
public OutputStreamSliceOutput(OutputStream outputStream) |
| 32 |
{ |
| 33 |
checkNotNull(outputStream, "outputStream is null"); |
| 34 |
countingOutputStream = new CountingOutputStream(outputStream); |
| 35 |
dataOutputStream = new LittleEndianDataOutputStream(countingOutputStream); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void flush() |
| 40 |
throws IOException |
| 41 |
{ |
| 42 |
countingOutputStream.flush(); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void close() |
| 47 |
throws IOException |
| 48 |
{ |
| 49 |
countingOutputStream.close(); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void reset() |
| 54 |
{ |
| 55 |
throw new UnsupportedOperationException("OutputStream can not be reset"); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public int size() |
| 60 |
{ |
| 61 |
return checkedCast(countingOutputStream.getCount()); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public int writableBytes() |
| 66 |
{ |
| 67 |
return Integer.MAX_VALUE; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public boolean isWritable() |
| 72 |
{ |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void writeByte(int value) |
| 78 |
{ |
| 79 |
try { |
| 80 |
dataOutputStream.writeByte(value); |
| 81 |
} |
| 82 |
catch (IOException e) { |
| 83 |
throw new RuntimeIOException(e); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void writeShort(int value) |
| 89 |
{ |
| 90 |
try { |
| 91 |
dataOutputStream.writeShort(value); |
| 92 |
} |
| 93 |
catch (IOException e) { |
| 94 |
throw new RuntimeIOException(e); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void writeInt(int value) |
| 100 |
{ |
| 101 |
try { |
| 102 |
dataOutputStream.writeInt(value); |
| 103 |
} |
| 104 |
catch (IOException e) { |
| 105 |
throw new RuntimeIOException(e); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void writeLong(long value) |
| 111 |
{ |
| 112 |
try { |
| 113 |
dataOutputStream.writeLong(value); |
| 114 |
} |
| 115 |
catch (IOException e) { |
| 116 |
throw new RuntimeIOException(e); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void writeFloat(float value) |
| 122 |
{ |
| 123 |
try { |
| 124 |
dataOutputStream.writeFloat(value); |
| 125 |
} |
| 126 |
catch (IOException e) { |
| 127 |
throw new RuntimeIOException(e); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void writeDouble(double value) |
| 133 |
{ |
| 134 |
try { |
| 135 |
dataOutputStream.writeDouble(value); |
| 136 |
} |
| 137 |
catch (IOException e) { |
| 138 |
throw new RuntimeIOException(e); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void writeBytes(Slice source) |
| 144 |
{ |
| 145 |
writeBytes(source, 0, source.length()); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void writeBytes(Slice source, int sourceIndex, int length) |
| 150 |
{ |
| 151 |
try { |
| 152 |
source.getBytes(sourceIndex, dataOutputStream, length); |
| 153 |
} |
| 154 |
catch (IOException e) { |
| 155 |
throw new RuntimeIOException(e); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public void writeBytes(byte[] source) |
| 161 |
{ |
| 162 |
writeBytes(source, 0, source.length); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void writeBytes(byte[] source, int sourceIndex, int length) |
| 167 |
{ |
| 168 |
try { |
| 169 |
dataOutputStream.write(source, sourceIndex, length); |
| 170 |
} |
| 171 |
catch (IOException e) { |
| 172 |
throw new RuntimeIOException(e); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void writeBytes(InputStream in, int length) |
| 178 |
throws IOException |
| 179 |
{ |
| 180 |
SliceStreamUtils.copyStreamFully(in, this, length); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public SliceOutput appendLong(long value) |
| 185 |
{ |
| 186 |
writeLong(value); |
| 187 |
return this; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public SliceOutput appendDouble(double value) |
| 192 |
{ |
| 193 |
writeDouble(value); |
| 194 |
return this; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public SliceOutput appendInt(int value) |
| 199 |
{ |
| 200 |
writeInt(value); |
| 201 |
return this; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public SliceOutput appendShort(int value) |
| 206 |
{ |
| 207 |
writeShort(value); |
| 208 |
return this; |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public SliceOutput appendByte(int value) |
| 213 |
{ |
| 214 |
writeByte(value); |
| 215 |
return this; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public SliceOutput appendBytes(byte[] source, int sourceIndex, int length) |
| 220 |
{ |
| 221 |
writeBytes(source, sourceIndex, length); |
| 222 |
return this; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public SliceOutput appendBytes(byte[] source) |
| 227 |
{ |
| 228 |
writeBytes(source); |
| 229 |
return this; |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public SliceOutput appendBytes(Slice slice) |
| 234 |
{ |
| 235 |
writeBytes(slice); |
| 236 |
return this; |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public Slice slice() |
| 241 |
{ |
| 242 |
throw new UnsupportedOperationException(); |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public Slice getUnderlyingSlice() |
| 247 |
{ |
| 248 |
throw new UnsupportedOperationException(); |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public String toString(Charset charset) |
| 253 |
{ |
| 254 |
return toString(); |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public String toString() |
| 259 |
{ |
| 260 |
StringBuilder builder = new StringBuilder("BasicSliceOutput{"); |
| 261 |
builder.append("countingOutputStream=").append(countingOutputStream); |
| 262 |
builder.append(", dataOutputStream=").append(dataOutputStream); |
| 263 |
builder.append('}'); |
| 264 |
return builder.toString(); |
| 265 |
} |
| 266 |
|
| 267 |
private static int checkedCast(long value) |
| 268 |
{ |
| 269 |
int result = (int) value; |
| 270 |
checkArgument(result == value, "Size is greater than maximum int value"); |
| 271 |
return result; |
| 272 |
} |
| 273 |
} |