region OutputStream and RandomAccessOutputStream Implementation
331136
@Override
    public void write(int b) throws IOException {
        if (this.isClosed) {
            throw new IOException("OutputStream is closed.");
        }

        if (this.position >= this.length) {
            throw new IOException("Buffer capacity exceeded.");
        }

        this.array[this.offset + this.position] = (byte) b;
        this.position++;
    }
624025
@Override
  public RandomAccessStream openFileRandomAccess() throws IOException
  {
    if (_isWindows && isAux())
      throw new FileNotFoundException(_file.toString());

    return new FileRandomAccessStream(new RandomAccessFile(getFile(), "rw"));
  }
873986
@Override
    protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
        if (finished) {
            if (!out.isWritable(in.readableBytes())) {
                // out should be EMPTY_BUFFER because we should have allocated enough space above in allocateBuffer.
                throw ENCODE_FINSHED_EXCEPTION;
            }
            out.writeBytes(in);
            return;
        }

        final ByteBuf buffer = this.buffer;
        int length;
        while ((length = in.readableBytes()) > 0) {
            final int nextChunkSize = Math.min(length, buffer.writableBytes());
            in.readBytes(buffer, nextChunkSize);

            if (!buffer.isWritable()) {
                flushBufferedData(out);
            }
        }
    }
982109
@Override
  void writeStreamBlob(OutputStream os, 
                       byte[] buffer, int rowOffset,
                       byte []blobBuffer,
                       PageServiceImpl tableService)
    throws IOException
  {
    int offset = rowOffset + offset();
    
    int blobLen = BitsUtil.readInt16(buffer, offset + 2);
    int blobOffset = BitsUtil.readInt16(buffer, offset);
    
    if (blobLen == 0) {
      BitsUtil.writeInt(os, 0);
      return;
    }
    
    if (isLargeBlob(blobLen)) {
      int blobId = BitsUtil.readInt(blobBuffer, blobOffset);
      
      ArrayList<PageBlob> blobList = new ArrayList<>();

      while (blobId > 0) {
        PageBlob page = tableService.getBlobPage(blobId);
        
        blobList.add(page);
        
        blobId = page.getNextId();
      }
        
      for (int i = 0; i < blobList.size(); i++) {
        PageBlob page = blobList.get(i);
        int length = page.getLength();
        
        if ((length & 0xc000_0000) != 0) {
          throw new IllegalStateException(L.l("Unexpected blob length {0} for {1}", 
                                              length, page));
        }
        
        if (i + 1 < blobList.size()) {
          length |= BLOB_CONT_MASK;
        }
        
        BitsUtil.writeInt(os, length);
        
        page.writeToStream(os);
      }
    }
    else {
      BitsUtil.writeInt(os, blobLen);
      
      os.write(blobBuffer, blobOffset, blobLen);
    }
  }
