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

The following examples show how to use java.nio.channels.Channels#newInputStream() . 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: OperationHandlerTestTemplate.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected String read(ReadableByteChannel channel) {
	InputStream input = null;
	ByteArrayOutputStream output = null;
	try {
		input = Channels.newInputStream(channel);	
		output = new ByteArrayOutputStream();
		copy(input, output);
	} catch (Throwable t) {
		assumeNoException(t);
	} finally {
		try {
			FileUtils.closeAll(input, output);
		} catch (IOException e) {
			assumeNoException(e);
		}
		if (channel != null) {
			try {
				channel.close();
			} catch (IOException ioe) {}
		}
	}
	return new String(output.toByteArray(), charset);
}
 
Example 2
Source File: ReadByte.java    From openjdk-jdk8u with GNU General Public License v2.0 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 3
Source File: AvroToCsv.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private static String getSchema(String schemaPath) throws IOException {
  ReadableByteChannel channel = FileSystems.open(FileSystems.matchNewResource(
      schemaPath, false));

  try (InputStream stream = Channels.newInputStream(channel)) {
    BufferedReader streamReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    StringBuilder dataBuilder = new StringBuilder();

    String line;
    while ((line = streamReader.readLine()) != null) {
      dataBuilder.append(line);
    }

    return dataBuilder.toString();
  }
}
 
Example 4
Source File: ReadOffset.java    From jdk8u-dev-jdk with GNU General Public License v2.0 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 5
Source File: CatOLog.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
private static void openFileAndParseEntries(File inputLogFile,
                                            HeaderWithCrcValidity doWithHeader,
                                            EntryWithAddress doForEach) throws IOException {
  try (BytePersistence persistence = new FilePersistence(inputLogFile.toPath());
       PersistenceReader reader = persistence.getReader();
       InputStream inputStream = Channels.newInputStream(reader)) {

    decodeAndUseLogHeader(inputStream, doWithHeader);

    //noinspection InfiniteLoopStatement
    do {
      long address = reader.position();
      OLogEntryDescription entry = CODEC.decode(inputStream);
      doForEach.accept(address, entry);
    } while (true);
  } catch (EOFException ignore) {
  }
}
 
Example 6
Source File: NamedPipe.java    From docker-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getInputStream() throws IOException {
    if (!channel.isOpen()) {
        throw new SocketException("Socket is closed");
    }

    if (inputShutdown) {
        throw new SocketException("Socket input is shutdown");
    }

    return new FilterInputStream(Channels.newInputStream(channel)) {

    	@Override
    	public int read(byte[] b, int off, int len) throws IOException {
    		int readed = super.read(b, off, len);
            log.debug("RESPONSE %s", new String(b, off, len, Charset.forName("UTF-8")));
            return readed;
    	}

        @Override
        public void close() throws IOException {
            shutdownInput();
        }
    };
}
 
Example 7
Source File: ReadByte.java    From TencentKona-8 with GNU General Public License v2.0 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 8
Source File: ReadOffset.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 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 9
Source File: CharReadingBenchmark.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Benchmark
public void intputStreamBytes(Blackhole blackhole) throws IOException {
    byte[] buffer = new byte[bufferSize];

    try (FileChannel channel = FileChannel.open(file.toPath())) {
        try (InputStream is = Channels.newInputStream(channel)) {
            int l;
            while ((l = is.read(buffer)) != -1) {
                blackhole.consume(buffer);
            }
        }
    }
}
 
Example 10
Source File: GoogleStorageResource.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getInputStream() throws IOException {
	if (isBucket()) {
		throw new IllegalStateException(
				"Cannot open an input stream to a bucket: '" + getURI() + "'");
	}
	else {
		return Channels.newInputStream(throwExceptionForNullBlob(getBlob()).reader());
	}
}
 
Example 11
Source File: InputStreamBenchmark.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Benchmark
public void testFileChannelViaRandomFile(Blackhole blackhole) throws IOException {
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
        try (FileChannel open = randomAccessFile.getChannel()) {
            try (InputStream inputStream = Channels.newInputStream(open)) {
                consume(inputStream, blackhole);
            }
        }
    }
}
 
