Java Code Examples for java.io.OutputStream.write()

The following are Jave code examples for showing how to use write() of the java.io.OutputStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: hypertrack-live-android   File: EasyImageFiles.java   View Source Code Vote up 6 votes
private static void writeToFile(InputStream in, File file) {
    try {
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();


    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Project: mobile-store   File: HexEncoder.java   View Source Code Vote up 6 votes
/**
 * encode the input data producing a Hex output stream.
 *
 * @return the number of bytes produced.
 */
public int encode(
    byte[]                data,
    int                    off,
    int                    length,
    OutputStream    out) 
    throws IOException
{        
    for (int i = off; i < (off + length); i++)
    {
        int    v = data[i] & 0xff;

        out.write(encodingTable[(v >>> 4)]);
        out.write(encodingTable[v & 0xf]);
    }

    return length * 2;
}
 
Example 3
Project: GitHub   File: URLConnectionTest.java   View Source Code Vote up 6 votes
@Test public void redirectedPostStripsRequestBodyHeaders() throws Exception {
  server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
      .addHeader("Location: /page2"));
  server.enqueue(new MockResponse().setBody("Page 2"));

  connection = urlFactory.open(server.url("/page1").url());
  connection.setDoOutput(true);
  connection.addRequestProperty("Content-Length", "4");
  connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
  connection.addRequestProperty("Transfer-Encoding", "identity");
  OutputStream outputStream = connection.getOutputStream();
  outputStream.write("ABCD".getBytes("UTF-8"));
  outputStream.close();
  assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));

  assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());

  RecordedRequest page2 = server.takeRequest();
  assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
  assertNull(page2.getHeader("Content-Length"));
  assertNull(page2.getHeader("Content-Type"));
  assertNull(page2.getHeader("Transfer-Encoding"));
}
 
Example 4
Project: vars-annotation   File: ActiveAppPinger.java   View Source Code Vote up 6 votes
public static boolean ping(int port, final String expectedMsg) {
    boolean beaconExists = false;
    try {
        Socket socket = new Socket("localhost", port);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        final OutputStream output = socket.getOutputStream();
        output.write("ping\n".getBytes());
        String response = reader.readLine();
        beaconExists = response.equals(expectedMsg);
        socket.close();
    }
    catch (Exception e) {
        Logger log = LoggerFactory.getLogger(ActiveAppPinger.class);
        if (log.isDebugEnabled()) {
            log.debug("Failed to connect to port " + port, e);
        }
    }
    return beaconExists;

}
 
Example 5
Project: Android-skin-support   File: SkinAssetsLoader.java   View Source Code Vote up 6 votes
private String copySkinFromAssets(Context context, String name) {
    String skinPath = new File(SkinFileUtils.getSkinDir(context), name).getAbsolutePath();
    try {
        InputStream is = context.getAssets().open(
                SkinConstants.SKIN_DEPLOY_PATH + File.separator + name);
        OutputStream os = new FileOutputStream(skinPath);
        int byteCount;
        byte[] bytes = new byte[1024];
        while ((byteCount = is.read(bytes)) != -1) {
            os.write(bytes, 0, byteCount);
        }
        os.close();
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return skinPath;
}
 
Example 6
Project: jdk8u-jdk   File: CertificateVersion.java   View Source Code Vote up 5 votes
/**
 * Encode the CertificateVersion period in DER form to the stream.
 *
 * @param out the OutputStream to marshal the contents to.
 * @exception IOException on errors.
 */
public void encode(OutputStream out) throws IOException {
    // Nothing for default
    if (version == V1) {
        return;
    }
    DerOutputStream tmp = new DerOutputStream();
    tmp.putInteger(version);

    DerOutputStream seq = new DerOutputStream();
    seq.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),
              tmp);

    out.write(seq.toByteArray());
}
 
Example 7
Project: parabuild-ci   File: StringConverter.java   View Source Code Vote up 5 votes
/**
 * Writes a string to the specified DataOutput using UTF-8 encoding in a
 * machine-independent manner.
 * <p>
 * @param      str   a string to be written.
 * @param      out   destination to write to
 * @return     The number of bytes written out.
 * @exception  IOException  if an I/O error occurs.
 */
