當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。