Example 12
Source File: FileByteIterable.java    From xodus with Apache License 2.0 4 votes vote down vote up
public InputStream asStream() throws IOException {
    return Channels.newInputStream(openChannel());
}
 
Example 13
Source File: Parser.java    From science-parse with Apache License 2.0 4 votes vote down vote up
public Parser(
        final File modelFile,
        final File gazetteerFile,
        final File bibModelFile
) throws Exception {
  // Load main model in one thread, and the rest in another thread, to speed up startup.
  final AtomicReference<Exception> exceptionThrownByModelLoaderThread = new AtomicReference<>();
  final Thread modelLoaderThread = new Thread(new Runnable() {
    @Override
    public void run() {
      logger.info("Loading model from {}", modelFile);
      try(
          final DataInputStream modelIs =
              new DataInputStream(new FileInputStream(modelFile));
      ) {
        model = loadModel(modelIs);
        logger.info("Loaded model from {}", modelFile);
      } catch(final Exception e) {
        exceptionThrownByModelLoaderThread.compareAndSet(null, e);
        logger.warn("Failed loading model from {}", modelFile);
      }
    }
  }, "ModelLoaderThread");
  modelLoaderThread.start();

  // Load non-main model stuff
  logger.info("Loading gazetteer from {}", gazetteerFile);
  logger.info("Loading bib model from {}", bibModelFile);
  try(
    final InputStream gazetteerIs = new FileInputStream(gazetteerFile);
    final DataInputStream bibModelIs = new DataInputStream(new FileInputStream(bibModelFile))
  ) {
    // Loading the gazetteer takes a long time, so we create a cached binary version of it that
    // loads very quickly. If that version is already there, we use it. Otherwise, we create it.
    val gazCacheFilename = String.format(
        "%s-%08x.gazetteerCache.bin",
        gazetteerFile.getName(),
        gazetteerFile.getCanonicalPath().hashCode());
    val gazCachePath = Paths.get(System.getProperty("java.io.tmpdir"), gazCacheFilename);
    try (
      final RandomAccessFile gazCacheFile = new RandomAccessFile(gazCachePath.toFile(), "rw");
      final FileChannel gazCacheChannel = gazCacheFile.getChannel();
      final FileLock gazCacheLock = gazCacheChannel.lock();
    ) {
      if (gazCacheChannel.size() == 0) {
        logger.info("Creating gazetteer cache at {}", gazCachePath);
        referenceExtractor =
            ExtractReferences.createAndWriteGazCache(
                gazetteerIs,
                bibModelIs,
                Channels.newOutputStream(gazCacheChannel));
      } else {
        logger.info("Reading from gazetteer cache at {}", gazCachePath);
        referenceExtractor = new ExtractReferences(
            gazetteerIs,
            bibModelIs,
            Channels.newInputStream(gazCacheChannel));
      }
    }
  }
  logger.info("Loaded gazetteer from {}", gazetteerFile);
  logger.info("Loaded bib model from {}", bibModelFile);

  // Close out the model loader thread and make sure the results are OK.
  modelLoaderThread.join();
  if(exceptionThrownByModelLoaderThread.get() != null)
    throw exceptionThrownByModelLoaderThread.get();
  assert(model != null);
}
 
Example 14
Source File: BeamFileInputStream.java    From gcp-ingestion with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Open {@code path} using beam's {@link FileSystems}.
 */
public static InputStream open(String path) throws IOException {
  return Channels
      .newInputStream(FileSystems.open(FileSystems.matchSingleFileSpec(path).resourceId()));
}
 
