|
||||
0001 /* 0002 * Licensed to the Apache Software Foundation (ASF) under one or more 0003 * contributor license agreements. See the NOTICE file distributed with 0004 * this work for additional information regarding copyright ownership. 0005 * The ASF licenses this file to You under the Apache License, Version 2.0 0006 * (the "License"); you may not use this file except in compliance with 0007 * the License. You may obtain a copy of the License at 0008 * 0009 * http://www.apache.org/licenses/LICENSE-2.0 0010 * 0011 * Unless required by applicable law or agreed to in writing, software 0012 * distributed under the License is distributed on an "AS IS" BASIS, 0013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0014 * See the License for the specific language governing permissions and 0015 * limitations under the License. 0016 */ 0017 0018 package org.apache.spark.network.util; 0019 0020 import java.nio.ByteBuffer; 0021 import java.nio.channels.WritableByteChannel; 0022 0023 /** 0024 * A writable channel that stores the written data in a byte array in memory. 0025 */ 0026 public class ByteArrayWritableChannel implements WritableByteChannel { 0027 0028 private final byte[] data; 0029 private int offset; 0030 0031 public ByteArrayWritableChannel(int size) { 0032 this.data = new byte[size]; 0033 } 0034 0035 public byte[] getData() { 0036 return data; 0037 } 0038 0039 public int length() { 0040 return offset; 0041 } 0042 0043 /** Resets the channel so that writing to it will overwrite the existing buffer. */ 0044 public void reset() { 0045 offset = 0; 0046 } 0047 0048 /** 0049 * Reads from the given buffer into the internal byte array. 0050 */ 0051 @Override 0052 public int write(ByteBuffer src) { 0053 int toTransfer = Math.min(src.remaining(), data.length - offset); 0054 src.get(data, offset, toTransfer); 0055 offset += toTransfer; 0056 return toTransfer; 0057 } 0058 0059 @Override 0060 public void close() { 0061 0062 } 0063 0064 @Override 0065 public boolean isOpen() { 0066 return true; 0067 } 0068 0069 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |