Java Code Examples for java.nio.channels.ReadableByteChannel

The following are top voted examples for showing how to use java.nio.channels.ReadableByteChannel. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: hadoop-oss   File: CryptoInputStream.java   Source Code and License 6 votes vote down vote up
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);
}
 
Example 2
Project: live_master   File: AbstractBox.java   Source Code and License 6 votes vote down vote up
/**
 * 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();
    }

}
 
Example 3
Project: dataflow-opinion-analysis   File: RecordFileSource.java   Source Code and License 6 votes vote down vote up
@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;
  }
}
 
Example 4
Project: heifreader   File: ItemInfoBox.java   Source Code and License 6 votes vote down vote up
@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();
    }
}
 
Example 5
Project: LuaViewPlayground   File: ChannelTools.java   Source Code and License 6 votes vote down vote up
/**
     * 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);
        }
    }
 
Example 6
Project: alfresco-repository   File: HttpClientTransmitterImpl.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 7
Project: alfresco-repository   File: SpoofedTextContentReader.java   Source Code and License 6 votes vote down vote up
@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);
    }
}
 
Example 8
Project: i_stolbov   File: CommandFactoryClient.java   Source Code and License 6 votes vote down vote up
/**
 * 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.");
    }
}
 
Example 9
Project: googles-monorepo-demo   File: ByteStreamsTest.java   Source Code and License 6 votes vote down vote up
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));
  }
}
 
Example 10
Project: guava-mock   File: ByteStreamsTest.java   Source Code and License 6 votes vote down vote up
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));
  }
}
 
Example 11
Project: JATS2LaTeX   File: BodyStandard.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 12
Project: jdk8u-jdk   File: ReadOffset.java   Source Code and License 6 votes vote down vote up
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
}
 
Example 13
Project: jdk8u-jdk   File: ReadByte.java   Source Code and License 6 votes vote down vote up
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");
}
 
Example 14
Project: mp4parser_android   File: AbstractBox.java   Source Code and License 6 votes vote down vote up
/**
 * 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();
    }

}
 
Example 15
Project: datarouter   File: VarLong.java   Source Code and License 6 votes vote down vote up
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());
}
 
Example 16
Project: dataflow-playground   File: CsvWithHeaderFileSource.java   Source Code and License 6 votes vote down vote up
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());
    }
}
 
Example 17
Project: openjdk-jdk10   File: ReadByte.java   Source Code and License 6 votes vote down vote up
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");
}
 
Example 18
Project: live_master   File: MovieCreator.java   Source Code and License 5 votes vote down vote up
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;
}
 
Example 19
Project: live_master   File: IsoFile.java   Source Code and License 5 votes vote down vote up
public IsoFile(ReadableByteChannel byteChannel, BoxParser boxParser) throws IOException {
    super("");
    this.byteChannel = byteChannel;
    this.boxParser = boxParser;
    parse();


}
 
Example 20
Project: nifi-jms-jndi   File: TestUtils.java   Source Code and License 5 votes vote down vote up
static String setupActiveMqLibForTesting(boolean clean) {
    String[] urlsStrings = new String[]{
            "http://central.maven.org/maven2/org/apache/activemq/activemq-client/5.13.0/activemq-client-5.13.0.jar",
            "http://central.maven.org/maven2/org/apache/activemq/activemq-broker/5.13.0/activemq-broker-5.13.0.jar",
            "http://central.maven.org/maven2/org/apache/geronimo/specs/geronimo-j2ee-management_1.0_spec/1.0.1/geronimo-j2ee-management_1.0_spec-1.0.1.jar",
            "http://central.maven.org/maven2/org/fusesource/hawtbuf/hawtbuf/1.11/hawtbuf-1.11.jar" };

    try {
        File activeMqLib = new File("target/active-mq-lib");
        if (activeMqLib.exists() && clean) {
            FileUtils.deleteDirectory(activeMqLib);
        }
        activeMqLib.mkdirs();
        for (String urlString : urlsStrings) {
            URL url = new URL(urlString);
            String path = url.getPath();
            path = path.substring(path.lastIndexOf("/") + 1);
            logger.info("Downloading: " + path);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            try (FileOutputStream fos = new FileOutputStream(new File(activeMqLib, path))) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                fos.close();
            }
        }
        return activeMqLib.getAbsolutePath();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to download ActiveMQ libraries.", e);
    }
}
 
Example 21
Project: live_master   File: MediaDataBox.java   Source Code and License 5 votes vote down vote up
public void parse(ReadableByteChannel readableByteChannel, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    this.header = header;
    this.contentSize = contentSize;

    if (readableByteChannel instanceof FileChannel && (contentSize > AbstractBox.MEM_MAP_THRESHOLD)) {
        this.fileChannel = ((FileChannel) readableByteChannel);
        this.startPosition = ((FileChannel) readableByteChannel).position();
        ((FileChannel) readableByteChannel).position(((FileChannel) readableByteChannel).position() + contentSize);
    } else {
        content = ChannelHelper.readFully(readableByteChannel, l2i(contentSize));
        cache.put(0l, new SoftReference<ByteBuffer>(content));
    }
}
 
Example 22
Project: openrouteservice   File: RoutingProfilesUpdater.java   Source Code and License 5 votes vote down vote up
private void downloadFile(String url, File destination) {
	try {
		URL website = new URL(url);
		ReadableByteChannel rbc = Channels.newChannel(website.openStream());
		FileOutputStream fos = new FileOutputStream(destination);
		fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
		fos.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 23
Project: hadoop-oss   File: SocketInputWrapper.java   Source Code and License 5 votes vote down vote up
/**
 * @return an underlying ReadableByteChannel implementation.
 * @throws IllegalStateException if this socket does not have a channel
 */
