Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 05b5a19

Browse files
authored
Merge pull request #23 from juanjux/avoid_memorystream
Avoid memorystream
2 parents 9e3b934 + 95419f1 commit 05b5a19

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

driver/normalizer/annotation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var Code = []CodeTransformer{
1818
positioner.NewFillLineColFromOffset(),
1919
}
2020

21+
var PreprocessCode = []CodeTransformer{}
22+
2123
func annotateTypeToken(typ, token string, roles ...role.Role) Mapping {
2224
return AnnotateType(typ,
2325
FieldRoles{

native/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>com.fasterxml.jackson.core</groupId>
7373
<artifactId>jackson-databind</artifactId>
74-
<version>2.8.11.1</version>
74+
<version>2.8.11.3</version>
7575
</dependency>
7676
<dependency>
7777
<groupId>com.fasterxml.jackson.core</groupId>

native/src/main/java/tech/sourced/babelfish/DriverResponse.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ void parseCode(EclipseCPPParser parser, String source) {
3838
translationUnit = parser.parseCPP(source);
3939
}
4040

41+
// Note: since we're using the System.out output stream with Jackson, output will
42+
// start to be written before this call so its not a deterministic "send everything".
43+
// The reason to not use a ByteArrayOutputStream and send everything in one go is that
44+
// sometimes memory can grow too much with some files.
4145
void send() throws ResponseSendException {
4246
// FIXME: this includes the errors in the already started document
4347
try {
4448
formatWritter.writeValue(this);
45-
ByteArrayOutputStream byteOut = formatWritter.getByteOutputStream();
46-
System.out.write(byteOut.toByteArray());
49+
OutputStream byteOut = formatWritter.getOutputStream();
50+
byteOut.flush();
4751
System.out.write('\n');
4852
} catch (IOException e) {
4953
throw new DriverResponse.ResponseSendException(e);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package tech.sourced.babelfish;
22

3-
import java.io.ByteArrayOutputStream;
3+
import java.io.OutputStream;
44
import java.io.IOException;
55

66
/**
@@ -10,5 +10,5 @@
1010
public interface IExchangeFormatWritter
1111
{
1212
void writeValue(DriverResponse response) throws IOException;
13-
ByteArrayOutputStream getByteOutputStream();
13+
OutputStream getOutputStream();
1414
}

native/src/main/java/tech/sourced/babelfish/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void main(String args[]) {
2020
private static ProcessCycle trySendError(String msg, Exception e) {
2121
try {
2222
final TranslationUnitJSONMapper responseJSONMapper =
23-
new TranslationUnitJSONMapper(false, new ByteArrayOutputStream());
23+
new TranslationUnitJSONMapper(false, System.out);
2424
DriverResponse response = new DriverResponse(responseJSONMapper);
2525
response.sendError(e, msg);
2626
return ProcessCycle.CONTINUE;
@@ -38,7 +38,7 @@ static private ProcessCycle process() {
3838
try {
3939
final EclipseCPPParser parser = new EclipseCPPParser();
4040
final TranslationUnitJSONMapper responseJSONMapper =
41-
new TranslationUnitJSONMapper(false, new ByteArrayOutputStream());
41+
new TranslationUnitJSONMapper(false, System.out);
4242
response = new DriverResponse(responseJSONMapper);
4343

4444
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

native/src/main/java/tech/sourced/babelfish/TranslationUnitJSONMapper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
import com.fasterxml.jackson.databind.module.SimpleModule;
1111
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
1212

13-
import java.io.ByteArrayOutputStream;
13+
import java.io.OutputStream;
14+
import java.io.PrintStream;
1415
import java.io.IOException;
1516

1617
class TranslationUnitJSONMapper implements IExchangeFormatWritter {
1718

1819
final JsonGenerator generator;
1920
final JsonFactory jsonFactory = new JsonFactory();
2021
final ObjectMapper mapper = new ObjectMapper();
21-
private ByteArrayOutputStream byteOutputStream;
22+
private OutputStream printStream;
2223

23-
TranslationUnitJSONMapper(boolean prettyPrint, ByteArrayOutputStream byteOutput) throws IOException {
24-
this.byteOutputStream = byteOutput;
24+
TranslationUnitJSONMapper(boolean prettyPrint, PrintStream byteOutput) throws IOException {
25+
this.printStream = byteOutput;
2526

26-
generator = jsonFactory.createGenerator(byteOutputStream);
27+
generator = jsonFactory.createGenerator(printStream);
2728
if (prettyPrint) {
2829
generator.setPrettyPrinter(new DefaultPrettyPrinter());
2930
mapper.enable(SerializationFeature.INDENT_OUTPUT);
@@ -40,7 +41,7 @@ public void writeValue(DriverResponse response) throws IOException {
4041
mapper.writeValue(generator, response);
4142
}
4243

43-
public ByteArrayOutputStream getByteOutputStream() {
44-
return byteOutputStream;
44+
public OutputStream getOutputStream() {
45+
return printStream;
4546
}
4647
}

0 commit comments

Comments
 (0)