Java Code Examples for java.nio.channels.Channels#newChannel()

The following examples show how to use java.nio.channels.Channels#newChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnvelopedRecordSetWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
EnvelopedRecordSetWriter(int initialBufferSize,
                         CompressionCodec.Type codec) {
    this.buffer = new Buffer(Math.max(initialBufferSize, HEADER_LEN));
    this.promiseList = new LinkedList<Promise<DLSN>>();
    this.codec = codec;
    switch (codec) {
        case LZ4:
            this.codecCode = COMPRESSION_CODEC_LZ4;
            break;
        default:
            this.codecCode = COMPRESSION_CODEC_NONE;
            break;
    }
    this.writer = new DataOutputStream(buffer);
    try {
        this.writer.writeInt((VERSION & METADATA_VERSION_MASK)
                | (codecCode & METADATA_COMPRESSION_MASK));
        this.writer.writeInt(0); // count
        this.writer.writeInt(0); // original len
        this.writer.writeInt(0); // actual len
    } catch (IOException e) {
        logger.warn("Failed to serialize the header to an enveloped record set", e);
    }
    this.writeChannel = Channels.newChannel(writer);
}
 
Example 2
Source File: H264AnnexBTrackTest.java    From mp4parser with Apache License 2.0 6 votes vote down vote up
@Test
    public void testMuxing() throws Exception {
        H264AnnexBTrack b = new H264AnnexBTrack(H264AnnexBTrackTest.class.getResourceAsStream("/org/mp4parser/streaming/input/h264/tos.h264"));
        //H264AnnexBTrack b = new H264AnnexBTrack(new FileInputStream("C:\\dev\\mp4parser\\out.264"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FragmentedMp4Writer writer = new FragmentedMp4Writer(Collections.<StreamingTrack>singletonList(b), Channels.newChannel(baos));
        //MultiTrackFragmentedMp4Writer writer = new MultiTrackFragmentedMp4Writer(new StreamingTrack[]{b}, new ByteArrayOutputStream());
        b.call();
        writer.close();
        IsoFile isoFile = new IsoFile(Channels.newChannel(new ByteArrayInputStream(baos.toByteArray())));
        Walk.through(isoFile);
        List<Sample> s = new Mp4SampleList(1, isoFile, new InMemRandomAccessSourceImpl(baos.toByteArray()));
        for (Sample sample : s) {
//            System.err.println("s: " + sample.getSize());
            sample.asByteBuffer();
        }
    }
 
Example 3
Source File: ChannelWriterTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
@Test
public void ChannelWriteFunctionalityTest() throws IOException {
  byte[] buf = new byte[1000];
  new Random().nextBytes(buf);
  ByteBuffer buffer = ByteBuffer.wrap(buf);
  ByteBufferInputStream stream = new ByteBufferInputStream(buffer);
  ByteBuffer output = ByteBuffer.allocate(10000);
  ByteBufferOutputStream outputstream = new ByteBufferOutputStream(output);
  WritableByteChannel channel = Channels.newChannel(outputstream);
  ChannelWriter writer = new ChannelWriter(channel);
  writer.writeInt(1000);
  writer.writeLong(10000);
  writer.writeShort((short) 5);
  writer.writeString("check");
  writer.writeStream(stream, 1000);
  output.flip();
  Assert.assertEquals(1000, output.getInt());
  Assert.assertEquals(10000, output.getLong());
  Assert.assertEquals(5, output.getShort());
  byte[] stringout = new byte[5];
  System.arraycopy(output.array(), 14, stringout, 0, 5);
  // Assert.assertArrayEquals(stringout, "check".getBytes());
  // byte[] streamout = new byte[1000];
  //System.arraycopy(output.array(), 19, streamout, 0, 1000);
  //Assert.assertArrayEquals(streamout, buf);
}
 
Example 4
Source File: DownloadServerFiles.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Downloads required Minecraft server jar from the Minecraft cdn.
 */