public ReadableByteChannel getReadableByteChannel() {
  Preconditions.checkState(hasChannel,
      "Socket %s does not have a channel",
      this.socket);
  return (SocketInputStream)in;
}
 
Example 24
Project: hadoop-oss   File: Server.java   Source Code and License 5 votes vote down vote up
/**
 * Helper for {@link #channelRead(ReadableByteChannel, ByteBuffer)}
 * and {@link #channelWrite(WritableByteChannel, ByteBuffer)}. Only
 * one of readCh or writeCh should be non-null.
 * 
 * @see #channelRead(ReadableByteChannel, ByteBuffer)
 * @see #channelWrite(WritableByteChannel, ByteBuffer)
 */
private static int channelIO(ReadableByteChannel readCh, 
                             WritableByteChannel writeCh,
                             ByteBuffer buf) throws IOException {
  
  int originalLimit = buf.limit();
  int initialRemaining = buf.remaining();
  int ret = 0;
  
  while (buf.remaining() > 0) {
    try {
      int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
      buf.limit(buf.position() + ioSize);
      
      ret = (readCh == null) ? writeCh.write(buf) : readCh.read(buf); 
      
      if (ret < ioSize) {
        break;
      }

    } finally {
      buf.limit(originalLimit);        
    }
  }

  int nBytes = initialRemaining - buf.remaining(); 
  return (nBytes > 0) ? nBytes : ret;
}
 
Example 25
Project: heifreader   File: ItemPropertiesBox.java   Source Code and License 5 votes vote down vote up
@Override
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    initContainer(dataSource, contentSize, boxParser);
    for (AbstractBox box : this.getBoxes(AbstractBox.class)) {
        box.parseDetails();
    }
}
 
Example 26
Project: heifreader   File: ItemPropertyContainerBox.java   Source Code and License 5 votes vote down vote up
@Override
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    initContainer(dataSource, contentSize, boxParser);
    for (AbstractBox box : this.getBoxes(AbstractBox.class)) {
        box.parseDetails();
    }
}
 
