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


Java ReadableByteChannel类代码示例

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


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

示例1: CryptoInputStream

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public CryptoInputStream(InputStream in, CryptoCodec codec,
    int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException {
  super(in);
  CryptoStreamUtils.checkCodec(codec);
  this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize);
  this.codec = codec;
  this.key = key.clone();
  this.initIV = iv.clone();
  this.iv = iv.clone();
  this.streamOffset = streamOffset;
  isByteBufferReadable = in instanceof ByteBufferReadable;
  isReadableByteChannel = in instanceof ReadableByteChannel;
  inBuffer = ByteBuffer.allocateDirect(this.bufferSize);
  outBuffer = ByteBuffer.allocateDirect(this.bufferSize);
  decryptor = getDecryptor();
  resetStreamOffset(streamOffset);
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:18,代码来源:CryptoInputStream.java

示例2: parse

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
/**
 * Read the box's content from a byte channel without parsing it. Parsing is done on-demand.
 *
 * @param readableByteChannel the (part of the) iso file to parse
 * @param contentSize         expected contentSize of the box
 * @param boxParser           creates inner boxes
 * @throws IOException in case of an I/O error.
 */
@DoNotParseDetail
public void parse(ReadableByteChannel readableByteChannel, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    if (readableByteChannel instanceof FileChannel && contentSize > MEM_MAP_THRESHOLD) {
        // todo: if I map this here delayed I could use transferFrom/transferTo in the getBox method
        // todo: potentially this could speed up writing.
        //
        // It's quite expensive to map a file into the memory. Just do it when the box is larger than a MB.
        content = ((FileChannel) readableByteChannel).map(FileChannel.MapMode.READ_ONLY, ((FileChannel) readableByteChannel).position(), contentSize);
        ((FileChannel) readableByteChannel).position(((FileChannel) readableByteChannel).position() + contentSize);
    } else {
        assert contentSize < Integer.MAX_VALUE;
        content = ChannelHelper.readFully(readableByteChannel, contentSize);
    }
    if (isParsed() == false) {
        parseDetails();
    }

}
 
开发者ID:lisnstatic,项目名称:live_master,代码行数:27,代码来源:AbstractBox.java

示例3: startReading

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
@Override
protected void startReading(ReadableByteChannel channel) throws IOException {
  this.inChannel = channel;
  // If the first offset is greater than zero, we need to skip bytes until we see our
  // first separator.
  if (getCurrentSource().getStartOffset() > 0) {
    checkState(channel instanceof SeekableByteChannel,
        "%s only supports reading from a SeekableByteChannel when given a start offset"
        + " greater than 0.", RecordFileSource.class.getSimpleName());
    long requiredPosition = getCurrentSource().getStartOffset() - 1;
    ((SeekableByteChannel) channel).position(requiredPosition);
    findSeparatorBounds();
    buffer = buffer.substring(endOfSeparatorInBuffer);
    startOfNextRecord = requiredPosition + endOfSeparatorInBuffer;
    endOfSeparatorInBuffer = 0;
    startOfSeparatorInBuffer = 0;
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:dataflow-opinion-analysis,代码行数:19,代码来源:RecordFileSource.java

示例4: parse

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
@Override
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    dataSource.read(buffer);
    buffer.rewind();
    version = IsoTypeReader.readUInt8(buffer);
    flags = IsoTypeReader.readUInt24(buffer);

    int entryCountLength = (version == 0) ? 2 : 4;
    buffer = ByteBuffer.allocate(entryCountLength);
    dataSource.read(buffer);
    buffer.rewind();

    initContainer(dataSource, contentSize - 4 - entryCountLength, boxParser);

    for (ItemInfoEntry entry : getBoxes(ItemInfoEntry.class)) {
        entry.parseDetails();
    }
}
 
开发者ID:yohhoy,项目名称:heifreader,代码行数:20,代码来源:ItemInfoBox.java

示例5: fastCopy

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
/**
     * copy
     *
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(8 * 1024);
        int count = 0;

        while ((count = src.read(buffer)) != -1) {
//            LogUtil.d("luaviewp-fastCopy", count, buffer.capacity(), buffer.remaining(), buffer.array().length);
            // prepare the buffer to be drained
            buffer.flip();
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
 
开发者ID:alibaba,项目名称:LuaViewPlayground,代码行数:29,代码来源:ChannelTools.java

示例6: channelCopy

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
private static void channelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException
{
    final ByteBuffer buffer = ByteBuffer.allocateDirect(2 * 1024);
    while (src.read(buffer) != -1)
    {
        // prepare the buffer to be drained
        buffer.flip();
        // write to the channel, may block
         dest.write(buffer);

        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }

    // EOF will leave buffer in fill state
    buffer.flip();

    // make sure the buffer is fully drained.
    while (buffer.hasRemaining())
    {
        dest.write(buffer);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:25,代码来源:HttpClientTransmitterImpl.java

示例7: getDirectReadableChannel

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
@Override
protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException
{
    try
    {
        // Interpret the URL to generate the text
        InputStream textStream = textGenerator.getInputStream(seed, size, words);
        ReadableByteChannel textChannel = Channels.newChannel(textStream);
        // done
        if (logger.isDebugEnabled())
        {
            logger.debug("Opened read channel to random text for URL: " + getContentUrl());
        }
        return textChannel;
    }
    catch (Throwable e)
    {
        throw new ContentIOException("Failed to read channel: " + this, e);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:21,代码来源:SpoofedTextContentReader.java

示例8: execute

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
/**
 * execute.
 * @param cmd - cmd
 * @throws IOException - IOException
 */
@Override
public void execute(Command cmd) throws IOException {

    ObjectOutputStream oos = new ObjectOutputStream(outputStream);
    DataInputStream dis = new DataInputStream(inputStream);
    oos.writeObject(cmd);
    oos.flush();

    if (SUCCESS.equals(dis.readUTF())) {
        System.out.println("Downloading...");

        ReadableByteChannel rbc = Channels.newChannel(dis);
        FileOutputStream fos = new FileOutputStream(cmd.getParam());
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.flush();

        System.out.println("Done.");
    } else {
        System.out.println("Error. Please try again.");
    }
}
 
开发者ID:istolbov,项目名称:i_stolbov,代码行数:27,代码来源:CommandFactoryClient.java

示例9: testCopyFileChannel

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public void testCopyFileChannel() throws IOException {
  final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  WritableByteChannel outChannel = Channels.newChannel(out);

  File testFile = createTempFile();
  FileOutputStream fos = new FileOutputStream(testFile);
  byte[] dummyData = newPreFilledByteArray(chunkSize);
  try {
    for (int i = 0; i < 500; i++) {
      fos.write(dummyData);
    }
  } finally {
    fos.close();
  }
  ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel();
  try {
    ByteStreams.copy(inChannel, outChannel);
  } finally {
    inChannel.close();
  }
  byte[] actual = out.toByteArray();
  for (int i = 0; i < 500 * chunkSize; i += chunkSize) {
    assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize));
  }
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:27,代码来源:ByteStreamsTest.java

示例10: downloadFigures

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
private static String downloadFigures(Figure figure)
		throws MalformedURLException, IOException, FileNotFoundException {
	String fileName = null;
	Pattern p = Pattern.compile("^((http[s]?|ftp):\\/)?\\/?([^:\\/\\s]+)((\\/\\w+)*\\/)([\\w\\-\\.]+[^#?\\s]+)(.*)?(#[\\w\\-]+)?$");
	Matcher m = p.matcher(figure.getLink().trim());
	if (m.find()) {
		fileName = m.group(6);
		URL website = new URL(m.group());
		System.out.println("----------------------------------------------------");
		System.out.println("Trying download file: " + m.group() + " from the web");
		try {
			ReadableByteChannel rbc = Channels.newChannel(website.openStream());
			FileOutputStream fos = new FileOutputStream(fileName);
			fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
			fos.close();
		} catch (Exception e) {
			System.err.println("Figure was not downloaded");
			e.printStackTrace();
		}
		System.out.println("file was downloaded successfully");
	}
	return fileName;
}
 
开发者ID:Vitaliy-1,项目名称:JATS2LaTeX,代码行数:24,代码来源:BodyStandard.java

示例11: main

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
    ReadableByteChannel rbc = new ReadableByteChannel() {
        public int read(ByteBuffer dst) {
            dst.put((byte)0);
            return 1;
        }
        public boolean isOpen() {
            return true;
        }
        public void close() {
        }
    };

    InputStream in = Channels.newInputStream(rbc);

    byte[] b = new byte[3];
    in.read(b, 0, 1);
    in.read(b, 2, 1);       // throws IAE
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:ReadOffset.java

示例12: main

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
    ReadableByteChannel channel = new ReadableByteChannel() {
        public int read(ByteBuffer dst) {
            dst.put((byte) 129);
            return 1;
        }

        public boolean isOpen() {
            return true;
        }

        public void close() {
        }
    };

    InputStream in = Channels.newInputStream(channel);
    int data = in.read();
    if (data < 0)
        throw new RuntimeException(
            "InputStream.read() spec'd to return 0-255");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:22,代码来源:ReadByte.java

示例13: fromReadableByteChannel

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public static VarLong fromReadableByteChannel(ReadableByteChannel fs) throws IOException{
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ByteBuffer byteBuffer = ByteBuffer.allocate(1);
	while(true){
		byteBuffer.clear();
		if(fs.read(byteBuffer) == -1){// unexpectedly hit the end of the input stream
			throw new IllegalArgumentException("end of InputStream");
		}
		int byteVar = byteBuffer.get(0);
		baos.write(byteVar);
		if(byteVar < 128){
			break;
		}
	}
	return fromByteArray(baos.toByteArray());
}
 
开发者ID:hotpads,项目名称:datarouter,代码行数:17,代码来源:VarLong.java

示例14: LineReader

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public LineReader(final ReadableByteChannel channel)
throws IOException {
    buf = ByteBuffer.allocate(BUF_SIZE);
    buf.flip();

    boolean removeLine = false;
    // If we are not at the beginning of a line, we should ignore the current line.
    if (channel instanceof SeekableByteChannel) {
        SeekableByteChannel seekChannel = (SeekableByteChannel) channel;
        if (seekChannel.position() > 0) {
            // Start from one character back and read till we find a new line.
            seekChannel.position(seekChannel.position() - 1);
            removeLine = true;
        }
        nextLineStart = seekChannel.position();
    }

    this.channel = channel;
    if (removeLine) {
        nextLineStart += readNextLine(new ByteArrayOutputStream());
    }
}
 
开发者ID:obradovicluka,项目名称:dataflow-playground,代码行数:23,代码来源:CsvWithHeaderFileSource.java

示例15: build

import java.nio.channels.ReadableByteChannel; //导入依赖的package包/类
public static Movie build(ReadableByteChannel channel) throws IOException {
    IsoFile isoFile = new IsoFile(channel);
    Movie m = new Movie();
    List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(TrackBox.class);
    for (TrackBox trackBox : trackBoxes) {
        m.addTrack(new Mp4TrackImpl(trackBox));
    }
    return m;
}
 
开发者ID:lisnstatic,项目名称:live_master,代码行数:10,代码来源:MovieCreator.java


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