665252
private void mapAndSetOffset() {
        try {
            final RandomAccessFile backingFile = new RandomAccessFile(this.file, "rw");
            backingFile.setLength(this.size);
            final FileChannel ch = backingFile.getChannel();
            this.addr = (Long) mmap.invoke(ch, 1, 0L, this.size);

            ch.close();
            backingFile.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
146392
@Override
  public void writeHeader(RandomAccessFile file) throws IOException {
    super.writeHeader(file);
    file.writeInt(this.dirCapacity);
    file.writeInt(this.leafCapacity);
    file.writeInt(this.dirMinimum);
    file.writeInt(this.leafMinimum);
    file.writeInt(this.emptyPagesSize);
    file.writeInt(this.largestPageID);
  }
1063391
@Override
  public void offerData(OutputStream stream) {
    byte[] next = null;
    try {
      next = queue.poll(timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignored) {
      // If this ever times out (unlikely given our assumption), next stays null.
    }

    try {
      // Only if next is not null nor CLOSED do we write the chunk.
      if (next != null && next != CLOSED) {
        stream.write(next);
        return;
      }
    } catch (IOException ignored) {
      // Any errors in the piped (NS)OutputStream (unlikely) fall through to the next section.
    }

    // Close the piped (NS)OutputStream and ignore any errors.
    try {
      stream.close();
    } catch (IOException ignored) {
      // Ignored.
    }
  }
1304294
public int read(long fileOffset, byte []buffer, int offset, int length)
    throws IOException
  {
    log.info("random-read(0x" + Long.toHexString(fileOffset) + "," + 
             length + ")");
    
    return _file.read(fileOffset, buffer, offset, length);
  }
452357
private void loadSectionBytes() throws IOException {
        Preconditions.checkState(size == (int) size,
                "section is too large to dump into byte array");
        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
            raf.seek(offset);
            byte[] bytes = new byte[(int) size];
            raf.read(bytes);
            sectionbytes = Optional.of(bytes);
        }
    }
1296163
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
        // Write out capacity and any hidden stuff
        s.defaultWriteObject();
    }
257617
public void setOutput(OutputStream stream, String encoding)
    throws UnsupportedEncodingException {
        
        if (encoding == null) {
            encoding = "UTF8";
        }
        
        java.io.Writer writer = new OutputStreamWriter(stream, encoding);
        fOut = new PrintWriter(writer);
        
    }
791469
private void fillOutputStream(OutputStream stream, int length)
			throws AsnException, IOException {
		byte[] dataBucket = new byte[DATA_BUCKET_SIZE];
		int readCount;

		while (length != 0) {
			int cnt = length < DATA_BUCKET_SIZE ? length : DATA_BUCKET_SIZE;
			readCount = read(dataBucket, 0, cnt);
			if (readCount < cnt)
				throw new AsnException("input stream has reached the end");
			stream.write(dataBucket, 0, readCount);
			length -= readCount;
		}
	}
1351647
private void readUncompressedIntegerRowByNumber( ByteBuffer rowdata, int rn, RandomAccessFile thefile ) throws IOException,
            DataFormatException {
        int cellValue = 0;
        ByteBuffer cell = ByteBuffer.allocate(rasterMapType);

        /* The number of bytes that are inside a row in the file. */
        int filerowsize = fileWindow.getCols() * rasterMapType;

        /* Position the file pointer to read the row */
        thefile.seek((rn * filerowsize));

        /* Read the row of data from the file */
        ByteBuffer tmpBuffer = ByteBuffer.allocate(filerowsize);
        thefile.read(tmpBuffer.array());

        /*
         * Transform the rasterMapType-size-values to a standard 4 bytes integer value
         */
        while( tmpBuffer.hasRemaining() ) {
            // read the value
            tmpBuffer.get(cell.array());

            /*
             * Integers can be of 1, 2, or 4 bytes. As rasterBuffer expects 4 byte integers we need
             * to pad them with 0's. The order of the padding is determined by the ByteOrder of the
             * buffer.
             */
            if (rasterMapType == 1) {
                cellValue = (cell.get(0) & 0xff);
            } else if (rasterMapType == 2) {
                cellValue = cell.getShort(0);
            } else if (rasterMapType == 4) {
                cellValue = cell.getInt(0);
            }
            // if (logger.isDebugEnabled()) logger.debug("tmpint=" + cellValue
            // );
            rowdata.putInt(cellValue);
        }
    }
502067
void writeStreams() {
    OkHttpClientStream[] streams = transport.getActiveStreams();
    int connectionWindow = connectionState.window();
    for (int numStreams = streams.length; numStreams > 0 && connectionWindow > 0;) {
      int nextNumStreams = 0;
      int windowSlice = (int) ceil(connectionWindow / (float) numStreams);
      for (int index = 0; index < numStreams && connectionWindow > 0; ++index) {
        OkHttpClientStream stream = streams[index];
        OutboundFlowState state = state(stream);

        int bytesForStream = min(connectionWindow, min(state.unallocatedBytes(), windowSlice));
        if (bytesForStream > 0) {
          state.allocateBytes(bytesForStream);
          connectionWindow -= bytesForStream;
        }

        if (state.unallocatedBytes() > 0) {
          // There is more data to process for this stream. Add it to the next
          // pass.
          streams[nextNumStreams++] = stream;
        }
      }
      numStreams = nextNumStreams;
    }

    // Now take one last pass through all of the streams and write any allocated bytes.
    WriteStatus writeStatus = new WriteStatus();
    for (OkHttpClientStream stream : transport.getActiveStreams()) {
      OutboundFlowState state = state(stream);
      state.writeBytes(state.allocatedBytes(), writeStatus);
      state.clearAllocatedBytes();
    }

    if (writeStatus.hasWritten()) {
      flush();
    }
  }
1003528
void bufferIt(BufferedOutputStream outStream, int code, int size) throws IOException {
        int PutBuffer = code;
        int PutBits = bufferPutBits;

        PutBuffer &= (1 << size) - 1;
        PutBits += size;
        PutBuffer <<= 24 - PutBits;
        PutBuffer |= bufferPutBuffer;

        while (PutBits >= 8) {
            int c = ((PutBuffer >> 16) & 0xFF);
            outStream.write(c);
            if (c == 0xFF) {
                outStream.write(0);
            }
            PutBuffer <<= 8;
            PutBits -= 8;
        }
        bufferPutBuffer = PutBuffer;
        bufferPutBits = PutBits;

    }
1201111
private ByteBuffer readHeader( RandomAccessFile ds ) throws IOException {

        /*
         * the first byte defines the number of bytes are used to describe the row addresses in the
         * header (once it was sizeof(long) in grass but then it was turned to an offset (that
         * brought to reading problems in JGrass whenever the offset was != 4).
         */
        int first = ds.read();

        ByteBuffer fileHeader = ByteBuffer.allocate(1 + first * fileWindow.getRows() + first);

        ds.seek(0);
        /* Read header */
        ds.read(fileHeader.array());

        return fileHeader;
    }
525509
public void write(OutputStream out, int offset,
                    int length) throws IOException {
    data.getBytes(offset, out, length);
  }