当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuffer.asReadOnlyBuffer方法代码示例

本文整理汇总了Java中java.nio.ByteBuffer.asReadOnlyBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.asReadOnlyBuffer方法的具体用法?Java ByteBuffer.asReadOnlyBuffer怎么用?Java ByteBuffer.asReadOnlyBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.asReadOnlyBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: outgoingData

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@DataProvider(name = "outgoingData", parallel = true)
public static Iterator<Object[]> outgoingData() {
    List<Integer> leading  = of(0, 1, 17, 125);
    List<Integer> trailing = of(0, 1, 19, 123);
    List<Integer> sizes    = of(0, 1, 2, 17, 32, 64, 122, 123, 124, 125, 126, 127, 128, 256);
    List<Boolean> direct   = of(true, false);
    List<Boolean> readonly = of(false); // TODO: return readonly (true)
    F5<Integer, Integer, Integer, Boolean, Boolean, Object[]> f =
            (l, t, s, d, r) -> {
                ByteBuffer b;
                if (d) {
                    b = ByteBuffer.allocateDirect(l + t + s);
                } else {
                    b = ByteBuffer.allocate(l + t + s);
                }
                fill(b);
                if (r) {
                    b = b.asReadOnlyBuffer();
                }
                b.position(l).limit(l + s);
                return new ByteBuffer[]{b};
            };
    Iterator<Object[]> product = cartesianIterator(leading, trailing, sizes, direct, readonly, f);
    Iterator<Object[]> i = iteratorOf1(new Object[]{null});
    return concat(iteratorOf(i, product));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:DataProviders.java

示例2: copyAllBytesFrom

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Returns a copy of all the bytes from the given <code>ByteBuffer</code>,
 * from the beginning to the buffer's limit; or null if the input is null.
 * <p>
 * The internal states of the given byte buffer will be restored when this
 * method completes execution.
 * <p>
 * When handling <code>ByteBuffer</code> from user's input, it's typical to
 * call the {@link #copyBytesFrom(ByteBuffer)} instead of
 * {@link #copyAllBytesFrom(ByteBuffer)} so as to account for the position
 * of the input <code>ByteBuffer</code>. The opposite is typically true,
 * however, when handling <code>ByteBuffer</code> from withint the
 * unmarshallers of the low-level clients.
 */
public static byte[] copyAllBytesFrom(ByteBuffer bb) {
    if (bb == null) {
        return null;
    }

    if (bb.hasArray()) {
        return Arrays.copyOfRange(
                bb.array(),
                bb.arrayOffset(),
                bb.arrayOffset() + bb.limit());
    }

    ByteBuffer copy = bb.asReadOnlyBuffer();
    copy.rewind();

    byte[] dst = new byte[copy.remaining()];
    copy.get(dst);
    return dst;
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:34,代码来源:BinaryUtils.java

示例3: checkWrapWithDirectBBReadonly

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void checkWrapWithDirectBBReadonly() {
  int memCapacity = 64;
  ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
  byteBuf.order(ByteOrder.nativeOrder());

  for (int i = 0; i < memCapacity; i++) {
    byteBuf.put(i, (byte) i);
  }
  ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
  byteBufRO.order(ByteOrder.nativeOrder());

  Memory mem = Memory.wrap(byteBufRO);

  for (int i = 0; i < memCapacity; i++) {
    assertEquals(mem.getByte(i), byteBuf.get(i));
  }

  //println(mem.toHexString("HeapBB", 0, memCapacity));
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:21,代码来源:WritableMemoryImplTest.java

示例4: checkWrapWithDirectBBReadonly

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void checkWrapWithDirectBBReadonly() {
  int memCapacity = 64;
  ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
  byteBuf.order(ByteOrder.nativeOrder());

  for (int i = 0; i < memCapacity; i++) {
    byteBuf.put(i, (byte) i);
  }
  ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
  byteBufRO.order(ByteOrder.nativeOrder());

  Buffer buf = Buffer.wrap(byteBufRO);

  for (int i = 0; i < memCapacity; i++) {
    assertEquals(buf.getByte(), byteBuf.get(i));
  }

  //println(mem.toHexString("HeapBB", 0, memCapacity));
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:21,代码来源:WritableBufferImplTest.java

示例5: checkWrapWithDirectBBReadonlyPut

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test(expectedExceptions = ReadOnlyException.class)
public void checkWrapWithDirectBBReadonlyPut() {
  int memCapacity = 64;
  ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
  ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
  byteBufRO.order(ByteOrder.nativeOrder());

  WritableMemory.wrap(byteBufRO);
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:10,代码来源:WritableMemoryImplTest.java

示例6: setData

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public GifHeaderParser setData(ByteBuffer data) {
  reset();
  rawData = data.asReadOnlyBuffer();
  rawData.position(0);
  rawData.order(ByteOrder.LITTLE_ENDIAN);
  return this;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:GifHeaderParser.java

示例7: toBytes

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static byte[] toBytes(ByteBuffer byteBuffer) {
  final byte[] result;
  SafeArray safeArray = getSafeArray(byteBuffer);
  if (safeArray != null && safeArray.offset == 0 && safeArray.limit == safeArray.data.length) {
    result = byteBuffer.array();
  } else {
    ByteBuffer toCopy = byteBuffer.asReadOnlyBuffer();
    result = new byte[toCopy.limit()];
    toCopy.position(0);
    toCopy.get(result);
  }
  return result;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:14,代码来源:ByteBufferUtil.java

示例8: readGIOPBody

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static Message readGIOPBody(ORB orb,
                                   CorbaConnection connection,
                                   Message msg)
{
    ReadTimeouts readTimeouts =
                       orb.getORBData().getTransportTCPReadTimeouts();
    ByteBuffer buf = msg.getByteBuffer();

    buf.position(MessageBase.GIOPMessageHeaderLength);
    int msgSizeMinusHeader =
        msg.getSize() - MessageBase.GIOPMessageHeaderLength;
    try {
        buf = connection.read(buf,
                      GIOPMessageHeaderLength, msgSizeMinusHeader,
                      readTimeouts.get_max_time_to_wait());
    } catch (IOException e) {
        throw wrapper.ioexceptionWhenReadingConnection(e);
    }

    msg.setByteBuffer(buf);

    if (orb.giopDebugFlag) {
        dprint(".readGIOPBody: received message:");
        ByteBuffer viewBuffer = buf.asReadOnlyBuffer();
        viewBuffer.position(0).limit(msg.getSize());
        ByteBufferWithInfo bbwi = new ByteBufferWithInfo(orb, viewBuffer);
        CDRInputStream_1_0.printBuffer(bbwi);
    }

    return msg;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:MessageBase.java

示例9: getBinary

import java.nio.ByteBuffer; //导入方法依赖的package包/类
ByteBuffer getBinary(int res) {
    int offset=RES_GET_OFFSET(res);
    int length;
    if(RES_GET_TYPE(res)==UResourceBundle.BINARY) {
        if(offset==0) {
            // Don't just
            //   return emptyByteBuffer;
            // in case it matters whether the buffer's mark is defined or undefined.
            return emptyByteBuffer.duplicate();
        } else {
            // Not cached: The returned buffer is small (shares its bytes with the bundle)
            // and usually quickly discarded after use.
            // Also, even a cached buffer would have to be cloned because it is mutable
            // (position & mark).
            offset=getResourceByteOffset(offset);
            length=getInt(offset);
            if(length == 0) {
                return emptyByteBuffer.duplicate();
            }
            offset += 4;
            ByteBuffer result = bytes.duplicate();
            result.position(offset).limit(offset + length);
            result = ICUBinary.sliceWithOrder(result);
            if(!result.isReadOnly()) {
                result = result.asReadOnlyBuffer();
            }
            return result;
        }
    } else {
        return null;
    }
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:33,代码来源:ICUResourceBundleReader.java

示例10: setData

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public GifHeaderParser setData(ByteBuffer data) {
	reset();
	rawData = data.asReadOnlyBuffer();
	rawData.position(0);
	rawData.order(ByteOrder.LITTLE_ENDIAN);
	return this;
}
 
开发者ID:MobClub,项目名称:BBSSDK-for-Android,代码行数:8,代码来源:GifHeaderParser.java

示例11: setData

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public synchronized void setData(GifHeader header, ByteBuffer buffer, int sampleSize) {
  if (sampleSize <= 0) {
    throw new IllegalArgumentException("Sample size must be >=0, not: " + sampleSize);
  }
  // Make sure sample size is a power of 2.
  sampleSize = Integer.highestOneBit(sampleSize);
  this.status = STATUS_OK;
  this.header = header;
  isFirstFrameTransparent = false;
  framePointer = INITIAL_FRAME_POINTER;
  resetLoopIndex();
  // Initialize the raw data buffer.
  rawData = buffer.asReadOnlyBuffer();
  rawData.position(0);
  rawData.order(ByteOrder.LITTLE_ENDIAN);

  // No point in specially saving an old frame if we're never going to use it.
  savePrevious = false;
  for (GifFrame frame : header.frames) {
    if (frame.dispose == DISPOSAL_PREVIOUS) {
      savePrevious = true;
      break;
    }
  }

  this.sampleSize = sampleSize;
  downsampledWidth = header.width / sampleSize;
  downsampledHeight = header.height / sampleSize;
  // Now that we know the size, init scratch arrays.
  // TODO Find a way to avoid this entirely or at least downsample it (either should be possible).
  mainPixels = bitmapProvider.obtainByteArray(header.width * header.height);
  mainScratch = bitmapProvider.obtainIntArray(downsampledWidth * downsampledHeight);
}
 
开发者ID:pedroSG94,项目名称:rtmp-rtsp-stream-client-java,代码行数:34,代码来源:GifDecoder.java

示例12: ensureWriteCapacity

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private synchronized void ensureWriteCapacity(int i) {
	int size=readSize();
	int writeCapacity=data.capacity()-size-1;
	while(writeCapacity<i)
	{
		ByteBuffer doubled=ByteBuffer.allocate(data.capacity()*2).order(AbstractMultiplexer.order);
		copyTo(doubled, size);
		readView=doubled.asReadOnlyBuffer();
		readPtr=0;
		writePtr=size;
		data=doubled;
		size=readSize();
		writeCapacity=data.capacity()-size-1;
	}
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:16,代码来源:AutoGrowBuffer.java

示例13: toReadOnlyByteBuffer

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public ByteBuffer toReadOnlyByteBuffer() {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, offset, length);
    return byteBuffer.asReadOnlyBuffer();
}
 
开发者ID:avast,项目名称:bytes,代码行数:6,代码来源:ByteArrayBytes.java

示例14: asReadOnlyByteBuffer

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Constructs a new read-only {@code java.nio.ByteBuffer} with the
 * same backing byte array.
 */
public ByteBuffer asReadOnlyByteBuffer() {
  final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
  return byteBuffer.asReadOnlyBuffer();
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:9,代码来源:ByteString.java

示例15: decodeLoop

import java.nio.ByteBuffer; //导入方法依赖的package包/类
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (detectedDecoder == null) {
        copyLeadingASCII(src, dst);

        // All ASCII?
        if (! src.hasRemaining())
            return CoderResult.UNDERFLOW;
        // Overflow only if there is still ascii but no out buffer.
        if (!dst.hasRemaining() &&
            isPlainASCII(src.get(src.position())))
            return CoderResult.OVERFLOW;

        // We need to perform double, not float, arithmetic; otherwise
        // we lose low order bits when src is larger than 2**24.
        int cbufsiz = (int)(src.limit() * (double)maxCharsPerByte());
        CharBuffer sandbox = CharBuffer.allocate(cbufsiz);

        // First try ISO-2022-JP, since there is no ambiguity
        Charset cs2022 = Charset.forName("ISO-2022-JP");
        DelegatableDecoder dd2022
            = (DelegatableDecoder) cs2022.newDecoder();
        ByteBuffer src2022 = src.asReadOnlyBuffer();
        CoderResult res2022 = dd2022.decodeLoop(src2022, sandbox);
        if (! res2022.isError())
            return decodeLoop(dd2022, src, dst);

        // We must choose between EUC and SJIS
        Charset csEUCJ = Charset.forName(EUCJPName);
        Charset csSJIS = Charset.forName(SJISName);

        DelegatableDecoder ddEUCJ
            = (DelegatableDecoder) csEUCJ.newDecoder();
        DelegatableDecoder ddSJIS
            = (DelegatableDecoder) csSJIS.newDecoder();

        ByteBuffer srcEUCJ = src.asReadOnlyBuffer();
        sandbox.clear();
        CoderResult resEUCJ = ddEUCJ.decodeLoop(srcEUCJ, sandbox);
        // If EUC decoding fails, must be SJIS
        if (resEUCJ.isError())
            return decodeLoop(ddSJIS, src, dst);
        ByteBuffer srcSJIS = src.asReadOnlyBuffer();
        CharBuffer sandboxSJIS = CharBuffer.allocate(cbufsiz);
        CoderResult resSJIS = ddSJIS.decodeLoop(srcSJIS, sandboxSJIS);
        // If SJIS decoding fails, must be EUC
        if (resSJIS.isError())
            return decodeLoop(ddEUCJ, src, dst);

        // From here on, we have some ambiguity, and must guess.

        // We prefer input that does not appear to end mid-character.
        if (srcEUCJ.position() > srcSJIS.position())
            return decodeLoop(ddEUCJ, src, dst);

        if (srcEUCJ.position() < srcSJIS.position())
            return decodeLoop(ddSJIS, src, dst);

        // end-of-input is after the first byte of the first char?
        if (src.position() == srcEUCJ.position())
            return CoderResult.UNDERFLOW;

        // Use heuristic knowledge of typical Japanese text
        sandbox.flip();
        return decodeLoop(looksLikeJapanese(sandbox) ? ddEUCJ : ddSJIS,
                          src, dst);
    }

    return detectedDecoder.decodeLoop(src, dst);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:70,代码来源:JISAutoDetect.java


注:本文中的java.nio.ByteBuffer.asReadOnlyBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。