diff --git a/Code/JavaWrappers/ExplicitBitVect.i b/Code/JavaWrappers/ExplicitBitVect.i index ac717b90c..9369366d6 100644 --- a/Code/JavaWrappers/ExplicitBitVect.i +++ b/Code/JavaWrappers/ExplicitBitVect.i @@ -61,37 +61,44 @@ #endif %typemap(javacode) ExplicitBitVect %{ - public static ExplicitBitVect fromByteArray(byte[] fp) { - UChar_Vect vec = new UChar_Vect(); - vec.reserve(fp.length); - for (int size=0;size %newobject ExplicitBitVect::getOnBits; %extend ExplicitBitVect { - IntVect *getOnBits() { - IntVect* bits = new IntVect; - ($self)->getOnBits(*bits); - return bits; - } + IntVect *getOnBits() { + IntVect* bits = new IntVect; + ($self)->getOnBits(*bits); + return bits; + } } #ifdef SWIGJAVA %extend ExplicitBitVect { - const std::string toByteArray() { - return ($self)->toString(); - } + const std::string toByteArray() { + return ($self)->toString(); + } - ExplicitBitVect(const std::vector & data ) { - std::string str(data.begin(), data.end()); - return new ExplicitBitVect(str); - } + ExplicitBitVect(const std::vector & data ) { + std::string str(data.begin(), data.end()); + return new ExplicitBitVect(str); + } } #endif diff --git a/Code/JavaWrappers/FilterCatalog.i b/Code/JavaWrappers/FilterCatalog.i index 2a4a0886f..1f9a2f44d 100644 --- a/Code/JavaWrappers/FilterCatalog.i +++ b/Code/JavaWrappers/FilterCatalog.i @@ -64,14 +64,21 @@ typedef std::vector STR_VECT; %typemap(javacode) RDKit::FilterCatalog %{ - public static FilterCatalog Deserialize(byte[] b) { - UChar_Vect vec = new UChar_Vect(); - vec.reserve(b.length); - for (int size=0;size > >; %template(Atom_Vect) std::vector; %template(StereoGroup_Vect) std::vector; +%template(UChar_Vect) std::vector; // These prevent duplicate definitions in Java code %ignore RDKit::ROMol::hasProp(std::string const) const ; @@ -97,6 +98,19 @@ %ignore RDKit::ROMol::getTopology() const ; +#ifdef SWIGJAVA +%typemap(jni) std::string RDKit::ROMol::toByteArray "jbyteArray" +%typemap(jtype) std::string RDKit::ROMol::toByteArray "byte[]" +%typemap(jstype) std::string RDKit::ROMol::toByteArray "byte[]" +%typemap(javaout) std::string RDKit::ROMol::toByteArray { + return $jnicall; +} +%typemap(out) std::string RDKit::ROMol::toByteArray { + $result = JCALL1(NewByteArray, jenv, $1.size()); + JCALL4(SetByteArrayRegion, jenv, $result, 0, $1.size(), (const jbyte*)$1.c_str()); +} +#endif + /* * Special handling for Conformer objects which should not be GCed until the molecule is destroyed * We want to modify the behavior of the Conformer coming into the addConformer method without @@ -115,6 +129,51 @@ conf.setSwigCMemOwn(false); return Conformer.getCPtr(conf); } + public static ROMol fromByteArray(byte[] pkl) { + UChar_Vect vec = null; + try { + vec = new UChar_Vect(); + vec.reserve(pkl.length); + for (int i = 0; i < pkl.length; ++i) { + vec.add((byte)pkl[i]); + } + return ROMol.fromUCharVect(vec); + } finally { + if (vec != null) { + vec.delete(); + } + } + } +%} +%typemap(cscode) RDKit::ROMol %{ + public static ROMol FromByteArray(byte[] pkl) { + UChar_Vect vec = null; + try { + vec = new UChar_Vect(); + vec.Capacity = pkl.Length; + for (int i = 0; i < pkl.Length; ++i) { + vec.Add((byte)pkl[i]); + } + return ROMol.fromUCharVect(vec); + } finally { + if (vec != null) { + vec.Dispose(); + } + } + } + public byte[] ToByteArray() { + UChar_Vect vec = null; + try { + vec = toUCharVect(); + byte[] res = new byte[vec.Count]; + vec.CopyTo(res); + return res; + } finally { + if (vec != null) { + vec.Dispose(); + } + } + } %} %include @@ -176,6 +235,14 @@ void setUseLegacyStereoPerception(bool); bool getAllowNontetrahedralChirality(); void setAllowNontetrahedralChirality(bool); +#ifdef SWIGJAVA +%javamethodmodifiers RDKit::ROMol::fromUCharVect "private"; +#endif +#ifdef SWIGCSHARP +%csmethodmodifiers RDKit::ROMol::fromUCharVect "private"; +%csmethodmodifiers RDKit::ROMol::toUCharVect "private"; +#endif + %extend RDKit::ROMol { std::string getProp(const std::string key){ std::string res; @@ -523,7 +590,7 @@ void setAllowNontetrahedralChirality(bool); std::copy(sres.begin(),sres.end(),res.begin()); return res; }; - static RDKit::ROMOL_SPTR MolFromBinary(std::vector pkl){ + static RDKit::ROMOL_SPTR MolFromBinary(const std::vector &pkl){ std::string sres; sres.resize(pkl.size()); std::copy(pkl.begin(),pkl.end(),sres.begin()); @@ -536,6 +603,32 @@ void setAllowNontetrahedralChirality(bool); } return RDKit::ROMOL_SPTR(res); } +#ifdef SWIGJAVA + const std::string toByteArray() { + std::string sres; + RDKit::MolPickler::pickleMol(*($self), sres); + return sres; + } +#endif +#ifdef SWIGCSHARP + const std::vector toUCharVect() { + std::string sres; + RDKit::MolPickler::pickleMol(*($self), sres); + const std::vector vec(sres.begin(), sres.end()); + return vec; + } +#endif + static RDKit::ROMOL_SPTR fromUCharVect(const std::vector &pkl) { + std::string sres(pkl.begin(), pkl.end()); + RDKit::ROMol *res; + try { + res = new RDKit::ROMol(sres); + } catch (const RDKit::MolPicklerException &e) { + res = nullptr; + throw; + } + return RDKit::ROMOL_SPTR(res); + } /* From AddHs.cpp */ RDKit::ROMol *addHs(bool explicitOnly,bool addCoords=false){ diff --git a/Code/JavaWrappers/SubstructLibrary.i b/Code/JavaWrappers/SubstructLibrary.i index cefe46966..3f9cfda23 100644 --- a/Code/JavaWrappers/SubstructLibrary.i +++ b/Code/JavaWrappers/SubstructLibrary.i @@ -52,14 +52,21 @@ %template(UChar_Vect) std::vector; %typemap(javacode) RDKit::SubstructLibrary %{ - public static SubstructLibrary Deserialize(byte[] b) { - UChar_Vect vec = new UChar_Vect(); - vec.reserve(b.length); - for (int size=0;size