1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 import java.io.IOException; 11 import java.nio.ByteBuffer; 12 13 /** 14 * An output target for raw bytes. This interface provides semantics that support two types of 15 * writing: 16 * 17 * <p><b>Traditional write operations:</b> (as defined by {@link java.io.OutputStream}) where the 18 * target method is responsible for either copying the data or completing the write before returning 19 * from the method call. 20 * 21 * <p><b>Lazy write operations:</b> where the caller guarantees that it will never modify the 22 * provided buffer and it can therefore be considered immutable. The target method is free to 23 * maintain a reference to the buffer beyond the scope of the method call (e.g. until the write 24 * operation completes). 25 */ 26 @ExperimentalApi 27 public abstract class ByteOutput { 28 /** 29 * Writes a single byte. 30 * 31 * @param value the byte to be written 32 * @throws IOException thrown if an error occurred while writing 33 */ write(byte value)34 public abstract void write(byte value) throws IOException; 35 36 /** 37 * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will not be 38 * processed prior to the return of this method call, since {@code value} may be reused/altered by 39 * the caller. 40 * 41 * <p>NOTE: This method <strong>MUST NOT</strong> modify the {@code value}. Doing so is a 42 * programming error and will lead to data corruption which will be difficult to debug. 43 * 44 * @param value the bytes to be written 45 * @param offset the offset of the start of the writable range 46 * @param length the number of bytes to write starting from {@code offset} 47 * @throws IOException thrown if an error occurred while writing 48 */ write(byte[] value, int offset, int length)49 public abstract void write(byte[] value, int offset, int length) throws IOException; 50 51 /** 52 * Writes a sequence of bytes. The {@link ByteOutput} is free to retain a reference to the value 53 * beyond the scope of this method call (e.g. write later) since it is considered immutable and is 54 * guaranteed not to change by the caller. 55 * 56 * <p>NOTE: This method <strong>MUST NOT</strong> modify the {@code value}. Doing so is a 57 * programming error and will lead to data corruption which will be difficult to debug. 58 * 59 * @param value the bytes to be written 60 * @param offset the offset of the start of the writable range 61 * @param length the number of bytes to write starting from {@code offset} 62 * @throws IOException thrown if an error occurred while writing 63 */ writeLazy(byte[] value, int offset, int length)64 public abstract void writeLazy(byte[] value, int offset, int length) throws IOException; 65 66 /** 67 * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will not be 68 * processed prior to the return of this method call, since {@code value} may be reused/altered by 69 * the caller. 70 * 71 * <p>NOTE: This method <strong>MUST NOT</strong> modify the {@code value}. Doing so is a 72 * programming error and will lead to data corruption which will be difficult to debug. 73 * 74 * @param value the bytes to be written. Upon returning from this call, the {@code position} of 75 * this buffer will be set to the {@code limit} 76 * @throws IOException thrown if an error occurred while writing 77 */ write(ByteBuffer value)78 public abstract void write(ByteBuffer value) throws IOException; 79 80 /** 81 * Writes a sequence of bytes. The {@link ByteOutput} is free to retain a reference to the value 82 * beyond the scope of this method call (e.g. write later) since it is considered immutable and is 83 * guaranteed not to change by the caller. 84 * 85 * <p>NOTE: This method <strong>MUST NOT</strong> modify the {@code value}. Doing so is a 86 * programming error and will lead to data corruption which will be difficult to debug. 87 * 88 * @param value the bytes to be written. Upon returning from this call, the {@code position} of 89 * this buffer will be set to the {@code limit} 90 * @throws IOException thrown if an error occurred while writing 91 */ writeLazy(ByteBuffer value)92 public abstract void writeLazy(ByteBuffer value) throws IOException; 93 } 94