public static void downloadMinecraftServer() {
    String fileName = "minecraft_server.1.12.2.jar";
    String downloadLink = "https://launcher.mojang.com/v1/objects/886945bfb2b978778c3a0288fd7fab09d315b25f/server.jar";

    File minecraftServerJar = new File(fileName);
    if (!minecraftServerJar.exists() && !minecraftServerJar.isDirectory()) {
        System.out.println("Downloading Minecraft Server Jar...");
        try {
            URL website = new URL(downloadLink);
            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream(fileName);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example 5
Source File: StreamingDataGenerator.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() throws IOException {
  dataGenerator = new JsonDataGeneratorImpl();

  Metadata metadata = FileSystems.matchSingleFileSpec(schemaLocation);

  // Copy the schema file into a string which can be used for generation.
  try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    try (ReadableByteChannel readerChannel = FileSystems.open(metadata.resourceId())) {
      try (WritableByteChannel writerChannel = Channels.newChannel(byteArrayOutputStream)) {
        ByteStreams.copy(readerChannel, writerChannel);
      }
    }

    schema = byteArrayOutputStream.toString();
  }
}
 
Example 6
Source File: AbstractCryptoStreamTest.java    From chimera with Apache License 2.0 5 votes vote down vote up
protected CryptoOutputStream getCryptoOutputStream(ByteArrayOutputStream baos,
                                                 Cipher cipher,
                                                 int bufferSize, byte[] iv,
                                                 boolean withChannel) throws
    IOException {
  if (withChannel) {
    return new CryptoOutputStream(Channels.newChannel(baos), cipher,
        bufferSize, key, iv);
  } else {
    return new CryptoOutputStream(baos, cipher, bufferSize, key, iv);
  }
}
 
Example 7
Source File: MavenWrapperDownloader.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 8
Source File: ReadableChannelDictionaryType.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ReadableByteChannel getReadableChannel() throws UnsupportedEncodingException {
	byte[] byteArray;
	if (StringUtils.isEmpty(charset)) {
		byteArray = data.getBytes(Defaults.DataFormatter.DEFAULT_CHARSET_ENCODER);
	} else {
		byteArray = data.getBytes(charset);
	}
	
	return Channels.newChannel(new ByteArrayInputStream(byteArray));
}
 
Example 9
Source File: MavenWrapperDownloader.java    From springboot-kafka-avro with MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 10
Source File: MavenWrapperDownloader.java    From spring-boot-demo with MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 11
Source File: MavenWrapperDownloader.java    From istio-fleetman with MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 12
Source File: PlunginGuesser.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
public static void getJoomlaThemeDB() throws IOException {
    URL website =
            new URL(
                    "https://fuzzdb.googlecode.com/svn/trunk/Discovery/PredictableRes/CMS/joomla_themes.fuzz.txt");
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    @SuppressWarnings("resource")
    FileOutputStream fos = new FileOutputStream("resources/pluginEnum/joomla_themes.txt");
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
 
Example 13
Source File: MavenWrapperDownloader.java    From springcloud_for_noob with MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 14
Source File: Server.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Process a wrapped RPC Request - unwrap the SASL packet and process
 * each embedded RPC request 
 * @param buf - SASL wrapped request of one or more RPCs
 * @throws IOException - SASL packet cannot be unwrapped
 * @throws InterruptedException
 */    
private void unwrapPacketAndProcessRpcs(byte[] inBuf)
    throws WrappedRpcServerException, IOException, InterruptedException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Have read input token of size " + inBuf.length
        + " for processing by saslServer.unwrap()");
  }
  inBuf = saslServer.unwrap(inBuf, 0, inBuf.length);
  ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(
      inBuf));
  // Read all RPCs contained in the inBuf, even partial ones
  while (true) {
    int count = -1;
    if (unwrappedDataLengthBuffer.remaining() > 0) {
      count = channelRead(ch, unwrappedDataLengthBuffer);
      if (count <= 0 || unwrappedDataLengthBuffer.remaining() > 0)
        return;
    }

    if (unwrappedData == null) {
      unwrappedDataLengthBuffer.flip();
      int unwrappedDataLength = unwrappedDataLengthBuffer.getInt();
      unwrappedData = ByteBuffer.allocate(unwrappedDataLength);
    }

    count = channelRead(ch, unwrappedData);
    if (count <= 0 || unwrappedData.remaining() > 0)
      return;

    if (unwrappedData.remaining() == 0) {
      unwrappedDataLengthBuffer.clear();
      unwrappedData.flip();
      processOneRpc(unwrappedData.array());
      unwrappedData = null;
    }
  }
}
 
Example 15
Source File: SuccinctBuffer.java    From succinct with Apache License 2.0 5 votes vote down vote up
/**
 * Write Succinct data structures to a DataOutputStream.
 *
 * @param os Output stream to write data to.
 * @throws IOException
 */
public void writeToStream(DataOutputStream os) throws IOException {
  WritableByteChannel dataChannel = Channels.newChannel(os);

  os.writeInt(getOriginalSize());
  os.writeInt(getSamplingRateSA());
  os.writeInt(getSamplingRateISA());
  os.writeInt(getSamplingRateNPA());
  os.writeInt(getSampleBitWidth());
  os.writeInt(getAlphabetSize());

  for (int i = 0; i < getAlphabetSize(); i++) {
    os.writeInt(alphabet[i]);
  }

  for (int i = 0; i < sa.limit(); i++) {
    os.writeLong(sa.get(i));
  }

  for (int i = 0; i < isa.limit(); i++) {
    os.writeLong(isa.get(i));
  }

  for (int i = 0; i < columnoffsets.limit(); i++) {
    os.writeInt(columnoffsets.get(i));
  }

  for (int i = 0; i < columns.length; i++) {
    os.writeInt(columns[i].limit());
    dataChannel.write(columns[i].order(ByteOrder.BIG_ENDIAN));
    columns[i].rewind();
  }
}
 
Example 16
Source File: MavenWrapperDownloader.java    From springcloud_for_noob with MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 17
Source File: MavenWrapperDownloader.java    From CourseSchedulingSystem with GNU General Public License v3.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 18
Source File: IOUtils.java    From jsch-extension with MIT License 5 votes vote down vote up
public static void copy( InputStream from, OutputStream to ) throws IOException {
    ReadableByteChannel in = Channels.newChannel( from );
    WritableByteChannel out = Channels.newChannel( to );

    final ByteBuffer buffer = ByteBuffer.allocateDirect( 16 * 1024 );
    while ( in.read( buffer ) != -1 ) {
        buffer.flip();
        out.write( buffer );
        buffer.compact();
    }
    buffer.flip();
    while ( buffer.hasRemaining() ) {
        out.write( buffer );
    }
}
 
Example 19
Source File: DurableOutput.java    From bifurcan with MIT License 4 votes vote down vote up
/**
 * @return an output wrapped around {@code os}
 */
static DurableOutput from(OutputStream os) {
  return new ByteChannelOutput(Channels.newChannel(os), 16 << 10);
}
 
Example 20
Source File: ByteBufferOutputStream.java    From hbase with Apache License 2.0 3 votes vote down vote up
/**
* Writes the complete contents of this byte buffer output stream to
* the specified output stream argument.
*
* @param      out   the output stream to which to write the data.
* @exception  IOException  if an I/O error occurs.
*/
public void writeTo(OutputStream out) throws IOException {
  WritableByteChannel channel = Channels.newChannel(out);
  ByteBuffer bb = curBuf.duplicate();
  bb.flip();
  channel.write(bb);
}