Java Code Examples for java.nio.file.Files#newByteChannel()

The following examples show how to use java.nio.file.Files#newByteChannel() . 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: MappedByteBufferUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception {
    // given
    final CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file");
    final Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt");

    // when
    try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length());

        if (mappedByteBuffer != null) {
            mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer));
        }
    }

    // then
    final List<String> fileContent = Files.readAllLines(pathToWrite);
    assertEquals(fileContent.get(0), "This will be written to the file");

}
 
Example 2
Source File: DataBufferUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeWritableByteChannelErrorInFlux() throws Exception {
	DataBuffer foo = stringBuffer("foo");
	DataBuffer bar = stringBuffer("bar");
	Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException()));

	WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);

	Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
	StepVerifier.create(writeResult)
			.consumeNextWith(stringConsumer("foo"))
			.consumeNextWith(stringConsumer("bar"))
			.expectError()
			.verify(Duration.ofSeconds(5));

	String result = String.join("", Files.readAllLines(tempFile));

	assertEquals("foobar", result);
	channel.close();
}
 
Example 3
Source File: DataBufferUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeWritableByteChannelCancel() throws Exception {
	DataBuffer foo = stringBuffer("foo");
	DataBuffer bar = stringBuffer("bar");
	Flux<DataBuffer> flux = Flux.just(foo, bar);

	WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);

	Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
	StepVerifier.create(writeResult, 1)
			.consumeNextWith(stringConsumer("foo"))
			.thenCancel()
			.verify(Duration.ofSeconds(5));

	String result = String.join("", Files.readAllLines(tempFile));

	assertEquals("foo", result);
	channel.close();

	flux.subscribe(DataBufferUtils::release);
}
 
Example 4
Source File: RqMtBase.java    From takes with MIT License 6 votes vote down vote up
/**
 * Make a request.
 *  Scans the origin request until the boundary reached. Caches
 *  the  content into a temporary file and returns it as a new request.
 * @param boundary Boundary
 * @param body Origin request body
 * @return Request
 * @throws IOException If fails
 */
private Request make(final byte[] boundary,
    final ReadableByteChannel body) throws IOException {
    final File file = File.createTempFile(
        RqMultipart.class.getName(), ".tmp"
    );
    try (WritableByteChannel channel = Files.newByteChannel(
        file.toPath(),
        StandardOpenOption.READ,
        StandardOpenOption.WRITE
    )
    ) {
        channel.write(
            ByteBuffer.wrap(
                this.head().iterator().next().getBytes(RqMtBase.ENCODING)
            )
        );
        channel.write(
            ByteBuffer.wrap(RqMtBase.CRLF.getBytes(RqMtBase.ENCODING))
        );
        this.copy(channel, boundary, body);
    }
    return new RqTemp(file);
}
 
Example 5
Source File: DflCache.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void expunge(Path p, KerberosTime currTime)
        throws IOException {
    Path p2 = Files.createTempFile(p.getParent(), "rcache", null);
    try (SeekableByteChannel oldChan = Files.newByteChannel(p);
            SeekableByteChannel newChan = createNoClose(p2)) {
        long timeLimit = currTime.getSeconds() - readHeader(oldChan);
        while (true) {
            try {
                AuthTime at = AuthTime.readFrom(oldChan);
                if (at.ctime > timeLimit) {
                    ByteBuffer bb = ByteBuffer.wrap(at.encode(true));
                    newChan.write(bb);
                }
            } catch (BufferUnderflowException e) {
                break;
            }
        }
    }
    makeMine(p2);
    Files.move(p2, p,
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.ATOMIC_MOVE);
}
 
Example 6
Source File: MCRIView2Tools.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public static BufferedImage readTile(Path iviewFileRoot, ImageReader imageReader, int zoomLevel, int x, int y)
    throws IOException {
    String tileName = new MessageFormat("{0}/{1}/{2}.jpg", Locale.ROOT).format(new Object[] { zoomLevel, y, x });
    Path tile = iviewFileRoot.resolve(tileName);
    if (Files.exists(tile)) {
        try (SeekableByteChannel fileChannel = Files.newByteChannel(tile)) {
            ImageInputStream iis = ImageIO.createImageInputStream(fileChannel);
            if (iis == null) {
                throw new IOException("Could not acquire ImageInputStream from SeekableByteChannel: " + tile);
            }
            imageReader.setInput(iis, true);
            BufferedImage image = imageReader.read(0);
            imageReader.reset();
            iis.close();
            return image;
        }
    } else {
        throw new NoSuchFileException(iviewFileRoot.toString(), tileName, null);
    }
}
 