public static int writeUTF(String str,
                           OutputStream out) throws IOException {

    int strlen = str.length();
    int c,
        count  = 0;

    for (int i = 0; i < strlen; i++) {
        c = str.charAt(i);

        if (c >= 0x0001 && c <= 0x007F) {
            out.write(c);

            count++;
        } else if (c > 0x07FF) {
            out.write(0xE0 | ((c >> 12) & 0x0F));
            out.write(0x80 | ((c >> 6) & 0x3F));
            out.write(0x80 | ((c >> 0) & 0x3F));

            count += 3;
        } else {
            out.write(0xC0 | ((c >> 6) & 0x1F));
            out.write(0x80 | ((c >> 0) & 0x3F));

            count += 2;
        }
    }

    return count;
}
 
Example 8
Project: hadoop-oss   File: TestReloadingX509TrustManager.java   View Source Code Vote up 5 votes
@Test
public void testReloadCorruptTrustStore() throws Exception {
  KeyPair kp = generateKeyPair("RSA");
  cert1 = generateCertificate("CN=Cert1", kp, 30, "SHA1withRSA");
  cert2 = generateCertificate("CN=Cert2", kp, 30, "SHA1withRSA");
  String truststoreLocation = BASEDIR + "/testcorrupt.jks";
  createTrustStore(truststoreLocation, "password", "cert1", cert1);

  ReloadingX509TrustManager tm =
    new ReloadingX509TrustManager("jks", truststoreLocation, "password", 10);
  try {
    tm.init();
    assertEquals(1, tm.getAcceptedIssuers().length);
    X509Certificate cert = tm.getAcceptedIssuers()[0];

    OutputStream os = new FileOutputStream(truststoreLocation);
    os.write(1);
    os.close();
    new File(truststoreLocation).setLastModified(System.currentTimeMillis() -
                                                 1000);

    // Wait so that the file modification time is different
    Thread.sleep((tm.getReloadInterval() + 200));

    assertEquals(1, tm.getAcceptedIssuers().length);
    assertEquals(cert, tm.getAcceptedIssuers()[0]);
  } finally {
    tm.destroy();
  }
}
 
Example 9
Project: ditb   File: ByteBufferUtils.java   View Source Code Vote up 5 votes
/**
 * Copy data from a buffer to an output stream. Does not update the position
 * in the buffer.
 * @param out the stream to write bytes to
 * @param in the buffer to read bytes from
 * @param offset the offset in the buffer (from the buffer's array offset)
 *      to start copying bytes from
 * @param length the number of bytes to copy
 */
public static void copyBufferToStream(OutputStream out, ByteBuffer in,
    int offset, int length) throws IOException {
  if (in.hasArray()) {
    out.write(in.array(), in.arrayOffset() + offset,
        length);
  } else {
    for (int i = 0; i < length; ++i) {
      out.write(in.get(offset + i));
    }
  }
}
 
Example 10
Project: jmzTab-m   File: MZTabErrorList.java   View Source Code Vote up 5 votes
/**
 * Print error list to output stream.
 * @param out SHOULD NOT set null.
 */
public void print(OutputStream out) throws IOException {
    if (out == null) {
        throw new NullPointerException("Output stream should be set first.");
    }

    for (MZTabError e : errorList) {
        out.write(e.toString().getBytes());
    }
}
 
Example 11
Project: openjdk-jdk10   File: TestHelper.java   View Source Code Vote up 5 votes
static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[8192];
    int n = in.read(buf);
    while (n > 0) {
        out.write(buf, 0, n);
        n = in.read(buf);
    }
}
 
Example 12
Project: holon-core   File: TestRestClient.java   View Source Code Vote up 5 votes
@GET
@Path("stream")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput getStream() {
	return new StreamingOutput() {
		@Override
		public void write(OutputStream output) throws IOException, WebApplicationException {
			output.write(new byte[] { 1, 2, 3 });
		}
	};
}
 
Example 13
Project: jdk8u-jdk   File: InvalidityDateExtension.java   View Source Code Vote up 5 votes
/**
 * Write the extension to the DerOutputStream.
 *
 * @param out the DerOutputStream to write the extension to
 * @exception IOException on encoding errors
 */
public void encode(OutputStream out) throws IOException {
    DerOutputStream  tmp = new DerOutputStream();

    if (this.extensionValue == null) {
        this.extensionId = PKIXExtensions.InvalidityDate_Id;
        this.critical = false;
        encodeThis();
    }
    super.encode(tmp);
    out.write(tmp.toByteArray());
}
 
