public void TestStringToBytesCompressed() { const string a = "abcd"; var bytes = a.GetBytes(); var compressed = CompressionUtil.Compress(bytes); var decompressed = CompressionUtil.Decompress(compressed); Assert.AreEqual(a, StringExtensions.GetString(decompressed)); }
public static T Deserialize <T>(byte[] compressedBytes) { // TODO: should not use ecs specific compressor when extracted var decompressedBytes = CompressionUtil.Decompress(compressedBytes); var serializedString = Encoding.UTF8.GetString(decompressedBytes); var deserializedObject = JsonConvert.DeserializeObject <T>(serializedString, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); return(deserializedObject); }
private JSONDocument DeserializeDocument(byte[] data) { var stream = new ClusteredMemoryStream(data); int header = stream.ReadByte(); if ((header & (long)PersistenceBits.Compressed) == (decimal)PersistenceBits.Compressed) { stream = CompressionUtil.Decompress(stream); } var document = JSONDocument.Deserialize(stream);// CompactBinaryFormatter.Deserialize(stream, string.Empty); stream.Dispose(); return(document as JSONDocument); }
public object Deserialize(byte[] data, Type type) { if (data[0] == 1) { var totalLength = data.Length - 1; var needDecompressBytes = new byte[totalLength]; Buffer.BlockCopy(data, 1, needDecompressBytes, 0, totalLength); var bytes = CompressionUtil.Decompress(needDecompressBytes); return(SerializerFactory.Deserializer(bytes)); } else { return(SerializerFactory.Deserializer(data, startOffset: 1)); } }
private void GetMessageItemPayLoad(MessageItem messageItem) { if (messageItem != null && !messageItem.Deserialized) { if (messageItem.Payload != null) { if (messageItem.Flag.IsBitSet(BitSetConstants.Compressed)) { messageItem.Payload = CompressionUtil.Decompress((byte[])messageItem.Payload); } } messageItem.Payload = _cacheImpl.SafeDeserialize <object>(messageItem.Payload, _cacheImpl.Name, messageItem.Flag, _cacheImpl, UserObjectType.CacheItem); messageItem.Deserialized = true; } }
/// <summary> /// Mainly written for cache data as JSON for backing source. Please do not call this method from anywhere /// other than the derived classes of <see cref="Runtime.Caching.ProviderItemBase"/>. /// </summary> public T GetCacheData <T>(object data, BitSet flag, UserObjectType userObjectType) { var userBinaryObject = data as UserBinaryObject; // Cache Items if (userBinaryObject != default(UserBinaryObject)) { var serializedObject = userBinaryObject.GetFullObject() as byte[]; if (_context.CompressionEnabled) { serializedObject = CompressionUtil.Decompress(serializedObject, flag); } return(SerializationUtil.SafeDeserializeOutProc <T>(serializedObject, _context.SerializationContext, flag, true, userObjectType)); } return((T)data); }
/// <summary> /// Recupera o valor da entrada. /// </summary> /// <param name="cacheContext"></param> /// <returns></returns> internal object DeflattedValue(string cacheContext) { object obj2 = this.Value; lock (this) { if (!IsFlattened) { return(obj2); } UserBinaryObject obj3 = null; CallbackEntry entry = obj2 as CallbackEntry; if (entry != null) { obj3 = entry.Value as UserBinaryObject; } else { obj3 = obj2 as UserBinaryObject; } byte[] fullObject = obj3.GetFullObject(); if (IsCompressed) { fullObject = CompressionUtil.Decompress(fullObject); _flags.UnsetBit(2); } _size = fullObject.Length; obj2 = CompactBinaryFormatter.FromByteBuffer(fullObject, cacheContext); if (entry != null) { entry.Value = obj2; obj2 = entry; } } return(obj2); }
public object GetCacheData(object data, BitSet flag) { byte[] serializedObject = null; try { UserBinaryObject userBinaryObject = null; if (data is UserBinaryObject) { userBinaryObject = (UserBinaryObject)data; } else { userBinaryObject = UserBinaryObject.CreateUserBinaryObject((ICollection)data, _context.TransactionalPoolManager); } if (userBinaryObject != null) { serializedObject = userBinaryObject.GetFullObject() as byte[]; if (_context.CompressionEnabled) { serializedObject = CompressionUtil.Decompress(serializedObject, flag); } return(SerializationUtil.SafeDeserialize(serializedObject, _context.SerializationContext, flag)); } } catch (Exception ex) { if (_context.NCacheLog != null && _context.NCacheLog.IsErrorEnabled) { _context.NCacheLog.Error("ObjectDataFormatService.GetCacheData()", ex.Message); } } return(data); }
public bool WriteThru(CacheBase cacheImpl, string key, CacheEntry cacheEntry, OpCode operationCode, OperationContext operationContext) { if ((_context.DatasourceMgr == null) || ((_context != null) && !_context.DatasourceMgr.IsWriteThruEnabled)) { throw new OperationFailedException("Backing source not available. Verify backing source settings"); } Exception inner = null; int num = 0; try { object serializedObject = null; if (((cacheEntry != null) && (operationCode != OpCode.Remove)) && (operationCode != OpCode.Clear)) { if (cacheEntry.Value is CallbackEntry) { serializedObject = ((CallbackEntry)cacheEntry.Value).Value; } else { serializedObject = cacheEntry.Value; } if (serializedObject != null) { serializedObject = CompressionUtil.Decompress(((UserBinaryObject)serializedObject).GetFullObject() as byte[], cacheEntry.Flag); serializedObject = SerializationUtil.SafeDeserialize(serializedObject, _context.SerializationContext, cacheEntry.Flag); } } num = this.WriteThru(key, serializedObject, operationCode); } catch (Exception exception2) { inner = exception2; } finally { if (num == 0) { try { switch (operationCode) { case OpCode.Add: case OpCode.Update: cacheImpl.Remove(key, ItemRemoveReason.Removed, false, null, 0, LockAccessType.IGNORE_LOCK, operationContext); break; } } catch (Exception exception3) { throw new OperationFailedException("error while trying to synchronize the cache with data source. Error: " + exception3.Message, exception3); } if (inner != null) { throw new OperationFailedException("IWriteThruProvider failed. Error: " + inner.Message, inner); } } } if (num != 1) { return(false); } return(true); }