Example 7
Source File: FilePersistence.java    From javan-warty-pig with MIT License 6 votes vote down vote up
/** Save the backing queue to a file */
protected synchronized void saveToFile() {
  lastSaveMs = System.currentTimeMillis();
  dequeuesSinceLastSave = 0;
  ByteArrayParamGenerator.TestCase[] testCases;
  synchronized (queue) {
    testCases = new ByteArrayParamGenerator.TestCase[queue.size()];
    queue.toArray(testCases);
  }
  try (SeekableByteChannel file = Files.newByteChannel(config.filePath,
      StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
    // Just a simple buf, the testCaseToBytes can overwrite it if needed
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.putInt(testCases.length);
    buf.flip();
    file.write(buf);
    for (ByteArrayParamGenerator.TestCase testCase : testCases) {
      buf.clear();
      buf = testCaseToBytes(testCase, buf);
      buf.flip();
      file.write(buf);
    }
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example 8
Source File: M4AReader.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Creates M4A reader from file input stream, sets up metadata generation flag.
 *
 * @param f
 *            File input stream
 * @throws IOException
 *             on IO error
 */
public M4AReader(File f) throws IOException {
    if (null == f) {
        log.warn("Reader was passed a null file");
        log.debug("{}", ToStringBuilder.reflectionToString(this));
    }
    String fileName = f.getName();
    if (fileName.endsWith("m4a") || fileName.endsWith("mp4")) {
        // create a datasource / channel
        dataSource = Files.newByteChannel(Paths.get(f.toURI()));
        // instance an iso file from mp4parser
        isoFile = new IsoFile(dataSource);
        //decode all the info that we want from the atoms
        decodeHeader();
        //analyze the samples/chunks and build the keyframe meta data
        analyzeFrames();
        //add meta data
        firstTags.add(createFileMeta());
        //create / add the pre-streaming (decoder config) tags
        createPreStreamingTags();
    } else {
        log.info("Unsupported file extension: {}", fileName);
    }
}
 
Example 9
Source File: ZipFSTester.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void chCopy(Path src, Path dst) throws IOException
{
    Set<OpenOption> read = new HashSet<>();
    read.add(READ);
    Set<OpenOption> openwrite = new HashSet<>();
    openwrite.add(CREATE_NEW);
    openwrite.add(WRITE);

    try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
         SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
    {

        ByteBuffer bb = ByteBuffer.allocate(8192);
        while (srcCh.read(bb) >= 0) {
            bb.flip();
            dstCh.write(bb);
            bb.clear();
        }

        // Check if source read position is at the end
        if (srcCh.position() != srcCh.size()) {
            System.out.printf("src[%s]: size=%d, position=%d%n",
                              srcCh.toString(), srcCh.size(), srcCh.position());
            throw new RuntimeException("CHECK FAILED!");
        }

        // Check if destination write position is at the end
        if (dstCh.position() != dstCh.size()) {
            System.out.printf("dst[%s]: size=%d, position=%d%n",
                              dstCh.toString(), dstCh.size(), dstCh.position());
            throw new RuntimeException("CHECK FAILED!");
        }
    }
}
 
Example 10
Source File: Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (
                InputStream source = Utils.class.getResourceAsStream(resource);
                ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = createByteBuffer(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example 11
Source File: ReplayCachePrecise.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0,
                "1111111111111111");
        AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
                "2222222222222222");
        KerberosTime now = new KerberosTime(time(0)*1000L);

        // When all new styles, must exact match
        ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
        cache.checkAndStore(now, a1);
        cache.checkAndStore(now, a2);

        // When only old style in cache, partial match
        cache = ReplayCache.getInstance("dfl:./c2");
        cache.checkAndStore(now, a1);
        // A small surgery to remove the new style from the cache file
        SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"),
                StandardOpenOption.WRITE,
                StandardOpenOption.READ);
        ch.position(6);
        ch.write(ByteBuffer.wrap(a1.encode(false)));
        ch.truncate(ch.position());
        ch.close();
        try {
            cache.checkAndStore(now, a2);
            throw new Exception();
        } catch (KrbException ke) {
            // Correct
            System.out.println(ke);
        }
    }
 
Example 12
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example 13
Source File: ReplayCachePrecise.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0,
                "1111111111111111");
        AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
                "2222222222222222");
        KerberosTime now = new KerberosTime(time(0)*1000L);

        // When all new styles, must exact match
        ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
        cache.checkAndStore(now, a1);
        cache.checkAndStore(now, a2);

        // When only old style in cache, partial match
        cache = ReplayCache.getInstance("dfl:./c2");
        cache.checkAndStore(now, a1);
        // A small surgery to remove the new style from the cache file
        SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"),
                StandardOpenOption.WRITE,
                StandardOpenOption.READ);
        ch.position(6);
        ch.write(ByteBuffer.wrap(a1.encode(false)));
        ch.truncate(ch.position());
        ch.close();
        try {
            cache.checkAndStore(now, a2);
            throw new Exception();
        } catch (KrbException ke) {
            // Correct
            System.out.println(ke);
        }
    }
 
