本文整理匯總了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);
}
示例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();
}
}
示例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;
}
}
示例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();
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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.");
}
}
示例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));
}
}
示例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;
}
示例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
}
示例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");
}
示例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());
}
示例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());
}
}
示例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;
}