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
/** * Handle the CommitRequest message/*from www .j av a 2s .c o m*/ */ private void handle(CommitRequest msg, ChannelHandlerContext ctx) { CommitResponse reply = new CommitResponse(msg.startTimestamp); DataOutputStream toWAL = sharedState.toWAL; synchronized (sharedState) { // 0. check if it should abort if (msg.startTimestamp < timestampOracle.first()) { reply.committed = false; LOG.warn("Aborting transaction after restarting TSO"); } else if (msg.rows.length > 0 && msg.startTimestamp < sharedState.largestDeletedTimestamp) { // Too old and not read only reply.committed = false;// set as abort LOG.warn("Too old startTimestamp: ST " + msg.startTimestamp + " MAX " + sharedState.largestDeletedTimestamp); } else { // 1. check the write-write conflicts for (RowKey r : msg.rows) { long value = sharedState.hashmap.getLatestWriteForRow(r.hashCode()); if (value != 0 && value > msg.startTimestamp) { reply.committed = false;// set as abort break; } } } if (reply.committed) { // 2. commit try { long commitTimestamp = timestampOracle.next(toWAL); sharedState.uncommited.commit(commitTimestamp); sharedState.uncommited.commit(msg.startTimestamp); reply.commitTimestamp = commitTimestamp; if (msg.rows.length > 0) { if (LOG.isTraceEnabled()) { LOG.trace("Adding commit to WAL"); } toWAL.writeByte(LoggerProtocol.COMMIT); toWAL.writeLong(msg.startTimestamp); toWAL.writeLong(commitTimestamp); long oldLargestDeletedTimestamp = sharedState.largestDeletedTimestamp; for (RowKey r : msg.rows) { sharedState.hashmap.putLatestWriteForRow(r.hashCode(), commitTimestamp); } sharedState.largestDeletedTimestamp = sharedState.hashmap.getLargestDeletedTimestamp(); sharedState.processCommit(msg.startTimestamp, commitTimestamp); if (sharedState.largestDeletedTimestamp > oldLargestDeletedTimestamp) { toWAL.writeByte(LoggerProtocol.LARGEST_DELETED_TIMESTAMP); toWAL.writeLong(sharedState.largestDeletedTimestamp); Set<Long> toAbort = sharedState.uncommited .raiseLargestDeletedTransaction(sharedState.largestDeletedTimestamp); if (LOG.isWarnEnabled() && !toAbort.isEmpty()) { LOG.warn("Slow transactions after raising max: " + toAbort.size()); } synchronized (sharedMsgBufLock) { for (Long id : toAbort) { sharedState.hashmap.setHalfAborted(id); queueHalfAbort(id); } queueLargestDeletedTimestamp(sharedState.largestDeletedTimestamp); } } if (sharedState.largestDeletedTimestamp > sharedState.previousLargestDeletedTimestamp + TSOState.MAX_ITEMS) { // schedule snapshot executor.submit(createAbortedSnaphostTask); sharedState.previousLargestDeletedTimestamp = sharedState.largestDeletedTimestamp; } synchronized (sharedMsgBufLock) { queueCommit(msg.startTimestamp, commitTimestamp); } } } catch (IOException e) { LOG.error("failed to handle CommitRequest", e); } } else { // add it to the aborted list handleHalfAbort(msg.startTimestamp); } commitCounter.incrementAndGet(); nextBatch.add(new ChannelAndMessage(ctx, reply)); if (sharedState.baos.size() >= batchSize) { if (LOG.isTraceEnabled()) { LOG.trace("Going to add record of size " + sharedState.baos.size()); } addRecord(); } } }
From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java
public void accept(OutputStream out, InputStream in, byte epType, int qSize, CommunicationMode communicationMode, Principal principal) throws IOException { DataOutputStream dos = new DataOutputStream(out); DataInputStream dis;// www . ja v a2s.c o m if (clientVersion.compareTo(Version.CURRENT) < 0) { dis = new VersionedDataInputStream(in, clientVersion); dos = new VersionedDataOutputStream(dos, clientVersion); } else { dis = new DataInputStream(in); } // Write ok reply if (communicationMode.isWAN() && principal != null) { dos.writeByte(REPLY_WAN_CREDENTIALS); } else { dos.writeByte(REPLY_OK);// byte 59 } // additional byte of wan site needs to send for Gateway BC if (communicationMode.isWAN()) { Version.writeOrdinal(dos, ServerHandShakeProcessor.currentServerVersion.ordinal(), true); } dos.writeByte(epType); dos.writeInt(qSize); // Write the server's member DistributedMember member = this.system.getDistributedMember(); ServerHandShakeProcessor.writeServerMember(member, dos); // Write no message dos.writeUTF(""); // Write delta-propagation property value if this is not WAN. if (!communicationMode.isWAN() && this.clientVersion.compareTo(Version.GFE_61) >= 0) { dos.writeBoolean(((InternalDistributedSystem) this.system).getConfig().getDeltaPropagation()); } // Neeraj: Now if the communication mode is GATEWAY_TO_GATEWAY // and principal not equal to null then send the credentials also if (communicationMode.isWAN() && principal != null) { sendCredentialsForWan(dos, dis); } // Write the distributed system id if this is a 6.6 or greater client // on the remote side of the gateway if (communicationMode.isWAN() && this.clientVersion.compareTo(Version.GFE_66) >= 0 && ServerHandShakeProcessor.currentServerVersion.compareTo(Version.GFE_66) >= 0) { dos.writeByte( ((InternalDistributedSystem) this.system).getDistributionManager().getDistributedSystemId()); } if ((communicationMode.isWAN()) && this.clientVersion.compareTo(Version.GFE_80) >= 0 && ServerHandShakeProcessor.currentServerVersion.compareTo(Version.GFE_80) >= 0) { int pdxSize = PeerTypeRegistration.getPdxRegistrySize(); dos.writeInt(pdxSize); } // Flush dos.flush(); }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> mediaSerialize(Cursor cursor) { logger.debug("::mediaSerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { MediaWrapper iw = new MediaWrapper(); iw.setEventId(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.EVENT_ID))); iw.setDataType(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA_TYPE))); iw.setData(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA))); iw.setCreatedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.CREATED_DATE))); iw.setModifiedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.MODIFIED_DATE))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(MediaTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try {//from w w w .j a v a2 s . co m eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :event id eventId event_id\n try { String fileName = iw.getData(); File dataFile = new File(fileName); int dataSize = (int) dataFile.length(); byte[] buffData = new byte[dataSize]; FileInputStream fileStream = new FileInputStream(dataFile); int ret = 0; for (int position = 0; (ret > -1 && dataSize > position); position += ret) { ret = fileStream.read(buffData, position, dataSize - position); } fileStream.close(); eos.writeBytes("data"); eos.writeByte(0); ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN); dataSizeBuf.putInt(dataSize); // write the media back out eos.write(dataSizeBuf.array()); eos.write(buffData); eos.write(dataSizeBuf.array()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // not a reference field name :created date createdDate created_date\n // not a reference field name :modified date modifiedDate modified_date\n // MediaTableSchemaBase._DISPOSITION; // try { // TODO write to content provider using openFile // if (!applCacheMediaDir.exists() ) applCacheMediaDir.mkdirs(); // File outfile = new File(applCacheMediaDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> categorySerialize(Cursor cursor) { logger.debug("::categorySerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { CategoryWrapper iw = new CategoryWrapper(); iw.setMainCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.MAIN_CATEGORY))); iw.setSubCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.SUB_CATEGORY))); iw.setTigrId(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.TIGR_ID))); iw.setIconType(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON_TYPE))); iw.setIcon(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(CategoryTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(CategoryTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try {/*from w w w . java2 s. c om*/ eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :main category mainCategory main_category\n // not a reference field name :sub category subCategory sub_category\n // not a reference field name :tigr id tigrId tigr_id\n try { String fileName = iw.getIcon(); File dataFile = new File(fileName); int dataSize = (int) dataFile.length(); byte[] buffData = new byte[dataSize]; FileInputStream fileStream = new FileInputStream(dataFile); int ret = 0; for (int position = 0; (ret > -1 && dataSize > position); position += ret) { ret = fileStream.read(buffData, position, dataSize - position); } fileStream.close(); eos.writeBytes("icon"); eos.writeByte(0); ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN); dataSizeBuf.putInt(dataSize); // write the category back out eos.write(dataSizeBuf.array()); eos.write(buffData); eos.write(dataSizeBuf.array()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // CategoryTableSchemaBase._DISPOSITION; // try { // if (!applCacheCategoryDir.exists() ) applCacheCategoryDir.mkdirs(); // // File outfile = new File(applCacheCategoryDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // // paths.add(outfile); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:org.openxdata.server.FormsServer.java
/** * Called when a new connection has been received. Failures are not handled * in this class as different servers (BT,SMS, etc) may want to handle them * differently./*w w w.j a va 2s .c om*/ * * @param dis - the stream to read from. * @param dos - the stream to write to. */ public void processConnection(InputStream disParam, OutputStream dosParam) { ZOutputStream gzip = new ZOutputStream(dosParam, JZlib.Z_BEST_COMPRESSION); DataOutputStream zdos = new DataOutputStream(gzip); byte responseStatus = ResponseStatus.STATUS_ERROR; try { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataInputStream dis = new DataInputStream(disParam); String name = dis.readUTF(); String password = dis.readUTF(); String serializer = dis.readUTF(); String locale = dis.readUTF(); byte action = dis.readByte(); User user = authenticationService.authenticate(name, password); if (user == null) responseStatus = ResponseStatus.STATUS_ACCESS_DENIED; else { DataOutputStream dosTemp = new DataOutputStream(baos); if (action == ACTION_DOWNLOAD_FORMS) formDownloadService.downloadForms(dosTemp, serializer, locale); else if (action == ACTION_UPLOAD_DATA) submitXforms(dis, dosTemp, serializer); else if (action == ACTION_DOWNLOAD_USERS) formDownloadService.downloadUsers(dosTemp, serializer); else if (action == ACTION_DOWNLOAD_USERS_AND_FORMS) downloadUsersAndForms(dis.readInt(), dosTemp, serializer, locale); else if (action == ACTION_DOWNLOAD_STUDY_LIST) formDownloadService.downloadStudies(dosTemp, serializer, locale); else if (action == ACTION_DOWNLOAD_LANGUAGES) formDownloadService.downloadLocales(dis, dosTemp, serializer); else if (action == ACTION_DOWNLOAD_MENU_TEXT) formDownloadService.downloadMenuText(dis, dosTemp, serializer, locale); else if (action == ACTION_DOWNLOAD_STUDY_FORMS) formDownloadService.downloadForms(dis.readInt(), zdos, serializer, locale); else if (action == ACTION_DOWNLOAD_USERS_AND_ALL_FORMS) downloadUsersAndAllForms(dosTemp, serializer, locale); responseStatus = ResponseStatus.STATUS_SUCCESS; } zdos.writeByte(responseStatus); if (responseStatus == ResponseStatus.STATUS_SUCCESS) { zdos.write(baos.toByteArray()); } } catch (Exception ex) { log.error(ex.getMessage(), ex); zdos.writeByte(responseStatus); } finally { zdos.flush(); gzip.finish(); } } catch (IOException e) { // this is for exceptions occurring in the catch or finally clauses. log.error(e.getMessage(), e); } }
From source file:com.yahoo.omid.tso.TSOHandler.java
/** * Handle the CommitRequest message/*from ww w . j a v a 2 s. co m*/ */ public void handle(CommitRequest msg, ChannelHandlerContext ctx) { CommitResponse reply = new CommitResponse(msg.startTimestamp); ByteArrayOutputStream baos = sharedState.baos; DataOutputStream toWAL = sharedState.toWAL; synchronized (sharedState) { // 0. check if it should abort if (msg.startTimestamp < timestampOracle.first()) { reply.committed = false; LOG.warn("Aborting transaction after restarting TSO"); } else if (msg.rows.length > 0 && msg.startTimestamp < sharedState.largestDeletedTimestamp) { // Too old and not read only reply.committed = false;// set as abort LOG.warn("Too old starttimestamp: ST " + msg.startTimestamp + " MAX " + sharedState.largestDeletedTimestamp); } else { // 1. check the write-write conflicts for (RowKey r : msg.rows) { long value; value = sharedState.hashmap.getLatestWriteForRow(r.hashCode()); if (value != 0 && value > msg.startTimestamp) { reply.committed = false;// set as abort break; } else if (value == 0 && sharedState.largestDeletedTimestamp > msg.startTimestamp) { // then it could have been committed after start // timestamp but deleted by recycling LOG.warn("Old transaction {Start timestamp " + msg.startTimestamp + "} {Largest deleted timestamp " + sharedState.largestDeletedTimestamp + "}"); reply.committed = false;// set as abort break; } } } if (reply.committed) { // 2. commit try { long commitTimestamp = timestampOracle.next(toWAL); sharedState.uncommited.commit(commitTimestamp); sharedState.uncommited.commit(msg.startTimestamp); reply.commitTimestamp = commitTimestamp; if (msg.rows.length > 0) { if (LOG.isTraceEnabled()) { LOG.trace("Adding commit to WAL"); } toWAL.writeByte(LoggerProtocol.COMMIT); toWAL.writeLong(msg.startTimestamp); toWAL.writeLong(commitTimestamp); long oldLargestDeletedTimestamp = sharedState.largestDeletedTimestamp; for (RowKey r : msg.rows) { sharedState.hashmap.putLatestWriteForRow(r.hashCode(), commitTimestamp); } sharedState.largestDeletedTimestamp = sharedState.hashmap.getLargestDeletedTimestamp(); sharedState.processCommit(msg.startTimestamp, commitTimestamp); if (sharedState.largestDeletedTimestamp > oldLargestDeletedTimestamp) { toWAL.writeByte(LoggerProtocol.LARGESTDELETEDTIMESTAMP); toWAL.writeLong(sharedState.largestDeletedTimestamp); Set<Long> toAbort = sharedState.uncommited .raiseLargestDeletedTransaction(sharedState.largestDeletedTimestamp); if (LOG.isWarnEnabled() && !toAbort.isEmpty()) { LOG.warn("Slow transactions after raising max: " + toAbort.size()); } synchronized (sharedMsgBufLock) { for (Long id : toAbort) { sharedState.hashmap.setHalfAborted(id); queueHalfAbort(id); } queueLargestIncrease(sharedState.largestDeletedTimestamp); } } if (sharedState.largestDeletedTimestamp > sharedState.previousLargestDeletedTimestamp + TSOState.MAX_ITEMS) { // schedule snapshot executor.submit(createAbortedSnaphostTask); sharedState.previousLargestDeletedTimestamp = sharedState.largestDeletedTimestamp; } synchronized (sharedMsgBufLock) { queueCommit(msg.startTimestamp, commitTimestamp); } } } catch (IOException e) { e.printStackTrace(); } } else { // add it to the aborted list abortCount++; try { toWAL.writeByte(LoggerProtocol.ABORT); toWAL.writeLong(msg.startTimestamp); } catch (IOException e) { e.printStackTrace(); } sharedState.processAbort(msg.startTimestamp); synchronized (sharedMsgBufLock) { queueHalfAbort(msg.startTimestamp); } } TSOHandler.transferredBytes.incrementAndGet(); ChannelandMessage cam = new ChannelandMessage(ctx, reply); sharedState.nextBatch.add(cam); if (sharedState.baos.size() >= TSOState.BATCH_SIZE) { if (LOG.isTraceEnabled()) { LOG.trace("Going to add record of size " + sharedState.baos.size()); } // sharedState.lh.asyncAddEntry(baos.toByteArray(), this, // sharedState.nextBatch); sharedState.addRecord(baos.toByteArray(), new AddRecordCallback() { @Override public void addRecordComplete(int rc, Object ctx) { if (rc != Code.OK) { LOG.warn("Write failed: " + LoggerException.getMessage(rc)); } else { synchronized (callbackLock) { @SuppressWarnings("unchecked") ArrayList<ChannelandMessage> theBatch = (ArrayList<ChannelandMessage>) ctx; for (ChannelandMessage cam : theBatch) { Channels.write(cam.ctx, Channels.succeededFuture(cam.ctx.getChannel()), cam.msg); } } } } }, sharedState.nextBatch); sharedState.nextBatch = new ArrayList<ChannelandMessage>(sharedState.nextBatch.size() + 5); sharedState.baos.reset(); } } }
From source file:ml.shifu.shifu.core.dtrain.dt.BinaryDTSerializer.java
public static void save(ModelConfig modelConfig, List<ColumnConfig> columnConfigList, List<List<TreeNode>> baggingTrees, String loss, int inputCount, OutputStream output) throws IOException { DataOutputStream fos = null; try {//from w w w .j av a 2s . c o m fos = new DataOutputStream(new GZIPOutputStream(output)); // version fos.writeInt(CommonConstants.TREE_FORMAT_VERSION); fos.writeUTF(modelConfig.getAlgorithm()); fos.writeUTF(loss); fos.writeBoolean(modelConfig.isClassification()); fos.writeBoolean(modelConfig.getTrain().isOneVsAll()); fos.writeInt(inputCount); Map<Integer, String> columnIndexNameMapping = new HashMap<Integer, String>(); Map<Integer, List<String>> columnIndexCategoricalListMapping = new HashMap<Integer, List<String>>(); Map<Integer, Double> numericalMeanMapping = new HashMap<Integer, Double>(); for (ColumnConfig columnConfig : columnConfigList) { if (columnConfig.isFinalSelect()) { columnIndexNameMapping.put(columnConfig.getColumnNum(), columnConfig.getColumnName()); } if (columnConfig.isCategorical() && CollectionUtils.isNotEmpty(columnConfig.getBinCategory())) { columnIndexCategoricalListMapping.put(columnConfig.getColumnNum(), columnConfig.getBinCategory()); } if (columnConfig.isNumerical() && columnConfig.getMean() != null) { numericalMeanMapping.put(columnConfig.getColumnNum(), columnConfig.getMean()); } } if (columnIndexNameMapping.size() == 0) { boolean hasCandidates = CommonUtils.hasCandidateColumns(columnConfigList); for (ColumnConfig columnConfig : columnConfigList) { if (CommonUtils.isGoodCandidate(columnConfig, hasCandidates)) { columnIndexNameMapping.put(columnConfig.getColumnNum(), columnConfig.getColumnName()); } } } // serialize numericalMeanMapping fos.writeInt(numericalMeanMapping.size()); for (Entry<Integer, Double> entry : numericalMeanMapping.entrySet()) { fos.writeInt(entry.getKey()); // for some feature, it is null mean value, it is not selected, just set to 0d to avoid NPE fos.writeDouble(entry.getValue() == null ? 0d : entry.getValue()); } // serialize columnIndexNameMapping fos.writeInt(columnIndexNameMapping.size()); for (Entry<Integer, String> entry : columnIndexNameMapping.entrySet()) { fos.writeInt(entry.getKey()); fos.writeUTF(entry.getValue()); } // serialize columnIndexCategoricalListMapping fos.writeInt(columnIndexCategoricalListMapping.size()); for (Entry<Integer, List<String>> entry : columnIndexCategoricalListMapping.entrySet()) { List<String> categories = entry.getValue(); if (categories != null) { fos.writeInt(entry.getKey()); fos.writeInt(categories.size()); for (String category : categories) { // There is 16k limitation when using writeUTF() function. // if the category value is larger than 10k, write a marker -1 and write bytes instead of // writeUTF; // in read part logic should be changed also to readByte not readUTF according to the marker if (category.length() < Constants.MAX_CATEGORICAL_VAL_LEN) { fos.writeUTF(category); } else { fos.writeShort(UTF_BYTES_MARKER); // marker here byte[] bytes = category.getBytes("UTF-8"); fos.writeInt(bytes.length); for (int i = 0; i < bytes.length; i++) { fos.writeByte(bytes[i]); } } } } } Map<Integer, Integer> columnMapping = getColumnMapping(columnConfigList); fos.writeInt(columnMapping.size()); for (Entry<Integer, Integer> entry : columnMapping.entrySet()) { fos.writeInt(entry.getKey()); fos.writeInt(entry.getValue()); } // after model version 4 (>=4), IndependentTreeModel support bagging, here write a default RF/GBT size 1 fos.writeInt(baggingTrees.size()); for (int i = 0; i < baggingTrees.size(); i++) { List<TreeNode> trees = baggingTrees.get(i); int treeLength = trees.size(); fos.writeInt(treeLength); for (TreeNode treeNode : trees) { treeNode.write(fos); } } } catch (IOException e) { LOG.error("Error in writing output.", e); } finally { IOUtils.closeStream(fos); } }
From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java
public static Properties readCredentials(DataInputStream dis, DataOutputStream dos, DistributedSystem system, SecurityService securityService) throws GemFireSecurityException, IOException { boolean requireAuthentication = securityService.isClientSecurityRequired(); Properties credentials = null; try {/* w w w .j av a2 s .c o m*/ byte secureMode = dis.readByte(); throwIfMissingRequiredCredentials(requireAuthentication, secureMode != CREDENTIALS_NONE); if (secureMode == CREDENTIALS_NORMAL) { if (requireAuthentication) { credentials = DataSerializer.readProperties(dis); } else { DataSerializer.readProperties(dis); // ignore the credentials } } else if (secureMode == CREDENTIALS_DHENCRYPT) { boolean sendAuthentication = dis.readBoolean(); InternalLogWriter securityLogWriter = (InternalLogWriter) system.getSecurityLogWriter(); // Get the symmetric encryption algorithm to be used String skAlgo = DataSerializer.readString(dis); // Get the public key of the other side byte[] keyBytes = DataSerializer.readByteArray(dis); byte[] challenge = null; PublicKey pubKey = null; if (requireAuthentication) { // Generate PublicKey from encoded form X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFact = KeyFactory.getInstance("DH"); pubKey = keyFact.generatePublic(x509KeySpec); // Send the public key to other side keyBytes = dhPublicKey.getEncoded(); challenge = new byte[64]; random.nextBytes(challenge); // If the server has to also authenticate itself then // sign the challenge from client. if (sendAuthentication) { // Get the challenge string from client byte[] clientChallenge = DataSerializer.readByteArray(dis); if (privateKeyEncrypt == null) { throw new AuthenticationFailedException( LocalizedStrings.HandShake_SERVER_PRIVATE_KEY_NOT_AVAILABLE_FOR_CREATING_SIGNATURE .toLocalizedString()); } // Sign the challenge from client and send it to the client Signature sig = Signature.getInstance(privateKeySignAlgo); sig.initSign(privateKeyEncrypt); sig.update(clientChallenge); byte[] signedBytes = sig.sign(); dos.writeByte(REPLY_OK); DataSerializer.writeByteArray(keyBytes, dos); // DataSerializer.writeString(privateKeyAlias, dos); DataSerializer.writeString(privateKeySubject, dos); DataSerializer.writeByteArray(signedBytes, dos); securityLogWriter.fine("HandShake: sent the signed client challenge"); } else { // These two lines should not be moved before the if{} statement in // a common block for both if...then...else parts. This is to handle // the case when an AuthenticationFailedException is thrown by the // if...then part when sending the signature. dos.writeByte(REPLY_OK); DataSerializer.writeByteArray(keyBytes, dos); } // Now send the server challenge DataSerializer.writeByteArray(challenge, dos); securityLogWriter.fine("HandShake: sent the public key and challenge"); dos.flush(); // Read and decrypt the credentials byte[] encBytes = DataSerializer.readByteArray(dis); KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(dhPrivateKey); ka.doPhase(pubKey, true); Cipher decrypt; int keysize = getKeySize(skAlgo); int blocksize = getBlockSize(skAlgo); if (keysize == -1 || blocksize == -1) { SecretKey sKey = ka.generateSecret(skAlgo); decrypt = Cipher.getInstance(skAlgo); decrypt.init(Cipher.DECRYPT_MODE, sKey); } else { String algoStr = getDhAlgoStr(skAlgo); byte[] sKeyBytes = ka.generateSecret(); SecretKeySpec sks = new SecretKeySpec(sKeyBytes, 0, keysize, algoStr); IvParameterSpec ivps = new IvParameterSpec(sKeyBytes, keysize, blocksize); decrypt = Cipher.getInstance(algoStr + "/CBC/PKCS5Padding"); decrypt.init(Cipher.DECRYPT_MODE, sks, ivps); } byte[] credentialBytes = decrypt.doFinal(encBytes); ByteArrayInputStream bis = new ByteArrayInputStream(credentialBytes); DataInputStream dinp = new DataInputStream(bis); credentials = DataSerializer.readProperties(dinp); byte[] challengeRes = DataSerializer.readByteArray(dinp); // Check the challenge string if (!Arrays.equals(challenge, challengeRes)) { throw new AuthenticationFailedException( LocalizedStrings.HandShake_MISMATCH_IN_CHALLENGE_BYTES_MALICIOUS_CLIENT .toLocalizedString()); } dinp.close(); } else { if (sendAuthentication) { // Read and ignore the client challenge DataSerializer.readByteArray(dis); } dos.writeByte(REPLY_AUTH_NOT_REQUIRED); dos.flush(); } } else if (secureMode == SECURITY_MULTIUSER_NOTIFICATIONCHANNEL) { // hitesh there will be no credential CCP will get credential(Principal) using // ServerConnection.. logger.debug("readCredential where multiuser mode creating callback connection"); } } catch (IOException ex) { throw ex; } catch (GemFireSecurityException ex) { throw ex; } catch (Exception ex) { throw new AuthenticationFailedException( LocalizedStrings.HandShake_FAILURE_IN_READING_CREDENTIALS.toLocalizedString(), ex); } return credentials; }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> eventSerialize(Cursor cursor) { logger.debug("::eventSerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { EventWrapper iw = new EventWrapper(); iw.setUuid(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.UUID))); iw.setMediaCount(cursor.getInt(cursor.getColumnIndex(EventTableSchemaBase.MEDIA_COUNT))); iw.setOriginator(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.ORIGINATOR))); iw.setDisplayName(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.DISPLAY_NAME))); iw.setCategoryId(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.CATEGORY_ID))); iw.setTitle(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.TITLE))); iw.setDescription(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.DESCRIPTION))); iw.setLongitude(/*from w w w .j av a2 s . co m*/ Util.scaleIntCoordinate(cursor.getInt(cursor.getColumnIndex(EventTableSchemaBase.LONGITUDE)))); iw.setLatitude( Util.scaleIntCoordinate(cursor.getInt(cursor.getColumnIndex(EventTableSchemaBase.LATITUDE)))); iw.setCreatedDate(cursor.getLong(cursor.getColumnIndex(EventTableSchemaBase.CREATED_DATE))); iw.setModifiedDate(cursor.getLong(cursor.getColumnIndex(EventTableSchemaBase.MODIFIED_DATE))); iw.setCid(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.CID))); iw.setCategory(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.CATEGORY))); iw.setUnit(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.UNIT))); iw.setSize(cursor.getLong(cursor.getColumnIndex(EventTableSchemaBase.SIZE))); iw.setDestGroupType(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.DEST_GROUP_TYPE))); iw.setDestGroupName(cursor.getString(cursor.getColumnIndex(EventTableSchemaBase.DEST_GROUP_NAME))); iw.setStatus(cursor.getInt(cursor.getColumnIndex(EventTableSchemaBase.STATUS))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(EventTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(EventTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try { eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :uuid uuid uuid\n // not a reference field name :media count mediaCount media_count\n // not a reference field name :originator originator originator\n // not a reference field name :display name displayName display_name\n // not a reference field name :category id categoryId category_id\n // not a reference field name :title title title\n // not a reference field name :description description description\n // not a reference field name :longitude longitude longitude\n // not a reference field name :latitude latitude latitude\n // not a reference field name :created date createdDate created_date\n // not a reference field name :modified date modifiedDate modified_date\n // not a reference field name :cid cid cid\n // not a reference field name :category category category\n // not a reference field name :unit unit unit\n // not a reference field name :size size size\n // not a reference field name :dest group type destGroupType dest_group_type\n // not a reference field name :dest group name destGroupName dest_group_name\n // not a reference field name :STATUS status status\n // EventTableSchemaBase._DISPOSITION; // try { // TODO write to content provider using openFile // if (!applCacheEventDir.exists() ) applCacheEventDir.mkdirs(); // File outfile = new File(applCacheEventDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = // new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // paths.add(outfile); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:GifEncoder.java
public void encode(BufferedImage bufferedimage, DataOutputStream dataoutputstream, Hashtable hashtable) throws Exception { try {/* w w w . jav a2 s . c o m*/ a = bufferedimage.getWidth(); g = bufferedimage.getHeight(); e = bufferedimage.getRGB(0, 0, a, g, null, 0, a); int i4 = 0; b = hashtable.get("encoding").toString(); if (b.equals("websafe")) { int ai[] = new int[256]; i = new int[256]; h = 8; int k1 = 0; int j; int j1 = j = 0; for (; j <= 255; j += 51) { for (int l = 0; l <= 255; l += 51) { for (int i1 = 0; i1 <= 255;) { i[j1] = (j << 16) + (l << 8) + i1; ai[k1++] = j1; i1 += 51; j1++; } } } if (f > 0) { int j4 = c[0]; int l1 = ((c[0] >> 16 & 0xff) + 25) / 51; int k2 = ((c[0] >> 8 & 0xff) + 25) / 51; int j3 = ((c[0] & 0xff) + 25) / 51; i4 = l1 * 36 + k2 * 6 + j3; for (j = 1; j < f; j++) { int i2 = ((c[j] >> 16 & 0xff) + 25) / 51; int l2 = ((c[j] >> 8 & 0xff) + 25) / 51; int k3 = ((c[j] & 0xff) + 25) / 51; ai[i2 * 36 + l2 * 6 + k3] = i4; } } j = 0; try { do { int i5 = e[j]; int j2 = ((i5 >> 16 & 0xff) + 25) / 51; int i3 = ((i5 >> 8 & 0xff) + 25) / 51; int l3 = ((i5 & 0xff) + 25) / 51; e[j++] = ai[j2 * 36 + i3 * 6 + l3]; } while (true); } catch (Exception exception1) { } } /*else if(b.equals("optimized")) { try { int k4 = Integer.parseInt(hashtable.get("colors").toString()); for(h = 1; k4 - 1 >> h > 0; h++) { } i = new int[1 << h]; CSelectiveQuant cselectivequant = new CSelectiveQuant(); for(int j5 = 0; j5 < e.length; j5++) { cselectivequant.addPixel(e[j5]); } boolean flag = f > 0; int k5 = flag ? 1 : 0; int ai1[] = cselectivequant.createPalette(k4 - k5); for(int l5 = 0; l5 < i.length; l5++) { try { i[l5] = ai1[l5 - k5]; } catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) { i[l5] = 0; } } if(flag) { i4 = 0; for(int i6 = 0; i6 < f; i6++) { cselectivequant.setIndex(c[i6], -1); } } for(int j6 = 0; j6 < e.length; j6++) { e[j6] = cselectivequant.getIndex(e[j6]) + k5; } } catch(NumberFormatException numberformatexception) { CmsLogger.logInfo("Parameter: 'colors' is malformated..."); return; } } */ dataoutputstream.write("GIF89a".getBytes()); dataoutputstream.writeByte(a); dataoutputstream.writeByte(a >> 8); dataoutputstream.writeByte(g); dataoutputstream.writeByte(g >> 8); dataoutputstream.writeByte(0xf0 | h - 1); dataoutputstream.writeByte(0); dataoutputstream.writeByte(0); int k = 0; try { do { int l4 = i[k++]; dataoutputstream.writeByte(l4 >> 16 & 0xff); dataoutputstream.writeByte(l4 >> 8 & 0xff); dataoutputstream.writeByte(l4 & 0xff); } while (true); } catch (Exception exception) { } if (f > 0) { dataoutputstream.writeByte(33); dataoutputstream.writeByte(249); dataoutputstream.writeByte(4); dataoutputstream.writeByte(1); dataoutputstream.writeByte(0); dataoutputstream.writeByte(0); dataoutputstream.writeByte(i4); dataoutputstream.writeByte(0); } dataoutputstream.writeByte(44); dataoutputstream.writeByte(0); dataoutputstream.writeByte(0); dataoutputstream.writeByte(0); dataoutputstream.writeByte(0); dataoutputstream.writeByte(a); dataoutputstream.writeByte(a >> 8); dataoutputstream.writeByte(g); dataoutputstream.writeByte(g >> 8); dataoutputstream.writeByte(0); dataoutputstream.writeByte(h); a(e, h, dataoutputstream); dataoutputstream.writeByte(59); dataoutputstream.flush(); return; } catch (Exception e) { } }