ParseHelper { static InputStream stripGraphicsFuzzDefines(InputStream inputStream) throws IOException { if (!containsEndOfGraphicsFuzzDefines(inputStream)) { return inputStream; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try ( BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream)); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { boolean passedEndOfGraphicsFuzzDefines = false; String line; while ((line = br.readLine()) != null) { if (passedEndOfGraphicsFuzzDefines || isVersion(line) || ShadingLanguageVersion.isWebGlHint(line)) { bw.write(line + "\n"); } else { if (line.trim().startsWith(END_OF_GRAPHICSFUZZ_DEFINES)) { passedEndOfGraphicsFuzzDefines = true; } } } } byte[] fileContents = outputStream.toByteArray(); return new ByteArrayInputStream(fileContents); }  static Optional<TranslationUnit> maybeParseShader(File shader); static synchronized TranslationUnit parse(File file); static synchronized TranslationUnit parse(String string, ShaderKind shaderKind); static synchronized TranslationUnit parse(String string); static final String END_OF_GRAPHICSFUZZ_DEFINES; }
@Test public void testStripHeader() throws Exception { final String withHeader = "#version 310 es\n" + " + " + " + " + "void main()\n" + "{\n" + "}\n"; final String expected = "#version 310 es\n" + " + "void main()\n" + "{\n" + "}\n"; final InputStream is = new ByteArrayInputStream(withHeader.getBytes(StandardCharsets.UTF_8)); final InputStream strippedIs = ParseHelper.stripGraphicsFuzzDefines(is); assertNotSame(is, strippedIs); assertEquals(expected, getStringFromInputStream(strippedIs)); }
@Test public void testStripHeader2() throws Exception { final String withHeader = "#version 440\n" + " + " + "\n\n\n" + " + "void main()\n" + "{\n" + "}\n"; final String expected = "#version 440\n" + "void main()\n" + "{\n" + "}\n"; final InputStream is = new ByteArrayInputStream(withHeader.getBytes(StandardCharsets.UTF_8)); final InputStream strippedIs = ParseHelper.stripGraphicsFuzzDefines(is); assertNotSame(is, strippedIs); assertEquals(expected, getStringFromInputStream(strippedIs)); }
@Test public void testStripHeader3() throws Exception { final String withHeader = " + " + "\n\n\n" + " + "void main()\n" + "{\n" + "}\n"; final String expected = "void main()\n" + "{\n" + "}\n"; final InputStream is = new ByteArrayInputStream(withHeader.getBytes(StandardCharsets.UTF_8)); final InputStream strippedIs = ParseHelper.stripGraphicsFuzzDefines(is); assertNotSame(is, strippedIs); assertEquals(expected, getStringFromInputStream(strippedIs)); }
@Test public void testDoNotStripHeader() throws Exception { final String withoutHeader = "#version 310 es\n" + " + " + " + "void main()\n" + "{\n" + "}\n"; final InputStream is = new ByteArrayInputStream(withoutHeader.getBytes(StandardCharsets.UTF_8)); final InputStream strippedIs = ParseHelper.stripGraphicsFuzzDefines(is); assertSame(is, strippedIs); }