Skip to content

Commit

Permalink
StreamOutWrapper: add a constructor that takes a StringStream
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Nov 26, 2024
1 parent b0a5144 commit baf1fbe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/StreamOutWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.std.StringStream;

/**
* A wrapper around an {@code std::ofstream}.
*
Expand All @@ -40,6 +42,17 @@ public StreamOutWrapper(String fileName, int streamMode) {
long streamVa = createStreamOutWrapper(fileName, streamMode);
setVirtualAddress(streamVa, true);
}

/**
* Open a {@code StringStream} for output.
*
* @param data the underlying stream (not null)
*/
public StreamOutWrapper(StringStream data) {
long dataVa = data.va();
long streamVa = createFromStringStream(dataVa);
setVirtualAddress(streamVa, true);
}
// *************************************************************************
// new methods exposed

Expand Down Expand Up @@ -89,6 +102,8 @@ public StreamOutWrapper(String fileName, int streamMode) {
// *************************************************************************
// native private methods

native private static long createFromStringStream(long dataVa);

native private static long createStreamOutWrapper(
String fileName, int streamMode);
}
14 changes: 14 additions & 0 deletions src/main/native/glue/s/StreamOutWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_StreamOutWrapper_trun
return result;
}

/*
* Class: com_github_stephengold_joltjni_StreamOutWrapper
* Method: createFromStringStream
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_StreamOutWrapper_createFromStringStream
(JNIEnv *, jclass, jlong streamVa) {
std::stringstream * const pStream
= reinterpret_cast<std::stringstream *> (streamVa);
StreamOutWrapper * const pResult = new StreamOutWrapper(*pStream);
TRACE_NEW("StreamOutWrapper", pResult)
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_StreamOutWrapper
* Method: createStreamOutWrapper
Expand Down

0 comments on commit baf1fbe

Please sign in to comment.