Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

In this page you can find the example usage for java.io BufferedOutputStream flush.

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:org.paolomoz.zehnkampf.utils.GenHTTPResponse.java

public void generateResponse(InputStream in, OutputStream out) throws IOException {
    requestLineParams = getRequestLineParams(in);

    if (requestLineParams[METHOD_REQUEST_PARAM].equals("POST")) {
        PostMethodUtil util = new PostMethodUtil(reader, docRootPath + requestLineParams[URI_REQUEST_PARAM]);
        util.upload();/*  ww w.j  a v  a 2 s.c  om*/
    }

    File requestedFile = new File(docRootPath + requestLineParams[URI_REQUEST_PARAM]);
    BufferedOutputStream buffOut = new BufferedOutputStream(out);
    HttpResponse response = setResponse(requestedFile);
    writeResponse(response, buffOut);
    buffOut.flush();
}

From source file:eu.scape_project.planning.plato.wf.ValidatePlan.java

public void deployPlan(String endpoint, String user, String password) throws PlanningException {

    SCAPEPlanManagementClient planManagement = new SCAPEPlanManagementClient(endpoint, user, password);

    String planIdentifier;/*  www.  jav a2 s  .c o m*/
    try {
        planIdentifier = planManagement.reservePlanIdentifier();
    } catch (Exception e) {
        throw new PlanningException("Could not reserve Identifier.", e);
    } catch (Throwable e) {
        throw new PlanningException("Could not reserve Identifier.", e);
    }
    this.plan.getPlanProperties().setRepositoryIdentifier(planIdentifier);
    saveWithoutModifyingPlanState();

    String binarydataTempPath = OS.getTmpPath() + planIdentifier + "/";
    File binarydataTempDir = new File(binarydataTempPath);
    binarydataTempDir.mkdirs();
    File planFile = new File(binarydataTempPath + "plan.xml");
    try {
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(planFile));
            projectExport.exportComplete(plan.getPlanProperties().getId(), out, binarydataTempPath);
            out.flush();
            out.close();

            planManagement.deployPlan(planIdentifier, new FileInputStream(planFile));
        } catch (Exception e) {
            throw new PlanningException("Failed to generate plan.", e);
        }
    } finally {
        OS.deleteDirectory(binarydataTempDir);
    }
}

From source file:ftpserver.Data.java

public void sendData(String sdata) throws Exception {
    //System.out.print("Sending data on datacon ");
    if (socket == null) {
        Thread.currentThread().sleep(500);
        if (socket == null) {
            throw new IOException("No client connected");
        }/*from w ww. j a v a2  s  .  c  om*/
    }
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(socket.getOutputStream());
        out.write(sdata.getBytes(), 0, sdata.length());
        out.flush();
    } catch (Exception e) {
        throw e;
    }
}

From source file:org.eclipse.rtp.configurator.service.provider.internal.deploy.RepositoryManager.java

private void saveFileData(ZipInputStream zis, BufferedOutputStream bfos) throws IOException {
    int resultLength;
    byte[] data = new byte[FILE_BUFFER];
    while ((resultLength = zis.read(data, 0, FILE_BUFFER)) != -1) {
        bfos.write(data, 0, resultLength);
    }//  ww w  .j av  a  2 s  .  co m
    bfos.flush();
    bfos.close();
}

From source file:com.mobile.natal.natalchart.NetworkUtilities.java

/**
 * Sends a string via POST to a given url.
 *
 * @param context      the context to use.
 * @param urlStr       the url to which to send to.
 * @param string       the string to send as post body.
 * @param user         the user or <code>null</code>.
 * @param password     the password or <code>null</code>.
 * @param readResponse if <code>true</code>, the response from the server is read and parsed as return message.
 * @return the response./*from w  ww  . j ava2s .  c  om*/
 * @throws Exception if something goes wrong.
 */
public static String sendPost(Context context, String urlStr, String string, String user, String password,
        boolean readResponse) throws Exception {
    BufferedOutputStream wr = null;
    HttpURLConnection conn = null;
    try {
        conn = makeNewConnection(urlStr);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // conn.setChunkedStreamingMode(0);
        conn.setUseCaches(false);
        if (user != null && password != null && user.trim().length() > 0 && password.trim().length() > 0) {
            conn.setRequestProperty("Authorization", getB64Auth(user, password));
        }
        conn.connect();

        // Make server believe we are form data...
        wr = new BufferedOutputStream(conn.getOutputStream());
        byte[] bytes = string.getBytes();
        wr.write(bytes);
        wr.flush();

        int responseCode = conn.getResponseCode();
        if (readResponse) {
            StringBuilder returnMessageBuilder = new StringBuilder();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
                while (true) {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    returnMessageBuilder.append(line + "\n");
                }
                br.close();
            }

            return returnMessageBuilder.toString();
        } else {
            return getMessageForCode(context, responseCode,
                    context.getResources().getString(R.string.post_completed_properly));
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (conn != null)
            conn.disconnect();
    }
}

From source file:com.streamsets.pipeline.stage.processor.geolocation.TestGeolocationProcessor.java