Example 15
Source File: VideoParameterSet.java    From mp4parser with Apache License 2.0 4 votes vote down vote up
public VideoParameterSet(ByteBuffer vps) throws IOException {
    this.vps = vps;
    CAVLCReader r = new CAVLCReader(Channels.newInputStream(new ByteBufferByteChannel((ByteBuffer) ((Buffer)vps).position(0))));
    vps_parameter_set_id = r.readU(4, "vps_parameter_set_id");
    int vps_reserved_three_2bits = r.readU(2, "vps_reserved_three_2bits");
    int vps_max_layers_minus1 = r.readU(6, "vps_max_layers_minus1");
    int vps_max_sub_layers_minus1 = r.readU(3, "vps_max_sub_layers_minus1");
    boolean vps_temporal_id_nesting_flag = r.readBool("vps_temporal_id_nesting_flag");
    int vps_reserved_0xffff_16bits = r.readU(16, "vps_reserved_0xffff_16bits");
    profile_tier_level(vps_max_sub_layers_minus1, r);


    boolean vps_sub_layer_ordering_info_present_flag = r.readBool("vps_sub_layer_ordering_info_present_flag");
    int[] vps_max_dec_pic_buffering_minus1 = new int[vps_sub_layer_ordering_info_present_flag ? 1 : vps_max_sub_layers_minus1 + 1];
    int[] vps_max_num_reorder_pics = new int[vps_sub_layer_ordering_info_present_flag ? 1 : vps_max_sub_layers_minus1 + 1];
    int[] vps_max_latency_increase_plus1 = new int[vps_sub_layer_ordering_info_present_flag ? 1 : vps_max_sub_layers_minus1 + 1];
    for (int i = (vps_sub_layer_ordering_info_present_flag ? 0 : vps_max_sub_layers_minus1); i <= vps_max_sub_layers_minus1; i++) {
        vps_max_dec_pic_buffering_minus1[i] = r.readUE("vps_max_dec_pic_buffering_minus1[" + i + "]");
        vps_max_num_reorder_pics[i] = r.readUE("vps_max_dec_pic_buffering_minus1[" + i + "]");
        vps_max_latency_increase_plus1[i] = r.readUE("vps_max_dec_pic_buffering_minus1[" + i + "]");
    }
    int vps_max_layer_id = r.readU(6, "vps_max_layer_id");
    int vps_num_layer_sets_minus1 = r.readUE("vps_num_layer_sets_minus1");
    boolean[][] layer_id_included_flag = new boolean[vps_num_layer_sets_minus1][vps_max_layer_id];
    for (int i = 1; i <= vps_num_layer_sets_minus1; i++) {
        for (int j = 0; j <= vps_max_layer_id; j++) {
            layer_id_included_flag[i][j] = r.readBool("layer_id_included_flag[" + i + "][" + j + "]");
        }
    }
    boolean vps_timing_info_present_flag = r.readBool("vps_timing_info_present_flag");
    if (vps_timing_info_present_flag) {
        long vps_num_units_in_tick = r.readU(32, "vps_num_units_in_tick");
        long vps_time_scale = r.readU(32, "vps_time_scale");
        boolean vps_poc_proportional_to_timing_flag = r.readBool("vps_poc_proportional_to_timing_flag");
        if (vps_poc_proportional_to_timing_flag) {
            int vps_num_ticks_poc_diff_one_minus1 = r.readUE("vps_num_ticks_poc_diff_one_minus1");
        }
        int vps_num_hrd_parameters = r.readUE("vps_num_hrd_parameters");
        int hrd_layer_set_idx[] = new int[vps_num_hrd_parameters];
        boolean cprms_present_flag[] = new boolean[vps_num_hrd_parameters];
        for (int i = 0; i < vps_num_hrd_parameters; i++) {
            hrd_layer_set_idx[i] = r.readUE("hrd_layer_set_idx[" + i + "]");
            if (i > 0) {
                cprms_present_flag[i] = r.readBool("cprms_present_flag[" + i + "]");
            } else {
                cprms_present_flag[0] = true;
            }

            hrd_parameters(cprms_present_flag[i], vps_max_sub_layers_minus1, r);
        }
    }

    boolean vps_extension_flag = r.readBool("vps_extension_flag");
    if (vps_extension_flag) {
        while (r.moreRBSPData()) {
            boolean vps_extension_data_flag = r.readBool("vps_extension_data_flag");
        }
    }
    r.readTrailingBits();

}
 
