List of usage examples for java.io DataOutputStream write
public synchronized void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to the underlying output stream. From source file:dj.comprobantes.offline.service.CPanelServiceImp.java
@Override public boolean guardarComprobanteNube(Comprobante comprobante) throws GenericException { boolean guardo = true; UtilitarioCeo utilitario = new UtilitarioCeo(); Map<String, Object> params = new LinkedHashMap<>(); params.put("NOMBRE_USUARIO", comprobante.getCliente().getNombreCliente()); params.put("IDENTIFICACION_USUARIO", comprobante.getCliente().getIdentificacion()); params.put("CORREO_USUARIO", comprobante.getCliente().getCorreo()); params.put("CODIGO_ESTADO", EstadoUsuarioEnum.NUEVO.getCodigo()); params.put("DIRECCION_USUARIO", comprobante.getCliente().getDireccion()); params.put("PK_CODIGO_COMP", comprobante.getCodigocomprobante()); params.put("CODIGO_DOCUMENTO", comprobante.getCoddoc()); params.put("ESTADO", EstadoComprobanteEnum.AUTORIZADO.getDescripcion()); params.put("CLAVE_ACCESO", comprobante.getClaveacceso()); params.put("SECUENCIAL", comprobante.getSecuencial()); params.put("FECHA_EMISION", utilitario.getFormatoFecha(comprobante.getFechaemision())); params.put("NUM_AUTORIZACION", comprobante.getNumAutorizacion()); params.put("FECHA_AUTORIZACION", utilitario.getFormatoFecha(comprobante.getFechaautoriza())); params.put("ESTABLECIM", comprobante.getEstab()); params.put("PTO_EMISION", comprobante.getPtoemi()); params.put("TOTAL", comprobante.getImportetotal()); params.put("CODIGO_EMPR", ParametrosSistemaEnum.CODIGO_EMPR.getCodigo()); params.put("CORREO_DOCUMENTO", comprobante.getCorreo()); //Para guardar el correo que se envio el comprobante byte[] bxml = archivoService.getXml(comprobante); StringBuilder postData = new StringBuilder(); postData.append("{"); for (Map.Entry<String, Object> param : params.entrySet()) { if (postData.length() != 1) { postData.append(','); }/* ww w . j a v a2 s. c o m*/ postData.append("\"").append(param.getKey()).append("\""); postData.append(":\""); postData.append(String.valueOf(param.getValue())).append("\""); } String encodedfileXML = new String(Base64.encodeBase64(bxml)); params.put("ARCHIVO_XML", encodedfileXML); //guarda en la nuebe el comprobante AUTORIZADO String filefield = "pdf"; String fileMimeType = "application/pdf"; HttpURLConnection connection = null; DataOutputStream outputStream = null; InputStream inputStream = null; String twoHyphens = "--"; String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****"; String lineEnd = "\r\n"; String result = ""; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; String fileName = comprobante.getClaveacceso() + ".pdf"; try { byte[] bpdf = archivoService.getPdf(comprobante); File file = File.createTempFile(comprobante.getClaveacceso(), ".tmp"); file.deleteOnExit(); try (FileOutputStream outputStream1 = new FileOutputStream(file);) { outputStream1.write(bpdf); //write the bytes and your done. outputStream1.close(); } catch (Exception e) { e.printStackTrace(); } FileInputStream fileInputStream = new FileInputStream(file); URL url = new URL(ParametrosSistemaEnum.CPANEL_WEB_COMPROBANTE.getCodigo()); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + fileName + "\"" + lineEnd); outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd); outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); // Upload POST Data Iterator<String> keys = params.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); String value = String.valueOf(params.get(key)); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd); outputStream.writeBytes("Content-Type: text/plain" + lineEnd); outputStream.writeBytes(lineEnd); outputStream.writeBytes(value); outputStream.writeBytes(lineEnd); } outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); if (200 != connection.getResponseCode()) { throw new Exception("Failed to upload code:" + connection.getResponseCode() + " " + connection.getResponseMessage()); } inputStream = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader((inputStream))); String output; while ((output = br.readLine()) != null) { result += output; } System.out.println(result); fileInputStream.close(); inputStream.close(); outputStream.flush(); outputStream.close(); //CAMBIA DE ESTADO A GUARDDADO EN LA NUBE comprobante.setEnNube(true); comprobanteDAO.actualizar(comprobante); } catch (Exception e) { guardo = false; throw new GenericException(e); } if (guardo) { //Guarda el detalle de la factura if (comprobante.getCoddoc().equals(TipoComprobanteEnum.FACTURA.getCodigo())) { for (DetalleComprobante detActual : comprobante.getDetalle()) { guardarDetalleComprobanteNube(detActual); } } } return guardo; }
From source file:org.openestate.is24.restapi.DefaultClient.java
@Override protected Response sendXmlAttachmentRequest(URL url, RequestMethod method, String xml, InputStream input, String fileName, String mimeType) throws IOException, OAuthException { if (method == null) method = RequestMethod.POST;/*from w w w .j a v a 2 s . co m*/ if (!RequestMethod.POST.equals(method) && !RequestMethod.PUT.equals(method)) throw new IllegalArgumentException("Invalid request method!"); xml = (RequestMethod.POST.equals(method) || RequestMethod.PUT.equals(method)) ? StringUtils.trimToNull(xml) : null; final String twoHyphens = "--"; final String lineEnd = "\r\n"; final String boundary = "*****" + RandomStringUtils.random(25, true, true) + "*****"; HttpURLConnection connection = null; DataOutputStream output = null; try { // create connection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.name()); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); connection.setRequestProperty("Content-Language", "en-US"); connection.setRequestProperty("Accept", "application/xml"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); getAuthConsumer().sign(connection); output = (xml != null || input != null) ? new DataOutputStream(connection.getOutputStream()) : null; // send xml part if (xml != null && output != null) { output.writeBytes(twoHyphens + boundary + lineEnd); output.writeBytes("Content-Type: application/xml; name=body.xml" + lineEnd); output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); output.writeBytes( "Content-Disposition: form-data; name=\"metadata\"; filename=\"body.xml\"" + lineEnd); output.writeBytes(lineEnd); output.writeBytes(xml); output.writeBytes(lineEnd); output.flush(); } // send file part if (input != null && output != null) { mimeType = StringUtils.trimToNull(mimeType); if (mimeType != null) mimeType = "application/octet-stream"; fileName = StringUtils.trimToNull(fileName); if (fileName == null) fileName = "upload.bin"; output.writeBytes(twoHyphens + boundary + lineEnd); output.writeBytes("Content-Type: " + mimeType + "; name=\"" + fileName + "\"" + lineEnd); output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); output.writeBytes("Content-Disposition: form-data; name=\"attachment\"; filename=\"" + fileName + "\"" + lineEnd); output.writeBytes(lineEnd); byte[] buffer = new byte[4096]; int i = input.read(buffer, 0, buffer.length); while (i > 0) { output.write(buffer, 0, i); i = input.read(buffer, 0, buffer.length); } output.writeBytes(lineEnd); } if (output != null) { output.writeBytes(twoHyphens + boundary + lineEnd); } if (output != null) output.flush(); connection.connect(); // read response into string return createResponse(connection); } finally { IOUtils.closeQuietly(output); IOUtils.close(connection); } }
From source file:org.openestate.is24.restapi.DefaultClient.java
@Override protected Response sendVideoUploadRequest(URL url, RequestMethod method, String auth, InputStream input, String fileName, long fileSize) throws IOException, OAuthException { if (method == null) method = RequestMethod.POST;//from w w w. j av a 2 s. c o m if (!RequestMethod.POST.equals(method) && !RequestMethod.PUT.equals(method)) throw new IllegalArgumentException("Invalid request method!"); auth = StringUtils.trimToNull(auth); final String twoHyphens = "--"; final String lineEnd = "\r\n"; final String boundary = "*****" + RandomStringUtils.random(25, true, true) + "*****"; HttpURLConnection connection = null; DataOutputStream output = null; try { // create connection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.name()); connection.setRequestProperty("MIME-Version", "1.0"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); connection.setRequestProperty("Content-Language", "en-US"); connection.setRequestProperty("Accept-Encoding", "gzip,deflate"); connection.setRequestProperty("Connection", "close"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); //getAuthConsumer().sign( connection ); output = (auth != null || input != null) ? new DataOutputStream(connection.getOutputStream()) : null; // send auth part if (auth != null && output != null) { output.writeBytes(twoHyphens + boundary + lineEnd); output.writeBytes("Content-Type: text/plain; charset=" + getEncoding() + lineEnd); output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); output.writeBytes("Content-Disposition: form-data; name=\"auth\"" + lineEnd); output.writeBytes(lineEnd); output.writeBytes(auth); output.writeBytes(lineEnd); output.flush(); } // send file part if (input != null && output != null) { fileName = StringUtils.trimToNull(fileName); if (fileName == null) fileName = "upload.bin"; output.writeBytes(twoHyphens + boundary + lineEnd); output.writeBytes("Content-Type: application/octet-stream; name=\"" + fileName + "\"" + lineEnd); output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); output.writeBytes("Content-Length: " + String.valueOf(fileSize) + lineEnd); output.writeBytes("Content-Disposition: form-data; name=\"videofile\"; filename=\"" + fileName + "\"" + lineEnd); output.writeBytes(lineEnd); byte[] buffer = new byte[4096]; int i = input.read(buffer, 0, buffer.length); while (i > 0) { output.write(buffer, 0, i); i = input.read(buffer, 0, buffer.length); } output.writeBytes(lineEnd); } if (output != null) { output.writeBytes(twoHyphens + boundary + lineEnd); } if (output != null) output.flush(); connection.connect(); // read response into string return createResponse(connection); } finally { IOUtils.closeQuietly(output); IOUtils.close(connection); } }
From source file:com.chinamobile.bcbsp.partition.HashWithBalancerWritePartition.java
/** * This method is used to partition graph vertexes. Writing Each vertex to the * corresponding partition. In this method calls recordParse method to create * an HeadNode object. The last call partitioner's getPartitionId method to * calculate the HeadNode belongs to partition's id. If the HeadNode belongs * local partition then written to the local partition or send it to the * appropriate partition./*from w w w . j a va 2 s. com*/ * @param recordReader The recordreader of the split. * @throws IOException The io exception * @throws InterruptedException The Interrupted Exception */ @Override public void write(RecordReader recordReader) throws IOException, InterruptedException { int headNodeNum = 0; int local = 0; int send = 0; int lost = 0; ThreadPool tpool = new ThreadPool(this.sendThreadNum); int staffNum = this.staff.getStaffNum(); BytesWritable kbytes = new BytesWritable(); int ksize = 0; BytesWritable vbytes = new BytesWritable(); int vsize = 0; DataOutputBuffer bb = new DataOutputBuffer(); int bufferSize = (int) ((this.TotalCacheSize * CONTAINERNUMBER * CONTAINERNUMBER) * PART); int dataBufferSize = (this.TotalCacheSize * CONTAINERNUMBER * CONTAINERNUMBER) / (this.staff.getStaffNum() + this.sendThreadNum); byte[] buffer = new byte[bufferSize]; int bufindex = 0; SerializationFactory sFactory = new SerializationFactory(new Configuration()); Serializer<IntWritable> psserializer = sFactory.getSerializer(IntWritable.class); byte[] pidandsize = new byte[TIME * CONTAINERNUMBER * CONTAINERNUMBER]; int psindex = 0; BytesWritable pidbytes = new BytesWritable(); int psize = 0; BytesWritable sizebytes = new BytesWritable(); int ssize = 0; try { this.keyserializer.open(bb); this.valueserializer.open(bb); psserializer.open(bb); } catch (IOException e) { throw e; } String path = "/tmp/bcbsp/" + this.staff.getJobID() + "/" + this.staff.getStaffID(); File dir = new File("/tmp/bcbsp/" + this.staff.getJobID()); dir.mkdir(); dir = new File("/tmp/bcbsp/" + this.staff.getJobID() + "/" + this.staff.getStaffID()); dir.mkdir(); ArrayList<File> files = new ArrayList<File>(); try { File file = new File(path + "/" + "data" + ".txt"); files.add(file); DataOutputStream dataWriter = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(path + "/" + "data" + ".txt", true))); DataInputStream dataReader = new DataInputStream( new BufferedInputStream(new FileInputStream(path + "/" + "data" + ".txt"))); File filet = new File(path + "/" + "pidandsize" + ".txt"); files.add(filet); DataOutputStream psWriter = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(path + "/" + "pidandsize" + ".txt", true))); DataInputStream psReader = new DataInputStream( new BufferedInputStream(new FileInputStream(path + "/" + "pidandsize" + ".txt"))); while (recordReader != null && recordReader.nextKeyValue()) { headNodeNum++; Text key = new Text(recordReader.getCurrentKey().toString()); Text value = new Text(recordReader.getCurrentValue().toString()); int pid = -1; Text vertexID = this.recordParse.getVertexID(key); if (vertexID != null) { pid = this.partitioner.getPartitionID(vertexID); } else { lost++; continue; } if (this.counter.containsKey(pid)) { this.counter.put(pid, (this.counter.get(pid) + 1)); } else { this.counter.put(pid, 1); } bb.reset(); this.keyserializer.serialize(key); kbytes.set(bb.getData(), 0, bb.getLength()); ksize = kbytes.getLength(); bb.reset(); this.valueserializer.serialize(value); vbytes.set(bb.getData(), 0, bb.getLength()); vsize = vbytes.getLength(); bb.reset(); psserializer.serialize(new IntWritable(ksize + vsize)); sizebytes.set(bb.getData(), 0, bb.getLength()); ssize = sizebytes.getLength(); bb.reset(); psserializer.serialize(new IntWritable(pid)); pidbytes.set(bb.getData(), 0, bb.getLength()); psize = pidbytes.getLength(); if ((pidandsize.length - psindex) > (ssize + psize)) { System.arraycopy(sizebytes.getBytes(), 0, pidandsize, psindex, ssize); psindex += ssize; System.arraycopy(pidbytes.getBytes(), 0, pidandsize, psindex, psize); psindex += psize; } else { psWriter.write(pidandsize, 0, psindex); psindex = 0; System.arraycopy(sizebytes.getBytes(), 0, pidandsize, psindex, ssize); psindex += ssize; System.arraycopy(pidbytes.getBytes(), 0, pidandsize, psindex, psize); psindex += psize; } if ((buffer.length - bufindex) > (ksize + vsize)) { System.arraycopy(kbytes.getBytes(), 0, buffer, bufindex, ksize); bufindex += ksize; System.arraycopy(vbytes.getBytes(), 0, buffer, bufindex, vsize); bufindex += vsize; } else if (buffer.length < (ksize + vsize)) { dataWriter.write(buffer, 0, bufindex); bufindex = 0; LOG.info("This is a super record"); dataWriter.write(kbytes.getBytes(), 0, ksize); dataWriter.write(vbytes.getBytes(), 0, vsize); } else { dataWriter.write(buffer, 0, bufindex); bufindex = 0; System.arraycopy(kbytes.getBytes(), 0, buffer, bufindex, ksize); bufindex += ksize; System.arraycopy(vbytes.getBytes(), 0, buffer, bufindex, vsize); bufindex += vsize; } } if (psindex != 0) { psWriter.write(pidandsize, 0, psindex); } if (bufindex != 0) { dataWriter.write(buffer, 0, bufindex); bufindex = 0; } dataWriter.close(); dataWriter = null; psWriter.close(); psWriter = null; buffer = null; pidandsize = null; this.ssrc.setDirFlag(new String[] { "3" }); this.ssrc.setCounter(this.counter); HashMap<Integer, Integer> hashBucketToPartition = this.sssc.loadDataInBalancerBarrier(ssrc, Constants.PARTITION_TYPE.HASH); this.staff.setHashBucketToPartition(hashBucketToPartition); byte[][] databuf = new byte[staffNum][dataBufferSize]; int[] databufindex = new int[staffNum]; try { IntWritable pid = new IntWritable(); IntWritable size = new IntWritable(); int belongPid = 0; while (true) { size.readFields(psReader); pid.readFields(psReader); belongPid = hashBucketToPartition.get(pid.get()); if (belongPid != this.staff.getPartition()) { send++; } else { local++; } if ((databuf[belongPid].length - databufindex[belongPid]) > size.get()) { dataReader.read(databuf[belongPid], databufindex[belongPid], size.get()); databufindex[belongPid] += size.get(); } else if (databuf[belongPid].length < size.get()) { LOG.info("This is a super record"); byte[] tmp = new byte[size.get()]; dataReader.read(tmp, 0, size.get()); if (belongPid == this.staff.getPartition()) { DataInputStream reader = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(tmp))); try { boolean stop = true; while (stop) { Text key = new Text(); key.readFields(reader); Text value = new Text(); value.readFields(reader); if (key.getLength() > 0 && value.getLength() > 0) { Vertex vertex = this.recordParse.recordParse(key.toString(), value.toString()); if (vertex == null) { lost++; continue; } this.staff.getGraphData().addForAll(vertex); } else { stop = false; } } } catch (IOException e) { LOG.info("IO exception: " + e.getStackTrace()); } } else { ThreadSignle t = tpool.getThread(); while (t == null) { t = tpool.getThread(); } t.setWorker( this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), belongPid)); t.setJobId(staff.getJobID()); t.setTaskId(staff.getStaffID()); t.setBelongPartition(belongPid); BytesWritable data = new BytesWritable(); data.set(tmp, 0, size.get()); t.setData(data); LOG.info("Using Thread is: " + t.getThreadNumber()); t.setStatus(true); } tmp = null; } else { if (belongPid == this.staff.getPartition()) { DataInputStream reader = new DataInputStream(new BufferedInputStream( new ByteArrayInputStream(databuf[belongPid], 0, databufindex[belongPid]))); try { boolean stop = true; while (stop) { Text key = new Text(); key.readFields(reader); Text value = new Text(); value.readFields(reader); if (key.getLength() > 0 && value.getLength() > 0) { Vertex vertex = this.recordParse.recordParse(key.toString(), value.toString()); if (vertex == null) { lost++; continue; } this.staff.getGraphData().addForAll(vertex); } else { stop = false; } } } catch (IOException e) { LOG.info("IO exception: " + e.getStackTrace()); } } else { ThreadSignle t = tpool.getThread(); while (t == null) { t = tpool.getThread(); } t.setWorker( this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), belongPid)); t.setJobId(staff.getJobID()); t.setTaskId(staff.getStaffID()); t.setBelongPartition(belongPid); BytesWritable data = new BytesWritable(); data.set(databuf[belongPid], 0, databufindex[belongPid]); t.setData(data); LOG.info("Using Thread is: " + t.getThreadNumber()); t.setStatus(true); } databufindex[belongPid] = 0; dataReader.read(databuf[belongPid], databufindex[belongPid], size.get()); databufindex[belongPid] += size.get(); } } } catch (EOFException ex) { LOG.error("[write]", ex); } for (int i = 0; i < staffNum; i++) { if (databufindex[i] != 0) { if (i == this.staff.getPartition()) { DataInputStream reader = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(databuf[i], 0, databufindex[i]))); try { boolean stop = true; while (stop) { Text key = new Text(); key.readFields(reader); Text value = new Text(); value.readFields(reader); if (key.getLength() > 0 && value.getLength() > 0) { Vertex vertex = this.recordParse.recordParse(key.toString(), value.toString()); if (vertex == null) { lost++; continue; } this.staff.getGraphData().addForAll(vertex); } else { stop = false; } } } catch (IOException e) { LOG.info("IO exception: " + e.getStackTrace()); } } else { ThreadSignle t = tpool.getThread(); while (t == null) { t = tpool.getThread(); } t.setWorker(this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), i)); t.setJobId(staff.getJobID()); t.setTaskId(staff.getStaffID()); t.setBelongPartition(i); BytesWritable data = new BytesWritable(); data.set(databuf[i], 0, databufindex[i]); t.setData(data); LOG.info("Using Thread is: " + t.getThreadNumber()); t.setStatus(true); } } } dataReader.close(); dataReader = null; psReader.close(); psReader = null; for (File f : files) { f.delete(); } dir.delete(); dir = new File(path.substring(0, path.lastIndexOf('/'))); dir.delete(); tpool.cleanup(); tpool = null; databuf = null; databufindex = null; this.counter = null; LOG.info("The number of vertices that were read from the input file: " + headNodeNum); LOG.info("The number of vertices that were put into the partition: " + local); LOG.info("The number of vertices that were sent to other partitions: " + send); LOG.info("The number of verteices in the partition that cound not be " + "parsed:" + lost); } catch (IOException e) { throw e; } catch (InterruptedException e) { throw e; } finally { for (File f : files) { f.delete(); } dir.delete(); dir = new File(path.substring(0, path.lastIndexOf('/'))); dir.delete(); } }
From source file:org.commoncrawl.service.listcrawler.CacheWriterThread.java
@Override public void run() { boolean shutdown = false; while (!shutdown) { try {/*from w w w.ja v a 2 s. com*/ final CacheWriteRequest request = _writeRequestQueue.take(); switch (request._requestType) { case ExitThreadRequest: { // shutdown condition ... CacheManager.LOG.info("Disk Writer Thread Received Shutdown. Exiting!"); shutdown = true; } break; case WriteRequest: { long timeStart = System.currentTimeMillis(); try { // reset crc calculator (single thread so no worries on synchronization) _crc32Out.reset(); // figure out if we need to compress the item ... if ((request._item.getFlags() & CacheItem.Flags.Flag_IsCompressed) == 0 && request._item.getContent().getCount() != 0) { LOG.info("Incoming Cache Request Content for:" + request._item.getUrl() + " is not compressed. Compressing..."); ByteStream compressedBytesOut = new ByteStream(request._item.getContent().getCount()); ThriftyGZIPOutputStream gzipOutputStream = new ThriftyGZIPOutputStream( compressedBytesOut); gzipOutputStream.write(request._item.getContent().getReadOnlyBytes(), 0, request._item.getContent().getCount()); gzipOutputStream.finish(); LOG.info("Finished Compressing Incoming Content for:" + request._item.getUrl() + " BytesIn:" + request._item.getContent().getCount() + " BytesOut:" + compressedBytesOut.size()); // replace buffer request._item.setContent( new FlexBuffer(compressedBytesOut.getBuffer(), 0, compressedBytesOut.size())); request._item.setFlags((request._item.getFlags() | CacheItem.Flags.Flag_IsCompressed)); } // create streams ... ByteStream bufferOutputStream = new ByteStream(8192); CheckedOutputStream checkedStream = new CheckedOutputStream(bufferOutputStream, _crc32Out); DataOutputStream dataOutputStream = new DataOutputStream(checkedStream); // remember if this item has content ... boolean hasContent = request._item.isFieldDirty(CacheItem.Field_CONTENT); // now mark the content field as clean, so that it will not be serialized in our current serialization attempt ... request._item.setFieldClean(CacheItem.Field_CONTENT); // and go ahead and write out the data to the intermediate buffer while also computing partial checksum request._item.write(dataOutputStream); request._item.setFieldDirty(CacheItem.Field_CONTENT); // ok, now ... write out file header ... CacheItemHeader itemHeader = new CacheItemHeader(_manager.getLocalLogSyncBytes()); itemHeader._status = CacheItemHeader.STATUS_ALIVE; itemHeader._lastAccessTime = System.currentTimeMillis(); itemHeader._fingerprint = request._itemFingerprint; // compute total length ... // first the header bytes in the cacheItem itemHeader._dataLength = bufferOutputStream.size(); // next the content length (encoded - as in size + bytes) ... itemHeader._dataLength += 4 + request._item.getContent().getCount(); // lastly the crc value iteself ... itemHeader._dataLength += 8; // open the log file ... DataOutputBuffer logStream = new DataOutputBuffer(); // ok, go ahead and write the header itemHeader.writeHeader(logStream); // ok now write out the item data minus content... logStream.write(bufferOutputStream.getBuffer(), 0, bufferOutputStream.size()); // now create a checked stream for the content ... CheckedOutputStream checkedStream2 = new CheckedOutputStream(logStream, checkedStream.getChecksum()); dataOutputStream = new DataOutputStream(checkedStream2); // content size dataOutputStream.writeInt(request._item.getContent().getCount()); // now write out the content (via checked stream so that we can calc checksum on content) dataOutputStream.write(request._item.getContent().getReadOnlyBytes(), 0, request._item.getContent().getCount()); // ok ... lastly write out the checksum bytes ... dataOutputStream.writeLong(checkedStream2.getChecksum().getValue()); // and FINALLY, write out the total item bytes (so that we can seek in reverse to read last request log logStream.writeInt(CacheItemHeader.SIZE + itemHeader._dataLength); // ok flush everyting to the memory stream dataOutputStream.flush(); //ok - time to acquire the log semaphore //LOG.info("Acquiring Local Log Semaphore"); _manager.getLocalLogAccessSemaphore().acquireUninterruptibly(); try { // now time to acquire the write semaphore ... _manager.getLocalLogWriteAccessSemaphore().acquireUninterruptibly(); // get the current file position long recordOffset = _manager.getLocalLogFilePos(); try { long ioTimeStart = System.currentTimeMillis(); RandomAccessFile logFile = new RandomAccessFile(_manager.getActiveLogFilePath(), "rw"); try { // seek to our known record offset logFile.seek(recordOffset); // write out the data logFile.write(logStream.getData(), 0, logStream.getLength()); } finally { logFile.close(); } // now we need to update the file header _manager.updateLogFileHeader(_manager.getActiveLogFilePath(), 1, CacheItemHeader.SIZE + itemHeader._dataLength + 4 /*trailing bytes*/); CacheManager.LOG .info("#### Wrote Cache Item in:" + (System.currentTimeMillis() - timeStart) + " iotime:" + (System.currentTimeMillis() - ioTimeStart) + " QueueSize:" + _writeRequestQueue.size()); } finally { // release write semaphore quickly _manager.getLocalLogWriteAccessSemaphore().release(); } // now inform the manager of the completed request ... _manager.writeRequestComplete(request, recordOffset); } finally { //LOG.info("Releasing Local Log Semaphore"); _manager.getLocalLogAccessSemaphore().release(); } } catch (IOException e) { CacheManager.LOG.error("### FUC# BATMAN! - GONNA LOSE THIS REQUEST!!!!:" + CCStringUtils.stringifyException(e)); _manager.writeRequestFailed(request, e); } } break; } } catch (InterruptedException e) { } } }
From source file:com.example.rartonne.appftur.HomeActivity.java
public int uploadFile(String sourceFileUri) { String fileName = sourceFileUri; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(sourceFileUri); if (!sourceFile.isFile()) { //dialog.dismiss(); Log.e("uploadFile", "Source File not exist"); runOnUiThread(new Runnable() { public void run() { //messageText.setText("Source File not exist :"+uploadFilePath + "" + uploadFileName); }/*from w ww . j a v a2 s. c o m*/ }); return 0; } else { try { // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); if (serverResponseCode == 200) { runOnUiThread(new Runnable() { public void run() { /*String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" +" http://www.androidexample.com/media/uploads/" +uploadFileName;*/ //messageText.setText(msg); /*Toast.makeText(getApplicationContext(), "File Upload Complete.", Toast.LENGTH_SHORT).show();*/ } }); } //close the streams // fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { //dialog.dismiss(); ex.printStackTrace(); runOnUiThread(new Runnable() { public void run() { //messageText.setText("MalformedURLException Exception : check script url."); /*Toast.makeText(getApplicationContext(), "MalformedURLException", Toast.LENGTH_SHORT).show();*/ } }); Log.e("Upload file to server", "error: " + ex.getMessage(), ex); } catch (Exception e) { //dialog.dismiss(); e.printStackTrace(); runOnUiThread(new Runnable() { public void run() { //messageText.setText("Got Exception : see logcat "); /*Toast.makeText(getApplicationContext(), "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();*/ } }); Log.e("Upload file to server", "Exception : " + e.getMessage(), e); } // dialog.dismiss(); return serverResponseCode; } // End else block }
From source file:com.roamprocess1.roaming4world.syncadapter.SyncAdapter.java
public int uploadFile(String sourceFileUri) { String upLoadServerUri = ""; upLoadServerUri = "http://ip.roaming4world.com/esstel/fetch_contacts_upload.php"; String fileName = sourceFileUri; Log.d("file upload url", upLoadServerUri); HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(sourceFileUri); if (!sourceFile.isFile()) { Log.d("uploadFile", "Source File Does not exist"); return 0; }//from w w w. jav a 2 s. c o m int serverResponseCode = 0; try { // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + selfNumber + "-" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); // create a buffer of maximum size bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.d("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); //close the streams // fileInputStream.close(); dos.flush(); dos.close(); Log.d("Contact file ", "uploaded"); } catch (MalformedURLException ex) { ex.printStackTrace(); Log.e("Upload file to server", "error: " + ex.getMessage(), ex); } catch (Exception e) { e.printStackTrace(); Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); } return serverResponseCode; }
From source file:MyZone.Settings.java
public boolean retrieveCert() { while (true) { try {/*w w w. ja v a 2 s.co m*/ Socket toCAServer = new Socket(globalProperties.caAddress, globalProperties.caPort); toCAServer.setSoTimeout(IDLE_LIMIT); byte[] usernameBytes = username.getBytes("UTF-8"); DataOutputStream outToCAServer = new DataOutputStream(toCAServer.getOutputStream()); byte[] buffer = new byte[usernameBytes.length + 4]; int len = 0; System.arraycopy(intToByteArray(usernameBytes.length), 0, buffer, len, 4); len += 4; System.arraycopy(usernameBytes, 0, buffer, len, usernameBytes.length); outToCAServer.write(buffer, 0, buffer.length); outToCAServer.flush(); int i = 0; byte[] lenBytes = new byte[4]; while (i < 4) { i += toCAServer.getInputStream().read(lenBytes, i, 4 - i); if (i < 0) { if (DEBUG) { System.out.println("DEBUG: line 2529 of Settings.java"); } } try { Thread.sleep(100); } catch (Exception e) { if (DEBUG) { e.printStackTrace(); } } } len = byteArrayToInt(lenBytes); byte[] certBytes = new byte[len]; i = 0; while (i < len) { i += toCAServer.getInputStream().read(certBytes, i, len - i); if (i < 0) { if (DEBUG) { System.out.println("DEBUG: line 2555 of Settings.java"); } len = 4; break; } try { Thread.sleep(100); } catch (Exception e) { if (DEBUG) { e.printStackTrace(); } } } if (len == 4) { Thread.sleep(30000); outToCAServer.close(); toCAServer.close(); continue; } outToCAServer.close(); toCAServer.close(); globalProperties.caCertPath = prefix + "/CAs/"; CertVerifier y = new CertVerifier(); userCertificate uc = y.verifyCertificate(certBytes, globalProperties.caCertPath, globalProperties.keyPairAlgorithm, globalProperties.certSigAlgorithm); if (uc == null) { Thread.sleep(30000); continue; } File file = new File(prefix + username + "/cert/" + username + ".cert"); FileOutputStream fos = new FileOutputStream(file); fos.write(certBytes); fos.flush(); fos.close(); outToCAServer.close(); toCAServer.close(); return true; } catch (Exception e) { if (DEBUG) e.printStackTrace(); } } }
From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java
private void formatEditScript(Delta delta, DataOutputStream outStream, CompareLineInfo[] fileA) throws QVCSOperationException { try {/*w ww .j ava 2s . co m*/ short editType; int seekPosition = -1; int deletedByteCount = 0; int insertedByteCount = 0; byte[] secondFileByteBuffer = null; if (delta instanceof ChangeDelta) { CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()]; seekPosition = originalStartingLine.getLineSeekPosition(); editType = CompareFilesEditInformation.QVCS_EDIT_REPLACE; deletedByteCount = computeDeletedByteCount(delta); insertedByteCount = computeInsertedByteCount(delta); secondFileByteBuffer = computeSecondFileByteBufferForInsert(delta, insertedByteCount); } else if (delta instanceof AddDelta) { int anchor = delta.getOriginal().anchor(); if (anchor == 0) { CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()]; seekPosition = originalStartingLine.getLineSeekPosition(); } else { CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor() - 1]; byte[] lineAsByteArray = originalStartingLine.getLineString().getBytes(UTF8); seekPosition = originalStartingLine.getLineSeekPosition() + lineAsByteArray.length; } editType = CompareFilesEditInformation.QVCS_EDIT_INSERT; insertedByteCount = computeInsertedByteCount(delta); AddDelta insertDelta = (AddDelta) delta; secondFileByteBuffer = computeSecondFileByteBufferForInsert(insertDelta, insertedByteCount); } else if (delta instanceof DeleteDelta) { CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()]; seekPosition = originalStartingLine.getLineSeekPosition(); editType = CompareFilesEditInformation.QVCS_EDIT_DELETE; deletedByteCount = computeDeletedByteCount(delta); } else { throw new QVCSOperationException("Internal error -- invalid edit type"); } CompareFilesEditInformation editInfo = new CompareFilesEditInformation(editType, seekPosition, deletedByteCount, insertedByteCount); switch (editType) { case CompareFilesEditInformation.QVCS_EDIT_DELETE: editInfo.write(outStream); break; case CompareFilesEditInformation.QVCS_EDIT_INSERT: case CompareFilesEditInformation.QVCS_EDIT_REPLACE: editInfo.write(outStream); outStream.write(secondFileByteBuffer, 0, insertedByteCount); break; default: throw new QVCSOperationException("Internal error -- invalid edit type"); } } catch (IOException e) { throw new QVCSOperationException("IOException in formatEditScript() " + e.getLocalizedMessage()); } }
From source file:com.github.hrpc.rpc.Server.java
/** * Setup response for the IPC Call./*from ww w. jav a 2s .c o m*/ * * @param responseBuf buffer to serialize the response into * @param call {@link Call} to which we are setting up the response * @param status of the IPC call * @param rv return value for the IPC Call, if the call was successful * @param errorClass error class, if the the call failed * @param error error message, if the call failed * @throws IOException */ private void setupResponse(ByteArrayOutputStream responseBuf, Call call, RpcStatusProto status, RpcErrorCodeProto erCode, Writable rv, String errorClass, String error) throws IOException { responseBuf.reset(); DataOutputStream out = new DataOutputStream(responseBuf); RpcResponseHeaderProto.Builder headerBuilder = RpcResponseHeaderProto.newBuilder(); headerBuilder.setClientId(ByteString.copyFrom(call.clientId)); headerBuilder.setCallId(call.callId); headerBuilder.setRetryCount(call.retryCount); headerBuilder.setStatus(status); headerBuilder.setServerIpcVersionNum(CURRENT_VERSION); if (status == RpcStatusProto.SUCCESS) { RpcResponseHeaderProto header = headerBuilder.build(); final int headerLen = header.getSerializedSize(); int fullLength = CodedOutputStream.computeRawVarint32Size(headerLen) + headerLen; try { if (rv instanceof ProtobufRpcEngine.RpcWrapper) { ProtobufRpcEngine.RpcWrapper resWrapper = (ProtobufRpcEngine.RpcWrapper) rv; fullLength += resWrapper.getLength(); out.writeInt(fullLength); header.writeDelimitedTo(out); rv.write(out); } else { // Have to serialize to buffer to get len final DataOutputBuffer buf = new DataOutputBuffer(); rv.write(buf); byte[] data = buf.getData(); fullLength += buf.getLength(); out.writeInt(fullLength); header.writeDelimitedTo(out); out.write(data, 0, buf.getLength()); } } catch (Throwable t) { LOG.warn("Error serializing call response for call " + call, t); // Call back to same function - this is OK since the // buffer is reset at the top, and since status is changed // to ERROR it won't infinite loop. setupResponse(responseBuf, call, RpcStatusProto.ERROR, RpcErrorCodeProto.ERROR_SERIALIZING_RESPONSE, null, t.getClass().getName(), StringUtils.stringifyException(t)); return; } } else { // Rpc Failure headerBuilder.setExceptionClassName(errorClass); headerBuilder.setErrorMsg(error); headerBuilder.setErrorDetail(erCode); RpcResponseHeaderProto header = headerBuilder.build(); int headerLen = header.getSerializedSize(); final int fullLength = CodedOutputStream.computeRawVarint32Size(headerLen) + headerLen; out.writeInt(fullLength); header.writeDelimitedTo(out); } call.setResponse(ByteBuffer.wrap(responseBuf.toByteArray())); }