@Before
public void setup() throws Exception {
    tempDir = Files.createTempDir();
    databaseFile = new File(tempDir, "GeoLite2-Country.mmdb");
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(databaseFile));
    Resources.copy(Resources.getResource("GeoLite2-Country.mmdb"), out);
    out.flush();
    out.close();//from   w  ww.j a v a  2s .  c om
}

From source file:com.streamsets.pipeline.stage.origin.kafka.TestKafkaSource.java

@BeforeClass
public static void setUp() throws IOException, InterruptedException {
    sdcKafkaTestUtil.startZookeeper();//from  ww w.  j a va  2s  .  c  om
    sdcKafkaTestUtil.startKafkaBrokers(3);

    zkConnect = sdcKafkaTestUtil.getZkConnect();

    sdcKafkaTestUtil.createTopic(TOPIC1, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC2, MULTIPLE_PARTITIONS, MULTIPLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC3, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC4, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC5, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC6, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC7, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC8, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC9, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC10, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC11, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC12, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC13, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC14, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC15, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);
    sdcKafkaTestUtil.createTopic(TOPIC16, SINGLE_PARTITION, SINGLE_REPLICATION_FACTOR);

    for (int i = 1; i <= 16; i++) {
        TestUtils.waitUntilMetadataIsPropagated(
                scala.collection.JavaConversions.asScalaBuffer(sdcKafkaTestUtil.getKafkaServers()),
                "TestKafkaSource" + String.valueOf(i), 0, 5000);
        // For now, the only topic that needs more than one partition is topic 2. Eventually we should put all these into
        // a class and make sure we create the topics based on a list of Topic/Partition info, rather than this.
        if (i == 2) {
            for (int j = 0; j < MULTIPLE_PARTITIONS; j++) {
                TestUtils.waitUntilMetadataIsPropagated(
                        scala.collection.JavaConversions.asScalaBuffer(sdcKafkaTestUtil.getKafkaServers()),
                        "TestKafkaSource" + String.valueOf(i), j, 5000);
            }
        }
    }

    producer = sdcKafkaTestUtil.createProducer(sdcKafkaTestUtil.getMetadataBrokerURI(), true);
    tempDir = Files.createTempDir();
    protoDescFile = new File(tempDir, "Employee.desc");
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(protoDescFile));
    Resources.copy(Resources.getResource("Employee.desc"), out);
    out.flush();
    out.close();
}

From source file:com.commonsware.android.downloader.Downloader.java

@Override
public void onHandleIntent(Intent i) {
    try {//w  w w . j  a v a  2s  . c o m
        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        root.mkdirs();

        File output = new File(root, i.getData().getLastPathSegment());

        if (output.exists()) {
            output.delete();
        }

        URL url = new URL(i.getData().toString());
        HttpURLConnection c = (HttpURLConnection) url.openConnection();

        FileOutputStream fos = new FileOutputStream(output.getPath());
        BufferedOutputStream out = new BufferedOutputStream(fos);

        try {
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[8192];
            int len = 0;

            while ((len = in.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
            }

            out.flush();
        } finally {
            fos.getFD().sync();
            out.close();
            c.disconnect();
        }

        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_COMPLETE));
    } catch (IOException e2) {
        Log.e(getClass().getName(), "Exception in download", e2);
    }
}

From source file:com.buaa.cfs.utils.FileUtil.java

private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir)
        throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }//  w  ww .  j a v  a 2  s  .com

        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }

        return;
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
    }

    int count;
    byte data[] = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }

    outputStream.flush();
    outputStream.close();
}

From source file:de.knurt.fam.template.controller.letter.EMailLetterAdapter.java

/**
 * send the email and return an errormessage. if errormessage is empty,
 * sending succeeded.//from  w w  w. j  a va2 s  .com
 * 
 * @param post
 *            getting the input stream from
 * @param customid
 *            for the to send via email
 * @return an errormessage (may empty on success)
 */
public String send(PostMethod post, String customid) {
    String errormessage = "";
    if (this.isValid()) {

        File file = null;
        try {
            file = this.getTmpFile(customid);

        } catch (IOException e) {
            FamLog.exception(e, 201106131728l);
            errormessage += "Fail: Create tmp file [201106131729].";
        }

        try {
            InputStream is = post.getResponseBodyAsStream();

            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            int bite = 0;
            while ((bite = is.read()) >= 0) {
                bos.write(bite);
            }
            bos.flush();
            bos.close();
        } catch (IOException e) {
            errormessage += "Fail: Write pdf to tmp file [201106141055].";
            FamLog.exception(e, 201106131733l);
        }

        Email mail = this.getEMail(file, this);
        if (mail == null) {
            errormessage += "Fail: Create e-mail object. Please check log files [201106141058].";
        }
        boolean succ = UserMailSender.sendWithoutUserBox(mail);
        if (!succ) {
            errormessage += "Fail: Send email through configured server. Please check log files [201106131756].";
        }

    } else {
        if (this.getTo() == null) {
            errormessage += "Fail: Find an recipient - form email_recipient sent? [201106131757]";
        } else {
            errormessage += "Invalid email address. Recheck email recipient.";
        }
    }
    return errormessage;
}