Example 27
Project: rcom   File: RoundBuffer.java   Source Code and License 5 votes vote down vote up
@Override
public int read(ReadableByteChannel bc, int remainingBytes) throws IOException {
	int n=Math.min(remainingBytes, buffer.capacity()-buffer.position());
	List<Send> toactivate=null;
	synchronized (buffer) {
		buffer.limit(buffer.position()+n);
		n=bc.read(buffer);
		if(n>0)
		{
			nRead+=n;
			toactivate=RoundBuffer.this.senders;
		}
		if(buffer.position()==buffer.capacity())
		{
			// Buffer is filled. We restart on the other end.
			buffer.position(0);
		}
	}
	if(toactivate!=null)
	{
		for(Send s: toactivate)
		{
			s.dataAvailable();
		}
	}
	return n;
}
 
Example 28
Project: hadoop   File: PacketReceiver.java   Source Code and License 5 votes vote down vote up
private static void doReadFully(ReadableByteChannel ch, InputStream in,
    ByteBuffer buf) throws IOException {
  if (ch != null) {
    readChannelFully(ch, buf);
  } else {
    Preconditions.checkState(!buf.isDirect(),
        "Must not use direct buffers with InputStream API");
    IOUtils.readFully(in, buf.array(),
        buf.arrayOffset() + buf.position(),
        buf.remaining());
    buf.position(buf.position() + buf.remaining());
  }
}
 
Example 29
Project: AutoUpdaterAPI   File: UtilDownloader.java   Source Code and License 5 votes vote down vote up
public static void downloadFile(String url, String location) throws IOException {
    URL website = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    File yourFile = new File(location);
    yourFile.getParentFile().mkdirs();
    if (!yourFile.exists()) {
        yourFile.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(yourFile);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
}
 
Example 30
Project: chromium-net-for-android   File: ChromiumUrlRequest.java   Source Code and License 5 votes vote down vote up
/**
 * Sets a readable byte channel to upload as part of a POST or PUT request.
 *
 * @param contentType MIME type of the upload content or null if this is not
 *            an upload request.
 * @param channel The channel to read to read upload data from if this is an
 *            upload request.
 * @param contentLength The length of data to upload.
 */
@Override
public void setUploadChannel(
        String contentType, ReadableByteChannel channel, long contentLength) {
    synchronized (mLock) {
        validateNotStarted();
        validateContentType(contentType);
        mUploadContentType = contentType;
        mUploadChannel = channel;
        mUploadContentLength = contentLength;
        mUploadData = null;
        mChunkedUpload = false;
    }
}
 
Example 31
Project: Parabot-317-API-Minified-OS-Scape   File: NetUtil.java   Source Code and License 5 votes vote down vote up
public static boolean downloadFile(String url, String location) {
    try {

        final URLConnection connection = createURLConnection(url);

        final int  contentLength = connection.getContentLength();
        final File destination   = new File(location);

        if (destination.exists()) {
            final URLConnection savedFileConnection = destination.toURI().toURL().openConnection();
            if (savedFileConnection.getContentLength() == contentLength) {
                return true;
            }
        } else {
            final File parent = destination.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
        }

        final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());

        final FileOutputStream fos = new FileOutputStream(destination);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();

    } catch (IOException exception) {
        exception.printStackTrace();
        return false;
    }

    System.out.println(url + "->" + location);
    return new File(location).exists();
}
 
Example 32
Project: chromium-net-for-android   File: HttpUrlConnectionUrlRequest.java   Source Code and License 5 votes vote down vote up
@Override
public void setUploadChannel(String contentType,
        ReadableByteChannel channel, long contentLength) {
    validateNotStarted();
    if (contentLength > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(
            "Upload contentLength is too big.");
    }
    mUploadContentLength = (int) contentLength;
    mPostContentType = contentType;
    mPostDataChannel = channel;
    mPostData = null;
}
 
Example 33
Project: chromium-net-for-android   File: UploadTest.java   Source Code and License 5 votes vote down vote up
/**
 * Sets request to have an upload channel containing the given data.
 * uploadDataLength should generally be uploadData.length(), unless a test
 * needs to get a read error.
 */
