Search in sources :

Example 56 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project graal by oracle.

the class CSourceCodeWriter method writeFile.

public Path writeFile(String fileName, boolean ensureCorrectExtension) {
    assert currentLine.length() == 0 : "last line not finished";
    String fixedFileName = fileName;
    if (!fileName.endsWith(C_SOURCE_FILE_EXTENSION) && ensureCorrectExtension) {
        fixedFileName = fileName.concat(C_SOURCE_FILE_EXTENSION);
    }
    Path outputFile = tempDirectory.resolve(fixedFileName);
    Charset charset = Charset.forName(CHARSET);
    try (BufferedWriter writer = Files.newBufferedWriter(outputFile, charset)) {
        for (String line : lines) {
            writer.write(line);
            writer.write("\n");
        }
    } catch (ClosedByInterruptException ex) {
        throw new InterruptImageBuilding();
    } catch (IOException ex) {
        throw shouldNotReachHere(ex);
    }
    return outputFile;
}
Also used : Path(java.nio.file.Path) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) Charset(java.nio.charset.Charset) InterruptImageBuilding(com.oracle.svm.core.util.InterruptImageBuilding) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 57 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project Bytecoder by mirkosertic.

the class FileChannelImpl method transferToTrustedChannel.

private long transferToTrustedChannel(long position, long count, WritableByteChannel target) throws IOException {
    boolean isSelChImpl = (target instanceof SelChImpl);
    if (!((target instanceof FileChannelImpl) || isSelChImpl))
        return IOStatus.UNSUPPORTED;
    // Trusted target: Use a mapped buffer
    long remaining = count;
    while (remaining > 0L) {
        long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
        try {
            MappedByteBuffer dbb = map(MapMode.READ_ONLY, position, size);
            try {
                // ## Bug: Closing this channel will not terminate the write
                int n = target.write(dbb);
                assert n >= 0;
                remaining -= n;
                if (isSelChImpl) {
                    // one attempt to write to selectable channel
                    break;
                }
                assert n > 0;
                position += n;
            } finally {
                unmap(dbb);
            }
        } catch (ClosedByInterruptException e) {
            // to be thrown after closing this channel.
            assert !target.isOpen();
            try {
                close();
            } catch (Throwable suppressed) {
                e.addSuppressed(suppressed);
            }
            throw e;
        } catch (IOException ioe) {
            // Only throw exception if no bytes have been written
            if (remaining == count)
                throw ioe;
            break;
        }
    }
    return count - remaining;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 58 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project com.revolsys.open by revolsys.

the class ScaledIntegerGriddedDigitalElevationModelReader method read.

@Override
public GriddedElevationModel read() {
    init();
    if (this.exists) {
        try {
            final ChannelReader in = this.reader;
            final int cellCount = this.gridWidth * this.gridHeight;
            final int[] elevations = new int[cellCount];
            final ReadableByteChannel channel = in.getChannel();
            if (isMemoryMapped() && channel instanceof FileChannel) {
                final FileChannel fileChannel = (FileChannel) channel;
                final MappedByteBuffer mappedBytes = fileChannel.map(MapMode.READ_ONLY, ScaledIntegerGriddedDigitalElevation.HEADER_SIZE, cellCount * ScaledIntegerGriddedDigitalElevation.RECORD_SIZE);
                final IntBuffer intBuffer = mappedBytes.asIntBuffer();
                for (int index = 0; index < cellCount; index++) {
                    elevations[index] = intBuffer.get();
                }
            } else {
                for (int index = 0; index < cellCount; index++) {
                    final int elevation = in.getInt();
                    elevations[index] = elevation;
                }
            }
            final IntArrayScaleGriddedElevationModel elevationModel = new IntArrayScaleGriddedElevationModel(this.geometryFactory, this.boundingBox, this.gridWidth, this.gridHeight, this.gridCellSize, elevations);
            elevationModel.setResource(this.resource);
            return elevationModel;
        } catch (final ClosedByInterruptException e) {
            return null;
        } catch (final IOException | RuntimeException e) {
            if (Exceptions.isException(e, ClosedByInterruptException.class)) {
                return null;
            } else {
                throw Exceptions.wrap("Unable to read DEM: " + this.resource, e);
            }
        }
    } else {
        return null;
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ChannelReader(com.revolsys.io.channels.ChannelReader) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IntBuffer(java.nio.IntBuffer) IntArrayScaleGriddedElevationModel(com.revolsys.elevation.gridded.IntArrayScaleGriddedElevationModel) IOException(java.io.IOException)

Example 59 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project h2database by h2database.

the class FileRetryOnInterrupt method reopen.

private void reopen(int i, IOException e) throws IOException {
    if (i > 20) {
        throw e;
    }
    if (!(e instanceof ClosedByInterruptException) && !(e instanceof ClosedChannelException)) {
        throw e;
    }
    // clear the interrupt flag, to avoid re-opening many times
    Thread.interrupted();
    FileChannel before = channel;
    // as this method is called in a loop
    synchronized (this) {
        if (before == channel) {
            open();
            reLock();
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ClosedChannelException(java.nio.channels.ClosedChannelException) FileChannel(java.nio.channels.FileChannel)

Example 60 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project Purus-Pasta by puruscor.

the class HackSocket method release.

private void release() throws ClosedByInterruptException {
    HackThread ut = (HackThread) Thread.currentThread();
    InterruptAction ia = this.ia.get();
    if (ia == null)
        throw (new Error("Tried to release a hacked thread without an interrupt handler."));
    ut.remil(ia);
    if (ia.interrupted) {
        ut.interrupt();
        throw (new ClosedByInterruptException());
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)81 IOException (java.io.IOException)48 ByteBuffer (java.nio.ByteBuffer)15 ClosedChannelException (java.nio.channels.ClosedChannelException)11 SocketTimeoutException (java.net.SocketTimeoutException)9 InetSocketAddress (java.net.InetSocketAddress)7 MappedByteBuffer (java.nio.MappedByteBuffer)7 SocketChannel (java.nio.channels.SocketChannel)7 File (java.io.File)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 ServerSocket (java.net.ServerSocket)5 FileChannel (java.nio.channels.FileChannel)5 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)5 InterruptedIOException (java.io.InterruptedIOException)4 Path (java.nio.file.Path)4 Test (org.junit.Test)4 BuildId (com.facebook.buck.model.BuildId)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)3