List of usage examples for java.io DataOutputStream writeByte
public final void writeByte(int v) throws IOException
byte
to the underlying output stream as a 1-byte value. From source file:com.codefollower.lealone.omid.tso.TSOHandler.java
private void handleHalfAbort(long startTimestamp) { abortCounter.incrementAndGet();/*from ww w . j av a 2s . c o m*/ synchronized (sharedState) { DataOutputStream toWAL = sharedState.toWAL; try { toWAL.writeByte(LoggerProtocol.ABORT); toWAL.writeLong(startTimestamp); } catch (IOException e) { LOG.error("failed to write abort wal", e); } sharedState.processHalfAbort(startTimestamp); synchronized (sharedMsgBufLock) { queueHalfAbort(startTimestamp); } } }
From source file:org.apache.hadoop.io.TestArrayOutputStream.java
private void runComparison(ArrayOutputStream aos, DataOutputStream dos, ByteArrayOutputStream bos) throws IOException { Random r = new Random(); // byte// w w w . j a va 2 s . c o m int b = r.nextInt(128); aos.write(b); dos.write(b); // byte[] byte[] bytes = new byte[10]; r.nextBytes(bytes); aos.write(bytes, 0, 10); dos.write(bytes, 0, 10); // Byte aos.writeByte(b); dos.writeByte(b); // boolean boolean bool = r.nextBoolean(); aos.writeBoolean(bool); dos.writeBoolean(bool); // short short s = (short) r.nextInt(); aos.writeShort(s); dos.writeShort(s); // char int c = r.nextInt(); aos.writeChar(c); dos.writeChar(c); // int int i = r.nextInt(); aos.writeInt(i); dos.writeInt(i); // long long l = r.nextLong(); aos.writeLong(l); dos.writeLong(l); // float float f = r.nextFloat(); aos.writeFloat(f); dos.writeFloat(f); // double double d = r.nextDouble(); aos.writeDouble(d); dos.writeDouble(d); // strings String str = RandomStringUtils.random(20); aos.writeBytes(str); aos.writeChars(str); aos.writeUTF(str); dos.writeBytes(str); dos.writeChars(str); dos.writeUTF(str); byte[] expected = bos.toByteArray(); assertEquals(expected.length, aos.size()); byte[] actual = new byte[aos.size()]; System.arraycopy(aos.getBytes(), 0, actual, 0, actual.length); // serialized bytes should be the same assertTrue(Arrays.equals(expected, actual)); }
From source file:org.openxdata.server.service.impl.FormDownloadServiceImpl.java
@Override public void submitForms(InputStream is, OutputStream os, String serializerName) { //When submitting data, we need all the form versions and not just the default ones //because the user can change the default form version which already has data on //mobile devices. So getting all versions shields us from such problems. List<FormData> formDataList = new ArrayList<FormData>(); User user = userService.getLoggedInUser(); XformSerializer formSerializer = serializationService.getFormSerializer(serializerName); Map<Integer, String> formsVersionXmlMap = getFormsVersionXmlMap(); List<String> xforms = (List<String>) formSerializer.deSerialize(is, formsVersionXmlMap); if (xforms == null || xforms.size() == 0) { throw new UnexpectedException("Problem encountered while deserializing data."); }//from w w w . j av a 2s .c o m try { DocumentBuilder db = dbf.newDocumentBuilder(); for (String xml : xforms) { Document doc = db.parse(IOUtils.toInputStream(xml, "UTF-8")); Integer formId = Integer .valueOf(doc.getDocumentElement().getAttribute(OpenXDataConstants.ATTRIBUTE_NAME_FORMID)); String descTemplate = doc.getDocumentElement() .getAttribute(OpenXDataConstants.ATTRIBUTE_NAME_DESCRIPTION_TEMPLATE); FormData formData = saveFormData(formId, xml, XmlUtil.getDescriptionTemplate(doc.getDocumentElement(), descTemplate), user, new Date()); formDataList.add(formData); } // serialize the summaries DataOutputStream dos = new DataOutputStream(os); dos.writeByte(formDataList.size()); for (FormData fd : formDataList) { String description = fd.getDescription(); if (description != null) { dos.writeUTF(description); } else { dos.writeUTF("form {" + fd.getFormDefVersionId() + "}"); // description can be null } dos.writeInt(fd.getFormDataId()); } } catch (ParserConfigurationException ex) { throw new UnexpectedException(ex); } catch (SAXException ex) { throw new UnexpectedException(ex); } catch (IOException ex) { throw new UnexpectedException(ex); } }
From source file:com.yahoo.omid.tso.TSOHandler.java
public void handle(AbortRequest msg, ChannelHandlerContext ctx) { synchronized (sharedState) { DataOutputStream toWAL = sharedState.toWAL; try {/* ww w . ja va 2 s .c o m*/ toWAL.writeByte(LoggerProtocol.ABORT); toWAL.writeLong(msg.startTimestamp); } catch (IOException e) { e.printStackTrace(); } abortCount++; sharedState.processAbort(msg.startTimestamp); synchronized (sharedMsgBufLock) { queueHalfAbort(msg.startTimestamp); } } }
From source file:com.yahoo.omid.tso.TSOHandler.java
/** * Handle the FullAbortReport message//from ww w. j a v a2 s . c o m */ public void handle(FullAbortRequest msg, ChannelHandlerContext ctx) { synchronized (sharedState) { DataOutputStream toWAL = sharedState.toWAL; try { toWAL.writeByte(LoggerProtocol.FULLABORT); toWAL.writeLong(msg.startTimestamp); } catch (IOException e) { e.printStackTrace(); } sharedState.processFullAbort(msg.startTimestamp); } synchronized (sharedMsgBufLock) { queueFullAbort(msg.startTimestamp); } }
From source file:com.chaosinmotion.securechat.server.messages.NotificationSocket.java
/** * Internal method for sending a message to the specified device. This * encodes the message as a binary array and transmits it as a single * packet to the listening device. This allows users to receive messages * during chat as soon as we are able to, for (more or less) just in time * messaging./* w w w .jav a2s .c o m*/ * * The packet returned here is similar to the packet returned by the * getmessages api, except we serialize as binary. * * @param messageid * @param senderid * @param sendername * @param ts * @param message * @throws IOException */ void sendMessage(int messageid, int senderid, String sendername, boolean toflag, Timestamp ts, byte[] message) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); /* * Encode. First byte is 0x20 */ String date; synchronized (format) { date = format.format(ts); } /* * Formulate packet in expected format */ dos.writeByte(0x20); // marker dos.writeBoolean(toflag); dos.writeInt(messageid); dos.writeInt(senderid); dos.writeUTF(date); dos.writeUTF(sendername); dos.writeInt(message.length); dos.write(message); /* * Flush and write packet to device. Our protocol does not depend on * the device actually receiving this message, as we wait until the * device deletes the messages by a separate command. */ dos.flush(); out.writeData(baos.toByteArray()); }
From source file:com.codefollower.lealone.omid.tso.TSOHandler.java
private void createAbortedSnapshot() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream toWAL = new DataOutputStream(baos); long snapshot = sharedState.hashmap.getAndIncrementAbortedSnapshot(); try {//from w w w . j a v a 2 s.co m toWAL.writeByte(LoggerProtocol.SNAPSHOT); toWAL.writeLong(snapshot); for (AbortedTransaction aborted : sharedState.hashmap.halfAborted) { // ignore aborted transactions from last snapshot if (aborted.getSnapshot() < snapshot) { toWAL.writeByte(LoggerProtocol.ABORT); toWAL.writeLong(aborted.getStartTimestamp()); } } } catch (IOException e) { // can't happen throw new RuntimeException(e); } sharedState.addRecord(baos.toByteArray(), noCallback, null); }
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.// ww w. j a va 2 s . com * <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:org.openxdata.server.service.impl.FormDownloadServiceImpl.java
@Override public void downloadLocales(InputStream is, OutputStream os, String serializerName) throws IOException { DataOutputStream dos = new DataOutputStream(os); List<Locale> locales = localeService.getLocales(); if (locales == null || locales.size() == 0) dos.writeByte(0); else {/*from w ww . j a v a 2s . co m*/ dos.writeByte(locales.size()); for (Locale locale : locales) { dos.writeUTF(locale.getKey()); dos.writeUTF(locale.getName()); } } }
From source file:com.yahoo.omid.tso.TSOHandler.java
public void createAbortedSnapshot() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream toWAL = new DataOutputStream(baos); long snapshot = sharedState.hashmap.getAndIncrementAbortedSnapshot(); try {//from w w w.j a va 2 s . co m toWAL.writeByte(LoggerProtocol.SNAPSHOT); toWAL.writeLong(snapshot); for (AbortedTransaction aborted : sharedState.hashmap.halfAborted) { // ignore aborted transactions from last snapshot if (aborted.getSnapshot() < snapshot) { toWAL.writeByte(LoggerProtocol.ABORT); toWAL.writeLong(aborted.getStartTimestamp()); } } } catch (IOException e) { // can't happen throw new RuntimeException(e); } sharedState.addRecord(baos.toByteArray(), noCallback, null); }