private void setUploadChannel(HttpUrlRequest request,
                              String contentType,
                              String uploadData,
                              int uploadDataLength) {
    InputStream uploadDataStream = new ByteArrayInputStream(
            uploadData.getBytes());
    ReadableByteChannel uploadDataChannel =
            Channels.newChannel(uploadDataStream);
    request.setUploadChannel(
            contentType, uploadDataChannel, uploadDataLength);
}
 
Example 34
Project: live_master   File: ChannelHelper.java   Source Code and License 5 votes vote down vote up
public static int readFully(final ReadableByteChannel channel, final ByteBuffer buf, final int length)
        throws IOException {
    int n, count = 0;
    while (-1 != (n = channel.read(buf))) {
        count += n;
        if (count == length) {
            break;
        }
    }
    if (n == -1) {
        throw new EOFException("End of file. No more boxes.");
    }
    return count;
}
 
Example 35
Project: guava-mock   File: ByteStreamsTest.java   Source Code and License 5 votes vote down vote up
public void testCopyChannel() throws IOException {
  byte[] expected = newPreFilledByteArray(100);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  WritableByteChannel outChannel = Channels.newChannel(out);

  ReadableByteChannel inChannel =
      Channels.newChannel(new ByteArrayInputStream(expected));
  ByteStreams.copy(inChannel, outChannel);
  assertEquals(expected, out.toByteArray());
}
 
Example 36
Project: V8LogScanner   File: RgxReader.java   Source Code and License 5 votes vote down vote up
public RgxReader(String fileName, Charset charset, int _limit) throws FileNotFoundException {

        this(CharBuffer.allocate(1024));

        fs = new FileInputStream(fileName);
        ReadableByteChannel channel = fs.getChannel();
        CharsetDecoder decoder = charset.newDecoder();
        reader = Channels.newReader(channel, decoder, -1);
        buf.limit(0);
        limit = _limit;
    }
 
Example 37
Project: gnirehtet   File: Packetizer.java   Source Code and License 5 votes vote down vote up
public IPv4Packet packetize(ReadableByteChannel channel, int maxChunkSize) throws IOException {
    payloadBuffer.limit(maxChunkSize).position(0);
    int payloadLength = channel.read(payloadBuffer);
    if (payloadLength == -1) {
        return null;
    }
    payloadBuffer.flip();
    return inflate();
}
 
Example 38
Project: hadoop   File: PacketReceiver.java   Source Code and License 5 votes vote down vote up
private static void readChannelFully(ReadableByteChannel ch, ByteBuffer buf)
    throws IOException {
  while (buf.remaining() > 0) {
    int n = ch.read(buf);
    if (n < 0) {
      throw new IOException("Premature EOF reading from " + ch);
    }
  }
}
 
Example 39
Project: gemoc-studio   File: IFileUtils.java   Source Code and License 5 votes vote down vote up
private static boolean isStreamEqual(InputStream i1, InputStream i2)
        throws IOException {

    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);

    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

    try {
        while (true) {

            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);

            if (n1 == -1 || n2 == -1) return n1 == n2;

            buf1.flip();
            buf2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++)
                if (buf1.get() != buf2.get())
                    return false;

            buf1.compact();
            buf2.compact();
        }

    } finally {
        if (i1 != null) i1.close();
        if (i2 != null) i2.close();
    }
}
 
Example 40
Project: KernelHive   File: JarFileLoaderService.java   Source Code and License 5 votes vote down vote up
public void downloadJar() throws IOException {
	final ReadableByteChannel channel = Channels.newChannel(url
			.openStream());
	file = File.createTempFile("kh." + UUID.randomUUID(), ".jar");
	file.deleteOnExit();
	final FileOutputStream fos = new FileOutputStream(file);
	while (fos.getChannel().transferFrom(channel, 0, 1 << 24) > 0)
		;
	fos.flush();
	fos.close();
}