Example 14
Project: iBase4J-Common   File: BASE64Encoder.java   View Source Code Vote up 4 votes
private void decodeAtom(PushbackInputStream paramPushbackInputStream, OutputStream paramOutputStream, int paramInt)
    throws IOException {
    int i;
    int j = -1;
    int k = -1;
    int m = -1;
    int n = -1;

    if (paramInt < 2) {
        throw new java.lang.ArrayStoreException("BASE64Decoder: Not enough bytes for an atom.");
    }
    do {
        i = paramPushbackInputStream.read();
        if (i == -1) {
            throw new RuntimeException();
        }
    } while ((i == 10) || (i == 13));
    this.decode_buffer[0] = (byte)i;

    i = readFully(paramPushbackInputStream, this.decode_buffer, 1, paramInt - 1);
    if (i == -1) {
        throw new RuntimeException();
    }

    if ((paramInt > 3) && (this.decode_buffer[3] == 61)) {
        paramInt = 3;
    }
    if ((paramInt > 2) && (this.decode_buffer[2] == 61)) {
        paramInt = 2;
    }
    switch (paramInt) {
    case 4:
        n = pem_convert_array[(this.decode_buffer[3] & 0xFF)];
    case 3:
        m = pem_convert_array[(this.decode_buffer[2] & 0xFF)];
    case 2:
        k = pem_convert_array[(this.decode_buffer[1] & 0xFF)];
        j = pem_convert_array[(this.decode_buffer[0] & 0xFF)];
    }

    switch (paramInt) {
    case 2:
        paramOutputStream.write((byte)(j << 2 & 0xFC | k >>> 4 & 0x3));
        break;
    case 3:
        paramOutputStream.write((byte)(j << 2 & 0xFC | k >>> 4 & 0x3));
        paramOutputStream.write((byte)(k << 4 & 0xF0 | m >>> 2 & 0xF));
        break;
    case 4:
        paramOutputStream.write((byte)(j << 2 & 0xFC | k >>> 4 & 0x3));
        paramOutputStream.write((byte)(k << 4 & 0xF0 | m >>> 2 & 0xF));
        paramOutputStream.write((byte)(m << 6 & 0xC0 | n & 0x3F));
    }
}
 
Example 15
Project: bitwise-tuples   File: ExcessKLongEncoder.java   View Source Code Vote up 4 votes
public static void encodeExcessK(long value, OutputStream output) throws IOException {
    value ^= SIGN_MASK; // flip MSB (the sign bit)
    for (int bitsToShift = INITIAL_BITS_TO_SHIFT; bitsToShift >= 0; bitsToShift -= ONE_BYTE) {
        output.write((byte)(value >>> bitsToShift));
    }
}
 
Example 16
Project: Bigbang   File: IOUtil.java   View Source Code Vote up 4 votes
public static void appendString(OutputStream out, String str) throws IOException {
    out.write(str.getBytes());
}
 
Example 17
Project: libRtmp   File: AmfUndefined.java   View Source Code Vote up 4 votes
public static void writeUndefinedTo(OutputStream out) throws IOException {
    out.write(AmfType.UNDEFINED.getValue());
}
 
Example 18
Project: openjdk-jdk10   File: PKCS9Attributes.java   View Source Code Vote up 2 votes
/**
 * Put the DER encoding of this PKCS9 attribute set on an
 * DerOutputStream, tagged with the given implicit tag.
 *
 * @param tag the implicit tag to use in the DER encoding.
 * @param out the output stream on which to put the DER encoding.
 *
 * @exception IOException  on output error.
 */
public void encode(byte tag, OutputStream out) throws IOException {
    out.write(tag);
    out.write(derEncoding, 1, derEncoding.length -1);
}
 
Example 19
Project: ipack   File: NTRUSigningPrivateKeyParameters.java   View Source Code Vote up 2 votes
/**
 * Writes the key to an output stream
 *
 * @param os an output stream
 * @throws IOException
 */
public void writeTo(OutputStream os)
    throws IOException
{
    os.write(getEncoded());
}
 
Example 20
Project: libRtmp   File: ContentData.java   View Source Code Vote up 2 votes
/**
 * Method is public for content (audio/video)
 * Write this packet body without chunking;
 * useful for dumping audio/video streams
 */
@Override
public void writeBody(OutputStream out) throws IOException {
    out.write(data);
}