Search in sources :

Example 66 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project jimfs by google.

the class JimfsFileChannel method lock.

@Override
public FileLock lock(long position, long size, boolean shared) throws IOException {
    checkLockArguments(position, size, shared);
    // lock is interruptible
    boolean completed = false;
    try {
        begin();
        completed = true;
        return new FakeFileLock(this, position, size, shared);
    } finally {
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException)

Example 67 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project j2objc by google.

the class ClosedByInterruptExceptionTest method test_Constructor.

/**
 * @tests {@link java.nio.channels.ClosedByInterruptException#ClosedByInterruptException()}
 */
public void test_Constructor() {
    ClosedByInterruptException e = new ClosedByInterruptException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException)

Example 68 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project j2objc by google.

the class FileChannelImpl method lock.

public FileLock lock(long position, long size, boolean shared) throws IOException {
    ensureOpen();
    if (shared && !readable)
        throw new NonReadableChannelException();
    if (!shared && !writable)
        throw new NonWritableChannelException();
    FileLockImpl fli = new FileLockImpl(this, position, size, shared);
    FileLockTable flt = fileLockTable();
    flt.add(fli);
    boolean completed = false;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        int n;
        do {
            n = nd.lock(fd, true, position, size, shared);
        } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
        if (isOpen()) {
            if (n == FileDispatcher.RET_EX_LOCK) {
                assert shared;
                FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
                flt.replace(fli, fli2);
                fli = fli2;
            }
            completed = true;
        }
    } finally {
        if (!completed)
            flt.remove(fli);
        threads.remove(ti);
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
    return fli;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 69 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project wigle-wifi-wardriving by wiglenet.

the class ObservationUploader method doUpload.

/**
 * upload guts. lifted from FileUploaderTask
 * @param bundle
 * @return
 * @throws InterruptedException
 */
private Status doUpload(final Bundle bundle) throws InterruptedException {
    Status status;
    try {
        final Object[] fileFilename = new Object[2];
        final OutputStream fos = getOutputStream(context, bundle, fileFilename);
        final File file = (File) fileFilename[0];
        final String filename = (String) fileFilename[1];
        // write file
        ObservationUploader.CountStats countStats = new ObservationUploader.CountStats();
        long maxId = writeFile(fos, bundle, countStats);
        final Map<String, String> params = new HashMap<>();
        final SharedPreferences prefs = context.getSharedPreferences(ListFragment.SHARED_PREFS, 0);
        if (prefs.getBoolean(ListFragment.PREF_DONATE, false)) {
            params.put("donate", "on");
        }
        final boolean beAnonymous = prefs.getBoolean(ListFragment.PREF_BE_ANONYMOUS, false);
        final String authname = prefs.getString(ListFragment.PREF_AUTHNAME, null);
        if (!beAnonymous && null == authname) {
            return Status.BAD_LOGIN;
        }
        final String userName = prefs.getString(ListFragment.PREF_USERNAME, null);
        final String token = TokenAccess.getApiToken(prefs);
        final String encoded = (null != token && null != authname) ? Base64.encodeToString((authname + ":" + token).getBytes("UTF-8"), Base64.NO_WRAP) : null;
        // don't upload empty files
        if (countStats.lineCount == 0 && !"ark-mobile".equals(userName) && !"bobzilla".equals(userName)) {
            return Status.EMPTY_FILE;
        }
        MainActivity.info("preparing upload...");
        // show on the UI
        sendBundledMessage(Status.UPLOADING.ordinal(), bundle);
        long filesize = file != null ? file.length() : 0L;
        if (filesize <= 0) {
            // find out how big the gzip'd file became
            final FileInputStream fin = context.openFileInput(filename);
            filesize = fin.available();
            fin.close();
            MainActivity.info("filesize: " + filesize);
        }
        if (filesize <= 0) {
            // as an upper bound
            filesize = countStats.byteCount;
        }
        // send file
        final boolean hasSD = MainActivity.hasSD();
        @SuppressWarnings("ConstantConditions") final FileInputStream fis = hasSD ? new FileInputStream(file) : context.openFileInput(filename);
        MainActivity.info("authname: " + authname);
        if (beAnonymous) {
            MainActivity.info("anonymous upload");
        }
        // Cannot set request property after connection is made
        PreConnectConfigurator preConnectConfigurator = new PreConnectConfigurator() {

            @Override
            public void configure(HttpURLConnection connection) {
                if (!beAnonymous) {
                    if (null != encoded && !encoded.isEmpty()) {
                        connection.setRequestProperty("Authorization", "Basic " + encoded);
                    }
                }
            }
        };
        final String response = HttpFileUploader.upload(MainActivity.FILE_POST_URL, filename, "file", fis, params, preConnectConfigurator, getHandler(), filesize);
        if (!prefs.getBoolean(ListFragment.PREF_DONATE, false)) {
            if (response != null && response.indexOf("donate=Y") > 0) {
                final SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean(ListFragment.PREF_DONATE, true);
                editor.apply();
            }
        }
        // TODO: any reason to parse this JSON object? all we care about are two strings.
        MainActivity.info(response);
        if (response != null && response.indexOf("\"success\":true") > 0) {
            status = Status.SUCCESS;
            // save in the prefs
            final SharedPreferences.Editor editor = prefs.edit();
            editor.putLong(ListFragment.PREF_DB_MARKER, maxId);
            editor.putLong(ListFragment.PREF_MAX_DB, maxId);
            editor.putLong(ListFragment.PREF_NETS_UPLOADED, dbHelper.getNetworkCount());
            editor.apply();
        } else if (response != null && response.indexOf("File upload failed.") > 0) {
            status = Status.FAIL;
        } else {
            String error;
            if (response != null && response.trim().equals("")) {
                error = "no response from server";
            } else {
                error = "response: " + response;
            }
            MainActivity.error(error);
            bundle.putString(BackgroundGuiHandler.ERROR, error);
            status = Status.FAIL;
        }
    } catch (final InterruptedException ex) {
        MainActivity.info("ObservationUploader interrupted");
        throw ex;
    } catch (final ClosedByInterruptException | UnknownHostException | ConnectException | FileNotFoundException ex) {
        MainActivity.error("connection problem: " + ex, ex);
        ex.printStackTrace();
        status = Status.EXCEPTION;
        bundle.putString(BackgroundGuiHandler.ERROR, context.getString(R.string.no_wigle_conn));
    } catch (final SSLException ex) {
        MainActivity.error("security problem: " + ex, ex);
        ex.printStackTrace();
        status = Status.EXCEPTION;
        bundle.putString(BackgroundGuiHandler.ERROR, context.getString(R.string.no_secure_wigle_conn));
    } catch (final IOException ex) {
        ex.printStackTrace();
        MainActivity.error("io problem: " + ex, ex);
        MainActivity.writeError(this, ex, context, "Has data connection: " + hasDataConnection(context));
        status = Status.EXCEPTION;
        bundle.putString(BackgroundGuiHandler.ERROR, "io problem: " + ex);
    } catch (final Exception ex) {
        ex.printStackTrace();
        MainActivity.error("ex problem: " + ex, ex);
        MainActivity.writeError(this, ex, context, "Has data connection: " + hasDataConnection(context));
        status = Status.EXCEPTION;
        bundle.putString(BackgroundGuiHandler.ERROR, "ex problem: " + ex);
    }
    return status;
}
Also used : HashMap(java.util.HashMap) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) SSLException(javax.net.ssl.SSLException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) HttpURLConnection(java.net.HttpURLConnection) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SSLException(javax.net.ssl.SSLException) BufferOverflowException(java.nio.BufferOverflowException) ConnectException(java.net.ConnectException) DBException(net.wigle.wigleandroid.DBException) IOException(java.io.IOException) WiGLEAuthException(net.wigle.wigleandroid.WiGLEAuthException) UnknownHostException(java.net.UnknownHostException) File(java.io.File)

Example 70 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project mapdb by jankotek.

the class FileChannelVol method readFully.

protected void readFully(long offset, ByteBuffer buf) {
    int remaining = buf.limit() - buf.position();
    try {
        while (remaining > 0) {
            int read = channel.read(buf, offset);
            if (read < 0)
                throw new EOFException();
            remaining -= read;
        }
    } catch (ClosedByInterruptException e) {
        throw new DBException.VolumeClosedByInterrupt(e);
    } catch (ClosedChannelException e) {
        throw new DBException.VolumeClosed(e);
    } catch (IOException e) {
        throw new DBException.VolumeIOException(e);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) DBException(org.mapdb.DBException) ClosedChannelException(java.nio.channels.ClosedChannelException) EOFException(java.io.EOFException) IOException(java.io.IOException)

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