Example 16
Source File: NamedPipeSocket.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream getInputStream() {
    return Channels.newInputStream(channel);
}
 
Example 17
Source File: NamedPipeSocket.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream getInputStream() {
    return Channels.newInputStream(channel);
}
 
Example 18
Source File: ApprovalBinaryFileWriter.java    From ApprovalTests.Java with Apache License 2.0 4 votes vote down vote up
public ApprovalBinaryFileWriter(ReadableByteChannel stream, String fileExtensionWithoutDot)
{
  this(Channels.newInputStream(stream), fileExtensionWithoutDot);
}
 
Example 19
Source File: TreeReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void processInput(Object input, DataRecord inputRecord) throws Exception {
	if (input instanceof ReadableByteChannel) {
		/*
		 * Convert input stream to XML
		 */
		InputSource source = new InputSource(Channels.newInputStream((ReadableByteChannel) input));
		if (charset != null) {
			source.setEncoding(charset);
		}

		Pipe pipe = null;
		try {
			pipe = Pipe.open();
			try {
				TreeStreamParser treeStreamParser = parserProvider.getTreeStreamParser();
				treeStreamParser.setTreeContentHandler(new TreeXmlContentHandlerAdapter());
				XMLReader treeXmlReader = new TreeXMLReaderAdaptor(treeStreamParser);
				pipeTransformer = new PipeTransformer(TreeReader.this, treeXmlReader);

				pipeTransformer.setInputOutput(Channels.newWriter(pipe.sink(), "UTF-8"), source);
				pipeParser = new PipeParser(TreeReader.this, pushParser, rootContext);
				pipeParser.setInput(Channels.newReader(pipe.source(), "UTF-8"));
				pipeParser.setInputDataRecord(inputRecord);
			} catch (TransformerFactoryConfigurationError e) {
				throw new JetelRuntimeException("Failed to instantiate transformer", e);
			}

			FutureOfRunnable<PipeTransformer> pipeTransformerFuture = CloverWorker.startWorker(pipeTransformer);
			FutureOfRunnable<PipeParser> pipeParserFuture = CloverWorker.startWorker(pipeParser);
		
			pipeTransformerFuture.get();
			pipeParserFuture.get();
			if (pipeTransformerFuture.getRunnable().getException() != null) {
				throw new JetelRuntimeException("Pipe transformer failed.", pipeTransformerFuture.getRunnable().getException());
			}
			if (pipeParserFuture.getRunnable().getException() != null) {
				throw new JetelRuntimeException("Pipe parser failed.", pipeParserFuture.getRunnable().getException());
			}
		} finally {
			if (pipe != null) {
				closeQuietly(pipe.sink());
				closeQuietly(pipe.source());
			}
		}
	} else {
		throw new JetelRuntimeException("Could not read input " + input);
	}

}
 
Example 20
Source File: Files.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Reads all the bytes from a file. The method ensures that the file is
 * closed when all bytes have been read or an I/O error, or other runtime
 * exception, is thrown.
 *
 * <p> Note that this method is intended for simple cases where it is
 * convenient to read all bytes into a byte array. It is not intended for
 * reading in large files.
 *
 * @param   path
 *          the path to the file
 *
 * @return  a byte array containing the bytes read from the file
 *
 * @throws  IOException
 *          if an I/O error occurs reading from the stream
 * @throws  OutOfMemoryError
 *          if an array of the required size cannot be allocated, for
 *          example the file is larger that {@code 2GB}
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 *          method is invoked to check read access to the file.
 */
public static byte[] readAllBytes(Path path) throws IOException {
    try (SeekableByteChannel sbc = Files.newByteChannel(path);
         InputStream in = Channels.newInputStream(sbc)) {
        long size = sbc.size();
        if (size > (long)MAX_BUFFER_SIZE)
            throw new OutOfMemoryError("Required array size too large");

        return read(in, (int)size);
    }
}