private String unwrapSource(String iSource) { if (iSource == null) throw new OSerializationException("Error on unmarshalling JSON content: content is null"); iSource = iSource.trim(); if (!iSource.startsWith("{") || !iSource.endsWith("}")) throw new OSerializationException("Error on unmarshalling JSON content '" + iSource + "': content must be between { }"); iSource = iSource.substring(1, iSource.length() - 1).trim(); return iSource; }
protected static byte[] bytesFromString(final String toWrite) { try { return toWrite.getBytes(CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string encoding"), e); } }
protected String stringFromBytes(final byte[] bytes, final int offset, final int len) { try { return new String(bytes, offset, len, CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string decoding"), e); } }
private byte[] bytesFromString(final String toWrite) { try { return toWrite.getBytes(CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string encoding"), e); } }
protected String stringFromBytes(final byte[] bytes, final int offset, final int len) { try { return new String(bytes, offset, len, CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string decoding"), e); } }
private byte[] bytesFromString(final String toWrite) { try { return toWrite.getBytes(CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string encoding"), e); } }
private byte[] bytesFromString(final String toWrite) { try { return toWrite.getBytes(CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string encoding"), e); } }
protected String stringFromBytes(final byte[] bytes, final int offset, final int len) { try { return new String(bytes, offset, len, CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string decoding"), e); } }
protected static String stringFromBytes(final byte[] bytes, final int offset, final int len) { try { return new String(bytes, offset, len, CHARSET_UTF_8); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error on string decoding"), e); } }
/** * Serialize the class name size + class name + object content * * @param iValue */ public StringBuilder toStream(final StringBuilder iOutput, Object iValue) { if (iValue != null) { if (!(iValue instanceof OSerializableStream)) throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface"); OSerializableStream stream = (OSerializableStream) iValue; iOutput.append(iValue.getClass().getName()); iOutput.append(OStringSerializerEmbedded.SEPARATOR); iOutput.append(Base64.getEncoder().encodeToString(stream.toStream())); } return iOutput; }
private Object getValueAsMap(ODocument iRecord, String iFieldValue, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions, String[] fields) { if (fields.length % 2 == 1) throw new OSerializationException("Bad JSON format on map. Expected pairs of field:value but received '" + iFieldValue + "'"); final Map<String, Object> embeddedMap = new LinkedHashMap<String, Object>(); for (int i = 0; i < fields.length; i += 2) { String iFieldName = fields[i]; if (iFieldName.length() >= 2) iFieldName = iFieldName.substring(1, iFieldName.length() - 1); iFieldValue = fields[i + 1]; final String valueAsString = OIOUtils.getStringContent(iFieldValue); embeddedMap.put(iFieldName, getValue(iRecord, null, iFieldValue, valueAsString, iLinkedType, null, iFieldTypes, iNoMap, iOptions)); } return embeddedMap; }
/** * Serialize the class name size + class name + object content */ public StringBuilder toStream(final StringBuilder iOutput, Object iValue) { if (iValue != null) { if (iValue instanceof ODocumentSerializable) iValue = ((ODocumentSerializable) iValue).toDocument(); if (!(iValue instanceof OSerializableStream)) throw new OSerializationException( "Cannot serialize the object since it's not implements the OSerializableStream interface"); OSerializableStream stream = (OSerializableStream) iValue; iOutput.append(iValue.getClass().getName()); iOutput.append(SEPARATOR); try { iOutput.append(new String(stream.toStream(), "UTF-8")); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error serializing embedded object"), e); } } return iOutput; }
public static String mapToJSON(final Map<?, ?> iMap, final String iFormat, final StringBuilder buffer) { try { buffer.append('{'); if (iMap != null) { int i = 0; Entry<?, ?> entry; for (Iterator<?> it = iMap.entrySet().iterator(); it.hasNext(); ++i) { entry = (Entry<?, ?>) it.next(); if (i>0) buffer.append(","); buffer.append(writeValue(entry.getKey(), iFormat)); buffer.append(":"); buffer.append(writeValue(entry.getValue(), iFormat)); } } buffer.append('}'); return buffer.toString(); } catch (IOException e) { throw OException.wrapException(new OSerializationException("Error on serializing map"), e); } }
public final int setCustom(final String iContent) { try { return set(iContent.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("error encoding string"),e); } }
public byte[] writeClassOnly(ORecord iSource) { final ODocument record = (ODocument) iSource; StringBuilder iOutput = new StringBuilder(); if (ODocumentInternal.getImmutableSchemaClass(record) != null) { iOutput.append(ODocumentInternal.getImmutableSchemaClass(record).getStreamableName()); iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR); } try { return iOutput.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("error writing class name"), e); } }
/** * Fills the ODocument directly with the string representation of the document itself. Use it for faster insertion but pay * attention to respect the OrientDB record format. <p> <code> record.reset();<br> record.setClassName("Account");<br> * record.fromString(new String("Account@id:" + data.getCyclesDone() + ",name:'Luca',surname:'Garulli',birthDate:" + * date.getTime()<br> + ",salary:" + 3000f + i));<br> record.save();<br> </code> </p> * * @param iValue String representation of the record. */ @Deprecated public void fromString(final String iValue) { _dirty = true; _contentChanged = true; try { _source = iValue.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSerializationException("Error reading content from string"), e); } removeAllCollectionChangeListeners(); _fields = null; _fieldSize = 0; }
@Override public byte[] toStream(final ORecord iSource, boolean iOnlyDelta) { try { return iSource.toStream(); } catch (Exception e) { final String message = "Error on unmarshalling object in binary format: " + iSource.getIdentity(); OLogManager.instance().error(this, message, e); throw OException.wrapException(new OSerializationException(message), e); } }
private void writeEmbeddedCollection(final BytesContainer bytes, final Collection<?> value) { OVarIntSerializer.write(bytes, value.size()); for (Object itemValue : value) { // TODO:manage in a better way null entry if (itemValue == null) { writeOType(bytes, bytes.alloc(1), null); continue; } OType type = getTypeFromValueEmbedded(itemValue); if (type != null) { writeOType(bytes, bytes.alloc(1), type); serializeValue(bytes, itemValue, type, null); } else { throw new OSerializationException( "Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer"); } } }
@Override public OStorageConfigurationImpl load(final OContextConfiguration configuration) throws OSerializationException { lock.acquireWriteLock(); try { initConfiguration(configuration); try { fromStream(serializedContent, 0, serializedContent.length, streamCharset); } catch (Exception e) { throw OException .wrapException(new OSerializationException("Cannot load database configuration. The database seems corrupted"), e); } return this; } finally { lock.releaseWriteLock(); } }
@Override public void update() throws OSerializationException { lock.acquireWriteLock(); try { try { serializedContent = toStream(streamCharset); } catch (Exception e) { throw OException.wrapException(new OSerializationException("Error on update storage configuration"), e); } if (updateListener != null) { updateListener.onUpdate(this); } } finally { lock.releaseWriteLock(); } } }