Search in sources :

Example 26 with FileInputStream

use of java.io.FileInputStream in project druid by druid-io.

the class GenericIndexedWriter method writeToChannelVersionTwo.

private void writeToChannelVersionTwo(WritableByteChannel channel, FileSmoosher smoosher) throws IOException {
    if (smoosher == null) {
        throw new IAE("version 2 GenericIndexedWriter requires FileSmoosher.");
    }
    int bagSizePower = bagSizePower();
    OutputStream metaOut = Channels.newOutputStream(channel);
    metaOut.write(GenericIndexed.VERSION_TWO);
    metaOut.write(objectsSorted ? 0x1 : 0x0);
    metaOut.write(Ints.toByteArray(bagSizePower));
    metaOut.write(Ints.toByteArray(Ints.checkedCast(numWritten)));
    metaOut.write(Ints.toByteArray(fileNameByteArray.length));
    metaOut.write(fileNameByteArray);
    try (RandomAccessFile headerFile = new RandomAccessFile(ioPeon.getFile(makeFilename("headerLong")), "r")) {
        Preconditions.checkNotNull(headerFile, "header file missing.");
        long previousValuePosition = 0;
        int bagSize = 1 << bagSizePower;
        int numberOfFilesRequired = GenericIndexed.getNumberOfFilesRequired(bagSize, numWritten);
        byte[] buffer = new byte[1 << 16];
        try (InputStream is = new FileInputStream(ioPeon.getFile(makeFilename("values")))) {
            int counter = -1;
            for (int i = 0; i < numberOfFilesRequired; i++) {
                if (i != numberOfFilesRequired - 1) {
                    // 8 for long bytes.
                    headerFile.seek((bagSize + counter) * Longs.BYTES);
                    counter = counter + bagSize;
                } else {
                    // for remaining items.
                    headerFile.seek((numWritten - 1) * Longs.BYTES);
                }
                long valuePosition = Long.reverseBytes(headerFile.readLong());
                long numBytesToPutInFile = valuePosition - previousValuePosition;
                try (SmooshedWriter smooshChannel = smoosher.addWithSmooshedWriter(generateValueFileName(filenameBase, i), numBytesToPutInFile)) {
                    writeBytesIntoSmooshedChannel(numBytesToPutInFile, buffer, smooshChannel, is);
                    previousValuePosition = valuePosition;
                }
            }
        }
        writeHeaderLong(smoosher, headerFile, bagSizePower, buffer);
    }
}
Also used : SmooshedWriter(io.druid.java.util.common.io.smoosh.SmooshedWriter) RandomAccessFile(java.io.RandomAccessFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStream(java.io.OutputStream) IAE(io.druid.java.util.common.IAE) FileInputStream(java.io.FileInputStream)

Example 27 with FileInputStream

use of java.io.FileInputStream in project druid by druid-io.

the class GenericIndexedWriter method writeHeaderLong.

private void writeHeaderLong(FileSmoosher smoosher, RandomAccessFile headerFile, int bagSizePower, byte[] buffer) throws IOException {
    ByteBuffer helperBuffer = ByteBuffer.allocate(Ints.BYTES).order(ByteOrder.nativeOrder());
    try (CountingOutputStream finalHeaderOut = new CountingOutputStream(ioPeon.makeOutputStream(makeFilename("header_final")))) {
        int numberOfElementsPerValueFile = 1 << bagSizePower;
        long currentNumBytes = 0;
        long relativeRefBytes = 0;
        long relativeNumBytes;
        headerFile.seek(0);
        // following block converts long header indexes into int header indexes.
        for (int pos = 0; pos < numWritten; pos++) {
            // to current offset.
            if ((pos & (numberOfElementsPerValueFile - 1)) == 0) {
                relativeRefBytes = currentNumBytes;
            }
            currentNumBytes = Long.reverseBytes(headerFile.readLong());
            relativeNumBytes = currentNumBytes - relativeRefBytes;
            writeIntValueToOutputStream(helperBuffer, Ints.checkedCast(relativeNumBytes), finalHeaderOut);
        }
        long numBytesToPutInFile = finalHeaderOut.getCount();
        finalHeaderOut.close();
        try (InputStream is = new FileInputStream(ioPeon.getFile(makeFilename("header_final")))) {
            try (SmooshedWriter smooshChannel = smoosher.addWithSmooshedWriter(generateHeaderFileName(filenameBase), numBytesToPutInFile)) {
                writeBytesIntoSmooshedChannel(numBytesToPutInFile, buffer, smooshChannel, is);
            }
        }
    }
}
Also used : SmooshedWriter(io.druid.java.util.common.io.smoosh.SmooshedWriter) CountingOutputStream(com.google.common.io.CountingOutputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 28 with FileInputStream

use of java.io.FileInputStream in project che by eclipse.

the class ApiEndpointAccessibilityChecker method start.

@PostConstruct
public void start() {
    try {
        final HttpJsonResponse pingResponse = httpJsonRequestFactory.fromUrl(apiEndpoint).setMethod(HttpMethod.GET).setTimeout(2000).request();
        if (pingResponse.getResponseCode() == HttpURLConnection.HTTP_OK) {
            return;
        }
    } catch (ApiException | IOException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
    LOG.error("The workspace agent has attempted to start, but it is unable to ping the Che server at " + apiEndpoint);
    LOG.error("The workspace agent has been forcefully stopped. " + "This error happens when the agent cannot resolve the location of the Che server. " + "This error can usually be fixed with additional configuration settings in /conf/che.properties. " + "The Che server will stop this workspace after a short timeout. " + "You can get help by posting your config, stacktrace and workspace /etc/hosts below as a GitHub issue.");
    // content of /etc/hosts file may provide clues on why the connection failed, e.g. how che-host is resolved
    try {
        LOG.info("Workspace /etc/hosts: " + IoUtil.readAndCloseQuietly(new FileInputStream(new File("/etc/hosts"))));
    } catch (Exception e) {
        LOG.info(e.getLocalizedMessage(), e);
    }
    System.exit(0);
}
Also used : IOException(java.io.IOException) HttpJsonResponse(org.eclipse.che.api.core.rest.HttpJsonResponse) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ApiException(org.eclipse.che.api.core.ApiException) ApiException(org.eclipse.che.api.core.ApiException) PostConstruct(javax.annotation.PostConstruct)

Example 29 with FileInputStream

use of java.io.FileInputStream in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStreamContent.

@Test
public void testServerWithHttpClientStreamContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new StringContentProvider(sent));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals(sent, _received);
    } finally {
        client.stop();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 30 with FileInputStream

use of java.io.FileInputStream in project che by eclipse.

the class DeltaProcessingState method getExternalLibTimeStamps.

public Hashtable getExternalLibTimeStamps() {
    if (this.externalTimeStamps == null) {
        Hashtable timeStamps = new Hashtable();
        File timestampsFile = getTimeStampsFile();
        DataInputStream in = null;
        try {
            in = new DataInputStream(new BufferedInputStream(new FileInputStream(timestampsFile)));
            int size = in.readInt();
            while (size-- > 0) {
                String key = in.readUTF();
                long timestamp = in.readLong();
                timeStamps.put(Path.fromPortableString(key), new Long(timestamp));
            }
        } catch (IOException e) {
            if (timestampsFile.exists())
                //$NON-NLS-1$
                Util.log(e, "Unable to read external time stamps");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                // nothing we can do: ignore
                }
            }
        }
        this.externalTimeStamps = timeStamps;
    }
    return this.externalTimeStamps;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FileInputStream (java.io.FileInputStream)14180 File (java.io.File)6801 IOException (java.io.IOException)6283 InputStream (java.io.InputStream)4121 FileOutputStream (java.io.FileOutputStream)2210 FileNotFoundException (java.io.FileNotFoundException)2002 InputStreamReader (java.io.InputStreamReader)1733 BufferedInputStream (java.io.BufferedInputStream)1593 Test (org.junit.Test)1548 BufferedReader (java.io.BufferedReader)1414 Properties (java.util.Properties)1410 ArrayList (java.util.ArrayList)960 OutputStream (java.io.OutputStream)753 ByteArrayInputStream (java.io.ByteArrayInputStream)599 HashMap (java.util.HashMap)597 ZipEntry (java.util.zip.ZipEntry)575 DataInputStream (java.io.DataInputStream)549 GZIPInputStream (java.util.zip.GZIPInputStream)435 KeyStore (java.security.KeyStore)430 ByteArrayOutputStream (java.io.ByteArrayOutputStream)407