private String getClassBody(Class<?> c) throws CompileException, IOException{ String path = c.getName(); path = path.replaceFirst("\\$.*", ""); path = path.replace(".", FileUtils.separator); path = "/" + path + ".java"; URL u = Resources.getResource(c, path); InputSupplier<InputStream> supplier = Resources.newInputStreamSupplier(u); try (InputStream is = supplier.getInput()) { if (is == null) { throw new IOException(String.format("Failure trying to located source code for Class %s, tried to read on classpath location %s", c.getName(), path)); } String body = IO.toString(is); //TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem... //return body.replaceAll("@(?:Output|Param|Workspace|Override|SuppressWarnings\\([^\\\\]*?\\)|FunctionTemplate\\([^\\\\]*?\\))", ""); return body.replaceAll("@(?:\\([^\\\\]*?\\))?", ""); } }
private ArrayList<String> extractProjectFiles(ZipFile inputZip, File projectRoot) throws IOException { ArrayList<String> projectFileNames = Lists.newArrayList(); Enumeration<? extends ZipEntry> inputZipEnumeration = inputZip.entries(); while (inputZipEnumeration.hasMoreElements()) { ZipEntry zipEntry = inputZipEnumeration.nextElement(); final InputStream extractedInputStream = inputZip.getInputStream(zipEntry); File extractedFile = new File(projectRoot, zipEntry.getName()); LOG.info("extracting " + extractedFile.getAbsolutePath() + " from input zip"); Files.createParentDirs(extractedFile); // Do I need this? Files.copy( new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { return extractedInputStream; } }, extractedFile); projectFileNames.add(extractedFile.getPath()); } return projectFileNames; }
private void verifyPutAndGet(String blobId, final byte[] blobData, Map<String, String> attributes, @Nullable Duration ttl) throws IOException { _store.put(TABLE, blobId, new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return new ByteArrayInputStream(blobData); } }, attributes, ttl); // verify that we can get what we put Blob blob = _store.get(TABLE, blobId); assertEquals(blob.getId(), blobId); assertEquals(blob.getLength(), blobData.length); assertEquals(blob.getAttributes(), attributes); ByteArrayOutputStream buf = new ByteArrayOutputStream(); blob.writeTo(buf); assertEquals(buf.toByteArray(), blobData); BlobMetadata md = _store.getMetadata(TABLE, blobId); assertEquals(md.getId(), blobId); assertEquals(md.getLength(), blobData.length); assertEquals(md.getAttributes(), attributes); }
@Converter public static Payload toPayload(final InputStream is, Exchange exchange) throws IOException { InputStreamPayload payload = new InputStreamPayload(is); // only set the contentlength if possible if (is.markSupported()) { long contentLength = ByteStreams.length(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return is; } }); is.reset(); payload.getContentMetadata().setContentLength(contentLength); } return payload; }
/** * Load the content of a file from a stream. * <p> * Use {@link Files#newInputStreamSupplier(java.io.File)} to provide a stream from a file. * @param stream - the stream supplier. * @param option - whether or not to decompress the input stream. * @return The decoded NBT compound. * @throws IOException If anything went wrong. */ public static NbtCompound fromStream(InputSupplier<? extends InputStream> stream, StreamOptions option) throws IOException { InputStream input = null; DataInputStream data = null; boolean suppress = true; try { input = stream.getInput(); data = new DataInputStream(new BufferedInputStream( option == StreamOptions.GZIP_COMPRESSION ? new GZIPInputStream(input) : input )); NbtCompound result = fromCompound(get().LOAD_COMPOUND.loadNbt(data)); suppress = false; return result; } finally { if (data != null) Closeables.close(data, suppress); else if (input != null) Closeables.close(input, suppress); } }
public static void setup(String deobFileName){ try{ LZMAInputSupplier zis = new LZMAInputSupplier(FMLInjectionData.class.getResourceAsStream(deobFileName)); InputSupplier<InputStreamReader> srgSupplier = CharStreams.newReaderSupplier(zis, Charsets.UTF_8); List<String> srgList = CharStreams.readLines(srgSupplier); for (String line : srgList) { line = line.replace(" #C", "").replace(" #S", ""); if (line.startsWith("CL")) { parseClass(line); } else if (line.startsWith("FD")) { parseField(line); } else if (line.startsWith("MD")) { parseMethod(line); } } }catch(Exception e){ e.printStackTrace(); } }
@Test public void read_properties_file_guava() throws IOException { URL url = Resources.getResource(PROPERTY_FILE_NAME); InputSupplier<InputStream> inputSupplier = Resources.newInputStreamSupplier(url); Properties properties = new Properties(); properties.load(inputSupplier.getInput()); logger.info(properties); assertEquals("http://www.leveluplunch.com", properties.getProperty("website")); assertEquals("English", properties.getProperty("language")); assertEquals("Welcome up to leveluplunch.com", properties.getProperty("message")); }
/** * 将文件打包成消息协议 * @param file * @return * 添加(修改)人:zhuyuping * @throws Exception * @throws FileNotFoundException */ private FileDataMessage wrapFile(File file){ FileDataMessage fm=new FileDataMessage(); try{ final InputStream input=new FileInputStream(file); fm.setBytes(ByteStreams.toByteArray(input)); fm.setSessionId(System.identityHashCode(file.getAbsolutePath())); fm.setHead(1314); fm.setCommand(1); fm.setLenth(file.length()); fm.setPath(file.getAbsolutePath()); fm.setStart(0); fm.setEnd(file.length()); fm.setHash(ByteStreams.getChecksum(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return input; } }, new Adler32()));//crc32最强验证 adl32最弱验证 }catch(Exception e){ } return fm; }
@Test public void testNoNulls() throws Exception { JvmOptions options = new JvmOptions("-version", new JvmOptions.DebugOptions(true, false, ImmutableSet.of("one", "two"))); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
@Test public void testSomeNulls() throws Exception { JvmOptions options = new JvmOptions(null, new JvmOptions.DebugOptions(false, false, null)); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
@Test public void testNoRunnables() throws Exception { List<String> noRunnables = Collections.emptyList(); JvmOptions options = new JvmOptions(null, new JvmOptions.DebugOptions(true, false, noRunnables)); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
/** * Load the content of a file from a stream. * <p> * Use {@link Files#newInputStreamSupplier(java.io.File)} to provide a stream from a file. * * @param stream - the stream supplier. * @param option - whether or not to decompress the input stream. * @return The decoded NBT compound. * @throws IOException If anything went wrong. */ public static NbtCompound fromStream(InputSupplier<? extends InputStream> stream, StreamOptions option) throws IOException { InputStream input = null; DataInputStream data = null; boolean suppress = true; try { input = stream.getInput(); data = new DataInputStream(new BufferedInputStream( option == StreamOptions.GZIP_COMPRESSION ? new GZIPInputStream(input) : input )); NbtCompound result = fromCompound(get().LOAD_COMPOUND.loadNbt(data)); suppress = false; return result; } finally { if (data != null) Closeables.close(data, suppress); else if (input != null) Closeables.close(input, suppress); } }
protected Payload doSlice(final FSDataInputStream inputStream, final long offset, final long length) { return new InputStreamSupplierPayload(new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { if (offset > 0) { try { inputStream.seek(offset); } catch (IOException e) { Closeables.closeQuietly(inputStream); throw e; } } return new LimitInputStream(inputStream, length); } }); }
public LazyLoadFrame(Context context, InputSupplier<? extends InputStream> frame) throws IOException { this.context = context; DataInputStream din = new DataInputStream(frame.getInput()); int version = din.readInt(); if(version != VERSION) { throw new IOException("Unsupported version: " + version); } int count = din.readInt(); for(int i=0;i!=count;++i) { String name = din.readUTF(); int byteCount = din.readInt(); byte[] bytes = new byte[byteCount]; din.readFully(bytes); map.put(Symbol.get(name), new Value(bytes)); } din.close(); }
public void testWriteToStrings() throws Exception { URL stringsUrl = Resources.getResource(AndroidStringsTest.class, "strings.xml"); InputSupplier<InputStream> inputSupplier = Resources.newInputStreamSupplier(stringsUrl); MessageCatalog messageCatalog = new AndroidStrings(); ReadableMessageCatalog readableCatalog = messageCatalog.readFrom(inputSupplier.getInput()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); WritableMessageCatalog writableCatalog = messageCatalog.writeTo(outputStream); for (Message message : readableCatalog.readMessages()) { writableCatalog.writeMessage(message); } writableCatalog.close(); String content = new String(outputStream.toByteArray(), Charsets.UTF_8); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<resources>\n" + " <string name=\"hello\">Hello!</string>\n" + "</resources>\n"; Assert.assertEquals(expected, content); }
@Override public InputStream newInput() { try { return ByteStreams.join(Iterables.transform( bytes, new Function<ByteStringLiteral, InputSupplier<InputStream>>() { @Override public InputSupplier<InputStream> apply(final ByteStringLiteral literal) { return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return literal.newInput(); } }; } } )).getInput(); } catch (IOException e) { throw Throwables.propagate(e); } }
@Override public boolean equals(final Object obj) { if (obj == null || !(obj instanceof ByteString)) { return false; } try { return ByteStreams.equal( new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return newInput(); } }, new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return ((ByteString) obj).newInput(); } } ); } catch (IOException e) { throw Throwables.propagate(e); } }
private File[] collectJars() throws IOException { File classpathFile = new File(outputDirectory, "classpath"); if (!classpathFile.exists()) { return null; } final FileReader cpFileReader = new FileReader(classpathFile); String classpath = CharStreams.readFirstLine(new InputSupplier<FileReader>() { @Override public FileReader getInput() throws IOException { return cpFileReader; } }); String[] jars = classpath.split(";"); File[] jarFiles = new File[jars.length]; for (int i = 0; i < jars.length; i++) { String jar = jars[i]; jarFiles[i] = new File(jar); } return jarFiles; }
public static <T extends Reader> List<Refactoring> fromInputSupplier(InputSupplier<T> supplier) throws IOException{ return CharStreams.readLines(supplier, new LineProcessor<List<Refactoring>>() { private List<Refactoring> refactorings = Lists.newArrayList(); private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings(); @Override public List<Refactoring> getResult() { return Collections.unmodifiableList(refactorings); } @Override public boolean processLine(String line) { Iterator<String> parts = SPLITTER.split(line).iterator(); String from = parts.next(); String to = parts.next(); refactorings.add(Refactoring.of(from, to, Refactoring.Visit.Expressions)); return true; } }); }
/** * @param resourceName * the classpath-relative location of the to-be-read resource */ private static List<String> getFileLines(final String resourceName) throws IOException { InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier( new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); } }, Charsets.UTF_8); return CharStreams.readLines(readerSupplier); }
/***/ public static String getContentsFromFileEntry(final ZipEntry entry, String rootName) throws IOException, URISyntaxException { URL rootURL = Thread.currentThread().getContextClassLoader().getResource(rootName); try (final ZipFile root = new ZipFile(new File(rootURL.toURI()));) { InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier( new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return root.getInputStream(entry); } }, Charsets.UTF_8); return CharStreams.toString(readerSupplier); } }
private CompilationUnit get(Class<?> c) throws IOException{ String path = c.getName(); path = path.replaceFirst("\\$.*", ""); path = path.replace(".", FileUtils.separator); path = "/" + path + ".java"; CompilationUnit cu = functionUnits.get(path); if(cu != null) { return cu; } URL u = Resources.getResource(c, path); InputSupplier<InputStream> supplier = Resources.newInputStreamSupplier(u); try (InputStream is = supplier.getInput()) { if (is == null) { throw new IOException(String.format("Failure trying to located source code for Class %s, tried to read on classpath location %s", c.getName(), path)); } String body = IO.toString(is); //TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem... body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", ""); try{ cu = new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit(); functionUnits.put(path, cu); return cu; } catch (CompileException e) { logger.warn("Failure while parsing function class:\n{}", body, e); return null; } } }
public long computeMD5() throws IOException { InputSupplier<InputStream> supplier = new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { return newInputStream(); } }; return ByteStreams.hash(supplier, Hashing.md5()).asLong(); }
/** * Returns an InputSupplier that throws an exception if the caller attempts to consume the input stream * multiple times. */ private InputSupplier<InputStream> onceOnlySupplier(final InputStream in) { final AtomicBoolean once = new AtomicBoolean(); return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { if (!once.compareAndSet(false, true)) { throw new IllegalStateException("Input stream may be consumed only once per BlobStore call."); } return in; } }; }
@Test public void testPut() throws IOException { InputSupplier<InputStream> in = new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return new ByteArrayInputStream("blob-content".getBytes()); } }; Map<String, String> attributes = ImmutableMap.of("key", "value"); blobClient().put("table-name", "blob-id", in, attributes, Duration.standardDays(30)); //noinspection unchecked verify(_server).put(eq("table-name"), eq("blob-id"), isA(InputSupplier.class), eq(attributes), eq(Duration.standardDays(30))); verifyNoMoreInteractions(_server); }
@Test @SuppressWarnings("unchecked") public void testMalformedChunkException() throws Exception { InputSupplier<InputStream> input = ByteStreams.join( ByteStreams.newInputStreamSupplier("[5,6".getBytes(Charsets.UTF_8)), exceptionStreamSupplier(new MalformedChunkCodingException("Bad chunk header"))); assertThrowsEOFException(input.getInput(), Integer.class); }
@Test @SuppressWarnings("unchecked") public void testTruncatedChunkException() throws Exception { InputSupplier<InputStream> input = ByteStreams.join( ByteStreams.newInputStreamSupplier("[5,6".getBytes(Charsets.UTF_8)), exceptionStreamSupplier(new TruncatedChunkException("Truncated chunk ( expected size: 3996; actual size: 1760)"))); assertThrowsEOFException(input.getInput(), Integer.class); }
private InputSupplier<InputStream> exceptionStreamSupplier(final Throwable t) { return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return exceptionStream(t); } }; }
@Override public void put(String apiKey, String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes, @Nullable Duration ttl) throws IOException { checkNotNull(table, "table"); checkNotNull(blobId, "blobId"); checkNotNull(in, "in"); checkNotNull(attributes, "attributes"); try { // Encode the ttl as a URL query parameter Integer ttlSeconds = Ttls.toSeconds(ttl, 0, null); URI uri = _blobStore.clone() .segment(table, blobId) .queryParam("ttl", (ttlSeconds != null) ? new Object[]{ttlSeconds} : new Object[0]) .build(); // Encode the rest of the attributes as request headers EmoResource request = _client.resource(uri); for (Map.Entry<String, String> entry : attributes.entrySet()) { request.header(X_BVA_PREFIX + entry.getKey(), entry.getValue()); } // Upload the object request.type(MediaType.APPLICATION_OCTET_STREAM_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .put(in.getInput()); } catch (EmoClientException e) { throw convertException(e); } }
public void injectIcon(int id, final InputStream iconStream) throws IOException { File f = File.createTempFile("launcher", "ico"); Files.copy(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return iconStream; } }, f); IconResourceInjector iconInjector = new IconResourceInjector(); iconInjector.injectIcon(f, myRoot, "IRD" + id); }
@Converter public static Payload toPayload(final StreamSourceCache cache, Exchange exchange) throws IOException { long contentLength = ByteStreams.length(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return cache.getInputStream(); } }); cache.reset(); InputStreamPayload payload = new InputStreamPayload(cache.getInputStream()); payload.getContentMetadata().setContentLength(contentLength); setContentMetadata(payload, exchange); return payload; }
public static <T> T deserialize(final Persister persister, final InputSupplier<? extends InputStream> inputSupplier, final Class<T> clazz) throws IOException, ClassCastException, ClassNotFoundException { final InputStream input = inputSupplier.getInput(); boolean threw = true; try { final T object = persister.deserialize(input, clazz); threw = false; return object; } finally { Closeables.close(input, threw); } }
public RestRequest(final String method, final String uri, final OAuthClient client, final OAuthToken token, final InputSupplier<? extends InputStream> body, final HttpHeader... headers) { this(method, uri, client, token, body, asList(headers)); }
public RestRequest(final String method, final String uri, final OAuthClient client, final OAuthToken token, final InputSupplier<? extends InputStream> body, final List<HttpHeader> headers) { this.method = method; this.uri = uri; this.client = client; this.token = token; this.headers = headers; this.body = body; }
public void prepareJavaCommand(JavaExecSpec execSpec) { execSpec.setMain("jarjar." + GradleWorkerMain.class.getName()); execSpec.classpath(classPathRegistry.getClassPath("WORKER_MAIN").getAsFiles()); Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager"); if (requestedSecurityManager != null) { execSpec.systemProperty("org.gradle.security.manager", requestedSecurityManager); } execSpec.systemProperty("java.security.manager", "jarjar." + BootstrapSecurityManager.class.getName()); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(bytes)); LOGGER.debug("Writing an application classpath to child process' standard input."); outstr.writeInt(processBuilder.getApplicationClasspath().size()); for (File file : processBuilder.getApplicationClasspath()) { outstr.writeUTF(file.getAbsolutePath()); } outstr.close(); final InputStream originalStdin = execSpec.getStandardInput(); InputStream input = ByteStreams.join(ByteStreams.newInputStreamSupplier(bytes.toByteArray()), new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { return originalStdin; } }).getInput(); execSpec.setStandardInput(input); } catch (IOException e) { throw new UncheckedIOException(e); } }
private void fillMapFromFile(final FileContext fc, FileStatus[] first, Multiset<String> countFirst) throws IOException { for (final FileStatus status : first) { Multiset<String> map = MultiJobTest.toMap(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return fc.open(status.getPath()); } }); countFirst.addAll(map); } }