List of usage examples for java.io DataOutputStream write
public synchronized void write(int b) throws IOException
b
) to the underlying output stream. From source file:com.linkedin.pinot.common.utils.DataTable.java
private byte[] serializeMetadata() throws Exception { if (metadata != null) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(baos); out.writeInt(metadata.size());/*w w w. j a v a 2 s . c o m*/ for (Entry<String, String> entry : metadata.entrySet()) { byte[] keyBytes = entry.getKey().getBytes(UTF8); out.writeInt(keyBytes.length); out.write(keyBytes); byte[] valueBytes = entry.getValue().getBytes(UTF8); out.writeInt(valueBytes.length); out.write(valueBytes); } return baos.toByteArray(); } return new byte[0]; }
From source file:darks.learning.word2vec.Word2Vec.java
/** * Save model/* w w w . j a v a 2 s .c om*/ * * @param file Model file saved */ public void saveModel(File file) { log.info("Saving word2vec model to " + file); DataOutputStream dos = null; try { dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); dos.writeInt(wordNodes.size()); dos.writeInt(config.featureSize); double[] syn0 = null; for (Entry<String, WordNode> element : wordNodes.entrySet()) { byte[] bytes = element.getKey().getBytes(); dos.writeInt(bytes.length); dos.write(bytes); syn0 = (element.getValue()).feature.toArray(); for (int i = 0; i < config.featureSize; i++) { dos.writeDouble(syn0[i]); } } } catch (IOException e) { log.error(e.getMessage(), e); } finally { IOUtils.closeStream(dos); } }
From source file:org.opencms.synchronize.CmsSynchronize.java
/** * This writes the byte content of a resource to the file on the server * file system.<p>/*from w w w .ja v a 2 s . co m*/ * * @param content the content of the file in the VFS * @param file the file in SFS that has to be updated with content * * @throws IOException if something goes wrong */ private void writeFileByte(byte[] content, File file) throws IOException { FileOutputStream fOut = null; DataOutputStream dOut = null; try { // write the content to the file in server filesystem fOut = new FileOutputStream(file); dOut = new DataOutputStream(fOut); dOut.write(content); dOut.flush(); } catch (IOException e) { throw e; } finally { try { if (fOut != null) { fOut.close(); } } catch (IOException e) { // ignore } try { if (dOut != null) { dOut.close(); } } catch (IOException e) { // ignore } } }
From source file:org.latticesoft.util.common.CryptoHelper.java
public String encode(String input) { if (this.keyPair == null || input == null || input.length() == 0) { return ""; }/*from w w w. java 2s . c om*/ int index = 0; int diff = 0; StringBuffer sb = new StringBuffer(); String algor = this.getInstanceAlgorithm(); PublicKey pubKey = null; SecureRandom srand = null; Cipher rsaEnc = null; ByteArrayOutputStream baos = null; DataOutputStream dos = null; byte[] data = input.getBytes(); int bufferSize = strength / 32; byte[] buffer = new byte[bufferSize]; byte[] tmp = null; String output = null; try { pubKey = this.keyPair.getPublic(); srand = SecureRandom.getInstance(this.secureRandomAlgorithm, this.secureRandomProviderName); rsaEnc = Cipher.getInstance(algor, this.providerName); rsaEnc.init(Cipher.ENCRYPT_MODE, pubKey, srand); baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); while (index < data.length) { diff = data.length - index; if (diff > buffer.length) { diff = buffer.length; } NumeralUtil.resetByteArray(buffer); System.arraycopy(data, index, buffer, 0, diff); tmp = rsaEnc.doFinal(buffer, 0, buffer.length); if (tmp != null) { dos.writeInt(tmp.length); dos.write(tmp); NumeralUtil.resetByteArray(tmp); } tmp = null;//*/ index += diff; } output = NumeralUtil.toHexString(baos.toByteArray()); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error in encryption", e); } } return output; }
From source file:fedora.test.api.TestRESTAPI.java
public void testAddDatastream() throws Exception { // inline (X) datastream String xmlData = "<foo>bar</foo>"; String dsPath = "/objects/" + pid + "/datastreams/FOO"; url = dsPath + "?controlGroup=X&dsLabel=bar"; assertEquals(SC_UNAUTHORIZED, post(xmlData, false).getStatusCode()); HttpResponse response = post(xmlData, true); assertEquals(SC_CREATED, response.getStatusCode()); Header locationHeader = response.getResponseHeader("location"); assertNotNull(locationHeader);/*from w w w . ja v a 2 s. com*/ assertEquals(new URL(url.substring(0, url.indexOf('?'))).toString(), locationHeader.getValue()); assertEquals("text/xml", response.getResponseHeader("Content-Type").getValue()); url = dsPath + "?format=xml"; assertEquals(response.getResponseBodyString(), get(true).getResponseBodyString()); // managed (M) datastream String mimeType = "text/plain"; dsPath = "/objects/" + pid + "/datastreams/BAR"; url = dsPath + "?controlGroup=M&dsLabel=bar&mimeType=" + mimeType; File temp = File.createTempFile("test", null); DataOutputStream os = new DataOutputStream(new FileOutputStream(temp)); os.write(42); os.close(); assertEquals(SC_UNAUTHORIZED, post(temp, false).getStatusCode()); response = post(temp, true); assertEquals(SC_CREATED, response.getStatusCode()); locationHeader = response.getResponseHeader("location"); assertNotNull(locationHeader); assertEquals(new URL(url.substring(0, url.indexOf('?'))).toString(), locationHeader.getValue()); assertEquals("text/xml", response.getResponseHeader("Content-Type").getValue()); url = dsPath + "?format=xml"; assertEquals(response.getResponseBodyString(), get(true).getResponseBodyString()); Datastream ds = apim.getDatastream(pid.toString(), "BAR", null); assertEquals(ds.getMIMEType(), mimeType); }
From source file:org.apache.hadoop.hbase.security.visibility.ExpAsStringVisibilityLabelServiceImpl.java
private Tag createTag(ExpressionNode node) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); List<String> labels = new ArrayList<String>(); List<String> notLabels = new ArrayList<String>(); extractLabels(node, labels, notLabels); Collections.sort(labels);/*ww w. ja v a 2 s . c o m*/ Collections.sort(notLabels); // We will write the NOT labels 1st followed by normal labels // Each of the label we will write with label length (as short 1st) followed // by the label bytes. // For a NOT node we will write the label length as -ve. for (String label : notLabels) { byte[] bLabel = Bytes.toBytes(label); short length = (short) bLabel.length; length = (short) (-1 * length); dos.writeShort(length); dos.write(bLabel); } for (String label : labels) { byte[] bLabel = Bytes.toBytes(label); dos.writeShort(bLabel.length); dos.write(bLabel); } return new Tag(VISIBILITY_TAG_TYPE, baos.toByteArray()); }
From source file:org.nuxeo.ecm.core.blob.binary.AESBinaryManager.java
/** * Encrypts the given input stream into the given output stream, while also computing the digest of the input * stream.//w w w . j a v a2s. c o m * <p> * File format version 1 (values are in network order): * <ul> * <li>10 bytes: magic number "NUXEOCRYPT" * <li>1 byte: file format version = 1 * <li>1 byte: use keystore = 1, use PBKDF2 = 2 * <li>if use PBKDF2: * <ul> * <li>4 bytes: salt length = n * <li>n bytes: salt data * </ul> * <li>4 bytes: IV length = p * <li>p bytes: IV data * <li>x bytes: encrypted stream * </ul> * * @param in the input stream containing the data * @param file the file containing the encrypted data * @return the digest of the input stream */ @Override public String storeAndDigest(InputStream in, OutputStream out) throws IOException { out.write(FILE_MAGIC); DataOutputStream data = new DataOutputStream(out); data.writeByte(FILE_VERSION_1); try { // get digest to use MessageDigest messageDigest = MessageDigest.getInstance(digestAlgorithm); // secret key Key secret; if (usePBKDF2) { data.writeByte(USE_PBKDF2); // generate a salt byte[] salt = new byte[16]; RANDOM.nextBytes(salt); // generate secret key secret = generateSecretKey(salt); // write salt data.writeInt(salt.length); data.write(salt); } else { data.writeByte(USE_KEYSTORE); // find secret key from keystore secret = getSecretKey(); } // cipher Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING); cipher.init(Cipher.ENCRYPT_MODE, secret); // write IV byte[] iv = cipher.getIV(); data.writeInt(iv.length); data.write(iv); // digest and write the encrypted data CipherAndDigestOutputStream cipherOut = new CipherAndDigestOutputStream(out, cipher, messageDigest); IOUtils.copy(in, cipherOut); cipherOut.close(); byte[] digest = cipherOut.getDigest(); return toHexString(digest); } catch (GeneralSecurityException e) { throw new NuxeoException(e); } }
From source file:fedora.test.api.TestRESTAPI.java
public void testModifyDatastreamByReference() throws Exception { // Create BAR datastream url = String.format("/objects/%s/datastreams/BAR?controlGroup=M&dsLabel=bar", pid.toString()); File temp = File.createTempFile("test", null); DataOutputStream os = new DataOutputStream(new FileOutputStream(temp)); os.write(42); os.close();//from w ww.ja v a2s.c om assertEquals(SC_UNAUTHORIZED, post(temp, false).getStatusCode()); assertEquals(SC_CREATED, post(temp, true).getStatusCode()); // Update the content of the BAR datastream (using PUT) url = String.format("/objects/%s/datastreams/BAR", pid.toString()); assertEquals(SC_UNAUTHORIZED, put(temp, false).getStatusCode()); HttpResponse response = put(temp, true); assertEquals(SC_OK, response.getStatusCode()); assertEquals("text/xml", response.getResponseHeader("Content-Type").getValue()); url = url + "?format=xml"; assertEquals(response.getResponseBodyString(), get(true).getResponseBodyString()); // Ensure 404 on attempt to update BOGUS_DS via PUT url = "/objects/" + pid + "/datastreams/BOGUS_DS"; assertEquals(SC_NOT_FOUND, put(temp, true).getStatusCode()); // Update the content of the BAR datastream (using POST) url = String.format("/objects/%s/datastreams/BAR", pid.toString()); response = post(temp, true); assertEquals(SC_CREATED, response.getStatusCode()); Header locationHeader = response.getResponseHeader("location"); assertNotNull(locationHeader); assertEquals(url, locationHeader.getValue()); assertEquals("text/xml", response.getResponseHeader("Content-Type").getValue()); url = url + "?format=xml"; assertEquals(response.getResponseBodyString(), get(true).getResponseBodyString()); // Update the label of the BAR datastream String newLabel = "tikibar"; url = String.format("/objects/%s/datastreams/BAR?dsLabel=%s", pid.toString(), newLabel); assertEquals(SC_UNAUTHORIZED, put(false).getStatusCode()); assertEquals(SC_OK, put(true).getStatusCode()); assertEquals(newLabel, apim.getDatastream(pid.toString(), "BAR", null).getLabel()); // Update the location of the EXTDS datastream (E type datastream) String newLocation = "http://" + getHost() + ":" + getPort() + "/" + getFedoraAppServerContext() + "/get/demo:REST/DC"; url = String.format("/objects/%s/datastreams/EXTDS?dsLocation=%s", pid.toString(), newLocation); assertEquals(SC_UNAUTHORIZED, put(false).getStatusCode()); assertEquals(SC_OK, put(true).getStatusCode()); assertEquals(newLocation, apim.getDatastream(pid.toString(), "EXTDS", null).getLocation()); String dcDS = new String(apia.getDatastreamDissemination(pid.toString(), "DC", null).getStream()); String extDS = new String(apia.getDatastreamDissemination(pid.toString(), "EXTDS", null).getStream()); assertEquals(dcDS, extDS); // Update DS1 by reference (X type datastream) // Error expected because attempting to access internal DS with API-A auth on url = String.format("/objects/%s/datastreams/DS1?dsLocation=%s", pid.toString(), newLocation); assertEquals(SC_UNAUTHORIZED, put(false).getStatusCode()); assertEquals(SC_INTERNAL_SERVER_ERROR, put(true).getStatusCode()); // Update DS1 by reference (X type datastream) - Success expected newLocation = getBaseURL() + "/ri/index.xsl"; url = String.format("/objects/%s/datastreams/DS1?dsLocation=%s", pid.toString(), newLocation); assertEquals(SC_UNAUTHORIZED, put(false).getStatusCode()); assertEquals(SC_OK, put(true).getStatusCode()); }
From source file:com.vimc.ahttp.HurlWorker.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private void addBodyIfExists(HttpURLConnection connection, Request request) throws IOException { if (request.containsMutilpartData()) { connection.setDoOutput(true);//from w w w . j av a2s.c o m connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); if (request.getStringParams().size() > 0) { writeStringFields(request.getStringParams(), out, request.getBoundray()); } if (request.getFileParams().size() > 0) { writeFiles(request.getFileParams(), out, request.getBoundray()); } if (request.getByteParams().size() > 0) { writeBytes(request.getByteParams(), out, request.getBoundray()); } out.flush(); out.close(); } else { byte[] body = request.getStringBody(); if (body != null) { connection.setDoOutput(true); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(body); out.flush(); out.close(); } } }
From source file:com.appbase.androidquery.callback.AbstractAjaxCallback.java
private static void writeField(DataOutputStream dos, String name, String value) throws IOException { dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\""); dos.writeBytes(lineEnd);// w w w . j a va2 s . c om dos.writeBytes(lineEnd); byte[] data = value.getBytes("UTF-8"); dos.write(data); dos.writeBytes(lineEnd); }