FileChannel fc = ... GZIPInputStream gis = new GZIPInputStream(Channels.newInputStream(fc));
/** * Deserializes a JSON file via a JSON object template with a shared lock on the file * * @param <T> child type of {@link JsonTemplate} * @param jsonFile a file containing a JSON string * @param templateClass the template to deserialize the string to * @return the template filled with the values parsed from {@code jsonFile} * @throws IOException if an error occurred during reading the file or parsing the JSON */ public static <T extends JsonTemplate> T readJsonFromFileWithLock( Path jsonFile, Class<T> templateClass) throws IOException { // channel is closed by inputStream.close() FileChannel channel = FileChannel.open(jsonFile, StandardOpenOption.READ); channel.lock(0, Long.MAX_VALUE, true); // shared lock, released by channel close try (InputStream inputStream = Channels.newInputStream(channel)) { return objectMapper.readValue(inputStream, templateClass); } }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
// STEP 1: Create random access file read-only RandomAccessFile raf = new RandomAccessFile("/text.txt", "r"); // STEP 2: Use Channels to convert to InputStream InputStream is = Channels.newInputStream(raf.getChannel());
/** * Open a stream to a file. * * @param offset If zero, stream the entire log. If positive, read from this byte position onwards. If negative, * read this many bytes from the end of the file. * * @return input supplier for this log, if available from this provider */ public static InputStream streamFile(final File file, final long offset) throws IOException { final RandomAccessFile raf = new RandomAccessFile(file, "r"); final long rafLength = raf.length(); if (offset > 0) { raf.seek(offset); } else if (offset < 0 && offset < rafLength) { raf.seek(Math.max(0, rafLength + offset)); } return Channels.newInputStream(raf.getChannel()); } }
@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 void close() throws IOException { shutdownInput(); } }; }
@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(); } }; }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
BlockingUnixSocket(File path, UnixSocketChannel channel) { super(channel); this.path = path; this.in = Channels.newInputStream(new UnselectableReadableByteChannel()); this.out = Channels.newOutputStream(new UnselectableWritableByteChannel()); }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
public UnixDomainSocket(String ipcSocketPath, int bufferSize) { this.bufferSize = bufferSize; try { UnixSocketAddress address = new UnixSocketAddress(ipcSocketPath); channel = UnixSocketChannel.open(address); reader = new InputStreamReader(Channels.newInputStream(channel)); writer = new PrintWriter(Channels.newOutputStream(channel)); } catch (IOException e) { throw new RuntimeException( "Provided file socket cannot be opened: " + ipcSocketPath, e); } }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return com.facebook.presto.jdbc.internal.guava.io.Files.readFile( Channels.newInputStream(channel), channel.size()); } }
@Override public InputStream getInputStream() throws IOException { if (!channel.isOpen()) { throw new SocketException("Socket is closed"); } if (!channel.isConnected()) { throw new SocketException("Socket is not connected"); } if (inputShutdown) { throw new SocketException("Socket input is shutdown"); } return new FilterInputStream(Channels.newInputStream(channel)) { @Override public void close() throws IOException { shutdownInput(); } }; }
@Override public LoadedInstanceConfig loadConfig() throws Exception { File propertiesFile = new File(propertiesDirectory, propertyFileName); Properties properties = new Properties(); if ( propertiesFile.exists() ) { RandomAccessFile raf = new RandomAccessFile(propertiesFile, "rw"); try { FileLock lock = raf.getChannel().lock(); try { properties.load(Channels.newInputStream(raf.getChannel())); } finally { lock.release(); } } finally { CloseableUtils.closeQuietly(raf); } } PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, defaults); return new LoadedInstanceConfig(config, propertiesFile.lastModified()); }
@Override public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(8); dataSource.read((ByteBuffer) byteBuffer.rewind()); byteBuffer.position(6); dataReferenceIndex = IsoTypeReader.readUInt16(byteBuffer); byte[] namespaceBytes = new byte[0]; int read; while ((read = Channels.newInputStream(dataSource).read()) != 0) { namespaceBytes = Mp4Arrays.copyOfAndAppend(namespaceBytes, (byte) read); } namespace = Utf8.convert(namespaceBytes); byte[] schemaLocationBytes = new byte[0]; while ((read = Channels.newInputStream(dataSource).read()) != 0) { schemaLocationBytes = Mp4Arrays.copyOfAndAppend(schemaLocationBytes, (byte) read); } schemaLocation = Utf8.convert(schemaLocationBytes); byte[] auxiliaryMimeTypesBytes = new byte[0]; while ((read = Channels.newInputStream(dataSource).read()) != 0) { auxiliaryMimeTypesBytes = Mp4Arrays.copyOfAndAppend(auxiliaryMimeTypesBytes, (byte) read); } auxiliaryMimeTypes = Utf8.convert(auxiliaryMimeTypesBytes); initContainer(dataSource, contentSize - (header.remaining() + namespace.length() + schemaLocation.length() + auxiliaryMimeTypes.length() + 3), boxParser); }
InputStream getStream(UUID jobId, String keyName) { String blobName = getDataKeyName(jobId, keyName); Blob blob = bucket.get(blobName); ReadChannel channel = blob.reader(); return Channels.newInputStream(channel); }
props.load( Channels.newInputStream( channel ) );
/** * Expands {@code archive} into {@code targetDirectory}. * * @param archive the file to expand * @param targetDirectory the directory to write to * @param format the archive format. This uses the same format as * accepted by {@link ArchiveStreamFactory}. * @throws IOException if an I/O error occurs * @throws ArchiveException if the archive cannot be read for other reasons */ public void expand(String format, SeekableByteChannel archive, File targetDirectory) throws IOException, ArchiveException { if (!prefersSeekableByteChannel(format)) { expand(format, Channels.newInputStream(archive), targetDirectory); } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) { expand(new ZipFile(archive), targetDirectory); } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) { expand(new SevenZFile(archive), targetDirectory); } else { // never reached as prefersSeekableByteChannel only returns true for ZIP and 7z throw new ArchiveException("don't know how to handle format " + format); } }
@SuppressWarnings({"rawtypes", "unchecked" }) @Override public void render(final Object value, final Context ctx) throws Exception { Object model = value; /** View? */ if (value instanceof View) { View view = (View) value; String path = path(Route.normalize(prefix + "/" + view.name() + suffix)); Map data = view.model(); model = Rocker.template(path).bind(data); } /** RockerModel: */ if (model instanceof RockerModel) { ArrayOfByteArraysOutput output = ((RockerModel) model).render(ArrayOfByteArraysOutput.FACTORY, template -> { if (template instanceof RequestRockerTemplate) { RequestRockerTemplate rrt = (RequestRockerTemplate) template; rrt.locals = ctx.locals(); } }); ctx.type(MediaType.html) .length(output.getByteLength()) // FIXME: make more efficient. Context should provide a way to send partial results .send(Channels.newInputStream(output.asReadableByteChannel())); } }
public VideoParameterSet(ByteBuffer vps) throws IOException { this.vps = vps; CAVLCReader r = new CAVLCReader(Channels.newInputStream(new ByteBufferByteChannel((ByteBuffer) 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");