List of usage examples for java.io ByteArrayOutputStream reset
public synchronized void reset()
From source file:com.streamsets.pipeline.stage.origin.kafka.TestKafkaSource.java
@Test public void testProduceProtobufRecords() throws StageException, InterruptedException, IOException { Producer<String, byte[]> producer = createDefaultProducer(); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); //send 10 protobuf messages to kafka topic for (int i = 0; i < 10; i++) { ProtobufTestUtil.getSingleProtobufData(bOut, i); producer.send(new KeyedMessage<>(TOPIC15, "0", bOut.toByteArray())); bOut.reset(); }//w w w. ja v a 2 s. c o m bOut.close(); Map<String, String> kafkaConsumerConfigs = new HashMap<>(); sdcKafkaTestUtil.setAutoOffsetReset(kafkaConsumerConfigs); KafkaConfigBean conf = new KafkaConfigBean(); conf.metadataBrokerList = sdcKafkaTestUtil.getMetadataBrokerURI(); conf.topic = TOPIC15; conf.consumerGroup = CONSUMER_GROUP; conf.zookeeperConnect = zkConnect; conf.maxBatchSize = 10; conf.maxWaitTime = 5000; conf.kafkaConsumerConfigs = kafkaConsumerConfigs; conf.produceSingleRecordPerMessage = false; conf.dataFormat = DataFormat.PROTOBUF; conf.dataFormatConfig.charset = "UTF-8"; conf.dataFormatConfig.removeCtrlChars = false; conf.dataFormatConfig.protoDescriptorFile = protoDescFile.getPath(); conf.dataFormatConfig.messageType = "util.Employee"; conf.dataFormatConfig.isDelimited = true; SourceRunner sourceRunner = new SourceRunner.Builder(StandaloneKafkaSource.class, createSource(conf)) .addOutputLane("lane").build(); sourceRunner.runInit(); List<Record> records = new ArrayList<>(); StageRunner.Output output = getOutputAndRecords(sourceRunner, 10, "lane", records); String newOffset = output.getNewOffset(); Assert.assertNull(newOffset); Assert.assertEquals(10, records.size()); ProtobufTestUtil.compareProtoRecords(records, 0); sourceRunner.runDestroy(); }
From source file:com.arpnetworking.tsdcore.sinks.KairosDbSink.java
private void addChunk(final ByteArrayOutputStream chunkStream, final ByteBuffer currentChunk, final Collection<byte[]> completedChunks) { final byte[] nextChunk = chunkStream.toByteArray(); final int nextChunkSize = nextChunk.length; if (currentChunk.position() + nextChunkSize > _maxRequestSize) { if (currentChunk.position() > HEADER_BYTE_LENGTH) { // TODO(vkoskela): Add chunk size metric. [MAI-?] // Copy the relevant part of the buffer currentChunk.put(currentChunk.position() - 1, FOOTER); completedChunks.add(Arrays.copyOf(currentChunk.array(), currentChunk.position())); // Truncate all but the beginning '[' to prepare the next entries currentChunk.clear();//from ww w . j a v a 2s . com currentChunk.put(HEADER); } else { CHUNK_TOO_BIG_LOGGER.warn().setMessage("First chunk too big").addData("sink", getName()) .addData("bufferLength", currentChunk.position()).addData("nextChunkSize", nextChunkSize) .addData("maxRequestSIze", _maxRequestSize).log(); } } currentChunk.put(nextChunk); currentChunk.put(SEPARATOR); chunkStream.reset(); }
From source file:org.firstopen.singularity.devicemgr.interrogator.WaveTrend_IO.java
/** * Header 1 Byte [0x55] 10. Length 1 Byte (Number of Bytes in data section) * 11. Network ID 1 Byte 12. Receiver ID 1 Byte 13. Node ID 1 Byte 14. * Command 1 Byte 15. Data Up to 64 Bytes of Data 16. Checksum 1 Byte (XOR * from Length to Last Data Byte), CRC/* w ww . jav a 2 s . c o m*/ * * @param message * @return messageList */ protected ArrayList<byte[]> findMessage(ByteArrayOutputStream message) { ArrayList<byte[]> messageList = new ArrayList<byte[]>(); synchronized (message) { // log.debug("message is " + message + " record length = " + // MAX_RECORD_LENGTH); byte[] buffer = message.toByteArray(); message.reset(); log.debug("found message is ->" + bytesToHex(buffer)); for (int i = 0; i < buffer.length - 1; i++) { if (byteToHex(buffer[i]).equals(commands.get(command.RESPONSE))) { int dataLength = Integer.valueOf(byteToHex(buffer[i + 1]), 16); /* * if message is not complete place it back in the origianl * message buffer */ if (i + 7 + dataLength - 1 > buffer.length) { message.write(buffer, i, buffer.length - i); break; } else { byte[] messageBuf = new byte[dataLength + 7]; System.arraycopy(buffer, i, messageBuf, 0, dataLength + 7); log.debug("message found, and added to list -> " + bytesToHex(messageBuf)); messageList.add(messageBuf); } } // end if start message found } } return messageList; }
From source file:org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.java
/** * Compute the owner entry in the encryption dictionary. * * @param ownerPassword The plaintext owner password. * @param userPassword The plaintext user password. * @param encRevision The revision number of the encryption algorithm. * @param length The length of the encryption key. * * @return The o entry of the encryption dictionary. * * @throws IOException if the owner password could not be computed *///from w w w . ja va 2s .co m public byte[] computeOwnerPassword(byte[] ownerPassword, byte[] userPassword, int encRevision, int length) throws IOException { if (encRevision == 2 && length != 5) { throw new IOException("Expected length=5 actual=" + length); } byte[] rc4Key = computeRC4key(ownerPassword, encRevision, length); byte[] paddedUser = truncateOrPad(userPassword); ByteArrayOutputStream encrypted = new ByteArrayOutputStream(); encryptDataRC4(rc4Key, new ByteArrayInputStream(paddedUser), encrypted); if (encRevision == 3 || encRevision == 4) { byte[] iterationKey = new byte[rc4Key.length]; for (int i = 1; i < 20; i++) { System.arraycopy(rc4Key, 0, iterationKey, 0, rc4Key.length); for (int j = 0; j < iterationKey.length; j++) { iterationKey[j] = (byte) (iterationKey[j] ^ (byte) i); } ByteArrayInputStream input = new ByteArrayInputStream(encrypted.toByteArray()); encrypted.reset(); encryptDataRC4(iterationKey, input, encrypted); } } return encrypted.toByteArray(); }
From source file:com.orange.api.atmosdav.AtmosDavServlet.java
public static String encode(String s) { int maxBytesPerChar = 10; // rather arbitrary limit, but safe for now StringBuffer out = new StringBuffer(s.length()); ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar); try {/*from w w w. jav a2s. c o m*/ OutputStreamWriter writer = new OutputStreamWriter(buf, dfltEncName); for (int i = 0; i < s.length(); i++) { int c = (int) s.charAt(i); if (dontNeedEncoding.get(c)) { out.append((char) c); } else { // convert to external encoding before hex conversion try { writer.write(c); /* * If this character represents the start of a Unicode * surrogate pair, then pass in two characters. It's not * clear what should be done if a bytes reserved in the * surrogate pairs range occurs outside of a legal * surrogate pair. For now, just treat it as if it were * any other character. */ if (c >= 0xD800 && c <= 0xDBFF) { if ((i + 1) < s.length()) { int d = (int) s.charAt(i + 1); if (d >= 0xDC00 && d <= 0xDFFF) { writer.write(d); i++; } } } writer.flush(); } catch (IOException e) { buf.reset(); continue; } byte[] ba = buf.toByteArray(); for (int j = 0; j < ba.length; j++) { out.append('%'); char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16); // converting to use uppercase letter as part of // the hex value if ch is a letter. if (Character.isLetter(ch)) { ch -= caseDiff; } out.append(ch); ch = Character.forDigit(ba[j] & 0xF, 16); if (Character.isLetter(ch)) { ch -= caseDiff; } out.append(ch); } buf.reset(); } } return out.toString(); } catch (UnsupportedEncodingException e) { return s; } }
From source file:org.apache.hadoop.hbase.security.visibility.VisibilityController.java
private List<Tag> createVisibilityTags(String visibilityLabelsExp) throws IOException, ParseException, InvalidLabelException { ExpressionNode node = null;//from ww w .jav a 2 s. co m node = this.expressionParser.parse(visibilityLabelsExp); node = this.expressionExpander.expand(node); List<Tag> tags = new ArrayList<Tag>(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); if (node.isSingleNode()) { writeLabelOrdinalsToStream(node, dos); tags.add(new Tag(VisibilityUtils.VISIBILITY_TAG_TYPE, baos.toByteArray())); baos.reset(); } else { NonLeafExpressionNode nlNode = (NonLeafExpressionNode) node; if (nlNode.getOperator() == Operator.OR) { for (ExpressionNode child : nlNode.getChildExps()) { writeLabelOrdinalsToStream(child, dos); tags.add(new Tag(VisibilityUtils.VISIBILITY_TAG_TYPE, baos.toByteArray())); baos.reset(); } } else { writeLabelOrdinalsToStream(nlNode, dos); tags.add(new Tag(VisibilityUtils.VISIBILITY_TAG_TYPE, baos.toByteArray())); baos.reset(); } } return tags; }
From source file:com.jivesoftware.os.amza.service.AmzaService.java
@Override public void availableRowsStream(boolean system, ChunkWriteable writeable, RingMember remoteRingMember, TimestampedRingHost remoteTimestampedRingHost, long takeSessionId, long sharedKey, long heartbeatIntervalMillis) throws Exception { ringStoreWriter.register(remoteRingMember, remoteTimestampedRingHost.ringHost, remoteTimestampedRingHost.timestampId, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new SnappyOutputStream(out), 8192)); takeCoordinator.availableRowsStream(system, ringStoreReader, partitionStripeProvider, remoteRingMember, takeSessionId, sharedKey, heartbeatIntervalMillis, (partitionName, txId) -> { dos.write(1);//from w w w . ja v a 2s. c o m byte[] bytes = partitionName.toBytes(); dos.writeInt(bytes.length); dos.write(bytes); dos.writeLong(txId); }, () -> { if (dos.size() > 0) { dos.flush(); byte[] chunk = out.toByteArray(); writeable.write(chunk); /*LOG.info("Offered rows for {} length={}", remoteRingMember, chunk.length);*/ out.reset(); } return null; }, () -> { dos.write(1); dos.writeInt(0); dos.flush(); writeable.write(out.toByteArray()); out.reset(); return null; }); dos.write(0); dos.flush(); writeable.write(out.toByteArray()); }
From source file:org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.java
/** * Get the user password based on the owner password. * * @param ownerPassword The plaintext owner password. * @param owner The o entry of the encryption dictionary. * @param encRevision The encryption revision number. * @param length The key length./*w w w . j ava 2s .com*/ * * @return The u entry of the encryption dictionary. * * @throws IOException If there is an error accessing data while generating the user password. */ public byte[] getUserPassword(byte[] ownerPassword, byte[] owner, int encRevision, int length) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] rc4Key = computeRC4key(ownerPassword, encRevision, length); if (encRevision == 2) { encryptDataRC4(rc4Key, owner, result); } else if (encRevision == 3 || encRevision == 4) { byte[] iterationKey = new byte[rc4Key.length]; byte[] otemp = new byte[owner.length]; System.arraycopy(owner, 0, otemp, 0, owner.length); for (int i = 19; i >= 0; i--) { System.arraycopy(rc4Key, 0, iterationKey, 0, rc4Key.length); for (int j = 0; j < iterationKey.length; j++) { iterationKey[j] = (byte) (iterationKey[j] ^ (byte) i); } result.reset(); encryptDataRC4(iterationKey, otemp, result); otemp = result.toByteArray(); } } return result.toByteArray(); }
From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;/* w w w. j ava 2 s . c o m*/ XMLStreamReader rdr = null; OutputStream out = null; XMLStreamWriter outWrt = null; try { Set<String> processedPropertyNames = new HashSet<String>(); in = new BufferedInputStream(new FileInputStream(configFile)); rdr = XML_INPUT_FACTORY.createXMLStreamReader(in); File tmpFile = File.createTempFile("hadoop-plugin", null); out = new FileOutputStream(tmpFile); outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); ByteArrayOutputStream stash = new ByteArrayOutputStream(); XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash); boolean outputActive = true; outWrt.writeStartDocument(); while (rdr.hasNext()) { int event = rdr.next(); XMLStreamWriter wrt = outputActive ? outWrt : stashWrt; switch (event) { case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CDATA: wrt.writeCData(rdr.getText()); break; case XMLStreamConstants.CHARACTERS: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.COMMENT: wrt.writeComment(rdr.getText()); break; case XMLStreamConstants.DTD: wrt.writeDTD(rdr.getText()); break; case XMLStreamConstants.END_DOCUMENT: wrt.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { String encoding = rdr.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } String propertyTagSoFar = Charset.forName(encoding) .decode(ByteBuffer.wrap(stash.toByteArray())).toString(); DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps); //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write. outWrt.flush(); out.write(propAndTag.updatedTag.getBytes("UTF-8")); processedPropertyNames.add(propAndTag.propertyName); //reset stuff stash.reset(); wrt = outWrt; outputActive = true; } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) { //now add the new props for (String prop : processedPropertyNames) { allProps.remove(prop); } for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) { outWrt.writeStartElement(PROPERTY_TAG_NAME); outWrt.writeStartElement(NAME_TAG_NAME); outWrt.writeCharacters(e.getKey()); outWrt.writeEndElement(); outWrt.writeStartElement(VALUE_TAG_NAME); outWrt.writeCharacters(e.getValue().getStringValue()); outWrt.writeEndElement(); outWrt.writeEndElement(); } } wrt.writeEndElement(); break; case XMLStreamConstants.ENTITY_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.ENTITY_REFERENCE: wrt.writeEntityRef(rdr.getText()); break; case XMLStreamConstants.NAMESPACE: for (int i = 0; i < rdr.getNamespaceCount(); ++i) { wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i)); } break; case XMLStreamConstants.NOTATION_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.PROCESSING_INSTRUCTION: wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData()); break; case XMLStreamConstants.SPACE: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.START_DOCUMENT: //this seems to be never called for some strange reason //wrt.writeStartDocument(); break; case XMLStreamConstants.START_ELEMENT: wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(), rdr.getName().getNamespaceURI()); for (int i = 0; i < rdr.getAttributeCount(); ++i) { wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i), rdr.getAttributeLocalName(i), rdr.getAttributeValue(i)); } if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { wrt.writeCharacters(""); outputActive = false; } break; } } outWrt.flush(); out.flush(); out.close(); in.close(); //now copy the temp file in the place of the original one FileUtil.copyFile(tmpFile, configFile); } finally { rdr.close(); outWrt.flush(); outWrt.close(); try { in.close(); } finally { out.flush(); out.close(); } } }
From source file:org.apache.pig.piggybank.storage.GAMultiStorage.java
@Override public void putNext(Tuple t) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(BUF_SIZE); // this magical limit is related to memory size of reducer to prevent OOM while trying // to write data for big games int limit = 67108864 / 8; boolean partialSave = false; try {// ww w . j ava 2s . c o m DataBag bag = (DataBag) t.get(1); String gameId = String.valueOf(((Tuple) t.get(0)).get(0)); if (this.statsOnly) { // produces debug memory/size stats in CSV format writer.write(gameId, new Text( gameId + ";" + String.valueOf(bag.size()) + ";" + String.valueOf(t.getMemorySize()))); } else { Iterator<Tuple> iterator = bag.iterator(); while (iterator.hasNext()) { Tuple t1 = (Tuple) iterator.next(); String line = t1.get(2).toString(); // we are using multiple files together and collectors // do not append end line if (!line.endsWith("\n")) { line += "\n"; } baos.write(line.getBytes(Charset.forName("UTF-8"))); // save partial result to prevent VM array limit exceptions if (baos.size() >= limit) { writer.write(String.valueOf(gameId), RemoveLastByte(baos.toByteArray())); baos.reset(); partialSave = true; } else { partialSave = false; } } if (!partialSave) { writer.write(String.valueOf(gameId), RemoveLastByte(baos.toByteArray())); } } } catch (InterruptedException ie) { throw new IOException(ie); } catch (Exception e) { throw new IOException(e); } }