Example 14
Source File: PathResource.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * This implementation opens a Channel for the underlying file.
 * @see Files#newByteChannel(Path, OpenOption...)
 */
@Override
public WritableByteChannel writableChannel() throws IOException {
	return Files.newByteChannel(this.path, StandardOpenOption.WRITE);
}
 
Example 15
Source File: WritableFileChannelExample.java    From java-1-class-demos with MIT License 4 votes vote down vote up
public static void main(String[] args) {

		Path file = Paths.get(System.getProperty("user.dir")).resolve("randomnums.txt");

		System.out.println("Will write file to: " + file);
		
		// this is just like an array that you don't need to size
		java.util.List<String> numbers = new ArrayList<>();

		for (int i = 0; i < 100; i++) {
			// casting to string from int, casting to int from double!
			numbers.add("" + (int)Math.floor((Math.random() * 1000*1000) + 1000*1000));
		}

		// temporary buffer, for each iteration.
		ByteBuffer buf = null;
		try {
			
			/***
			 * Why use a fileChannel over other methods?
			 * 
			 * https://dzone.com/articles/java-nio-vs-io
			 * 
			 * NIO vs IO
			 * 
			 * NIO uses buffers
			 * IO uses streams
			 * 
			 * Read about the differences, however you may not understand them until you understand what a "Thread" is.
			 * 
			 * 
			 * FileChannels are NIO.
			 * 
			 * InputStream / OutputStream ar IO.
			 * 
			 * 
			 * Streams are byte by byte, buffers work in blocks of bytes.
			 * 
			 * 
			 */
			// the file channel 
			WritableByteChannel channel =
					Files.newByteChannel(file, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));

			for (String num : numbers) {
				buf = ByteBuffer.wrap((num + "\n").getBytes());
				channel.write(buf);
			}
			System.out.println("File written.");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
 
Example 16
Source File: FilesNewByteChannelTest.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Test(expected = NonWritableChannelException.class)
public void nonWritableGitByteChannelTest() throws IOException {
  try(SeekableByteChannel channel = Files.newByteChannel(file, StandardOpenOption.READ)) {
    channel.write(ByteBuffer.wrap(someBytes()));
  }
}
 
Example 17
Source File: DflCache.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.isSameIgnoresHash(a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}
 
Example 18
Source File: FileUtils.java    From Flink-CEPplus with Apache License 2.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>This is an implementation that follow {@link java.nio.file.Files#readAllBytes(java.nio.file.Path)},
 * and the difference is that it limits the size of the direct buffer to avoid
 * direct-buffer OutOfMemoryError. When {@link java.nio.file.Files#readAllBytes(java.nio.file.Path)}
 * or other interfaces in java API can do this in the future, we should remove it.
 *
 * @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}
 */
public static byte[] readAllBytes(java.nio.file.Path path) throws IOException {
	try (SeekableByteChannel channel = Files.newByteChannel(path);
		InputStream in = Channels.newInputStream(channel)) {

		long size = channel.size();
		if (size > (long) MAX_BUFFER_SIZE) {
			throw new OutOfMemoryError("Required array size too large");
		}

		return read(in, (int) size);
	}
}
 
Example 19
Source File: FileUtils.java    From flink with Apache License 2.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>This is an implementation that follow {@link java.nio.file.Files#readAllBytes(java.nio.file.Path)},
 * and the difference is that it limits the size of the direct buffer to avoid
 * direct-buffer OutOfMemoryError. When {@link java.nio.file.Files#readAllBytes(java.nio.file.Path)}
 * or other interfaces in java API can do this in the future, we should remove it.
 *
 * @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}
 */
public static byte[] readAllBytes(java.nio.file.Path path) throws IOException {
	try (SeekableByteChannel channel = Files.newByteChannel(path);
		InputStream in = Channels.newInputStream(channel)) {

		long size = channel.size();
		if (size > (long) MAX_BUFFER_SIZE) {
			throw new OutOfMemoryError("Required array size too large");
		}

		return read(in, (int) size);
	}
}
 
Example 20
Source File: FLVWriter.java    From red5-io with Apache License 2.0 2 votes vote down vote up
/**
 * Create the stream output file; the flv itself.
 * 
 * @throws IOException
 */
private void createOutputFile() throws IOException {
    this.fileChannel = Files.newByteChannel(Paths.get(filePath), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}