com.google.common.io.ByteStreams Java Examples

The following examples show how to use com.google.common.io.ByteStreams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServletResponseResultWriter.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
protected void write(int status, Map<String, String> headers, Object content) throws IOException {
  // write response status code
  servletResponse.setStatus(status);

  // write response headers
  if (headers != null) {
    for (Map.Entry<String, String> entry : headers.entrySet()) {
      servletResponse.addHeader(entry.getKey(), entry.getValue());
    }
  }

  // write response body
  if (content != null) {
    servletResponse.setContentType(SystemService.MIME_JSON);
    if (addContentLength) {
      CountingOutputStream counter = new CountingOutputStream(ByteStreams.nullOutputStream());
      objectWriter.writeValue(counter, content);
      servletResponse.setContentLength((int) counter.getCount());
    }
    objectWriter.writeValue(servletResponse.getOutputStream(), content);
  }
}
 
Example #2
Source File: MainTest.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
@Test
public void exitIfChangedStdin() throws Exception {
  Path path = testFolder.newFile("Test.java").toPath();
  Files.write(path, "class Test {\n}\n".getBytes(UTF_8));
  Process process =
      new ProcessBuilder(
              ImmutableList.of(
                  Paths.get(System.getProperty("java.home")).resolve("bin/java").toString(),
                  "-cp",
                  System.getProperty("java.class.path"),
                  Main.class.getName(),
                  "-n",
                  "--set-exit-if-changed",
                  "-"))
          .redirectInput(path.toFile())
          .redirectError(Redirect.PIPE)
          .redirectOutput(Redirect.PIPE)
          .start();
  process.waitFor();
  String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8);
  String out = new String(ByteStreams.toByteArray(process.getInputStream()), UTF_8);
  assertThat(err).isEmpty();
  assertThat(out).isEqualTo("<stdin>" + System.lineSeparator());
  assertThat(process.exitValue()).isEqualTo(1);
}
 
Example #3
Source File: SquallArchiveWriter.java    From imhotep with Apache License 2.0 6 votes vote down vote up
private void internalAppendFile(FSDataOutputStream os, File file, List<String> parentDirectories, SquallArchiveCompressor compressor, String archiveFilename) throws IOException {
    final String baseFilename = file.getName().replaceAll("\\s+", "_");
    final String filename = makeFilename(parentDirectories, baseFilename);
    final long size = file.length();
    final long timestamp = file.lastModified();
    final long startOffset = os.getPos();

    final InputStream is = new BufferedInputStream(new FileInputStream(file));
    final String checksum;
    try {
        final CompressionOutputStream cos = compressor.newOutputStream(os);
        final DigestOutputStream dos = new DigestOutputStream(cos, ArchiveUtils.getMD5Digest());
        ByteStreams.copy(is, dos);
        checksum = ArchiveUtils.toHex(dos.getMessageDigest().digest());
        cos.finish();
    } finally {
        is.close();
    }

    pendingMetadataWrites.add(new FileMetadata(filename, size, timestamp, checksum, startOffset, compressor, archiveFilename));
}
 
Example #4
Source File: LobbyCommand.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void execute(CommandSender commandSender, String[] strings) {
    ServerInfo serverInfo = this.getLobby();
    if (serverInfo != null && commandSender instanceof ProxiedPlayer) {
        ProxiedPlayer player = (ProxiedPlayer) commandSender;
        if (!player.getServer().getInfo().getName().equals(serverInfo.getName())) {
            player.connect(getLobby());
        } else {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF("HubBasics");
            out.writeUTF("Lobby");
            player.sendData("BungeeCord", out.toByteArray());
        }
    } else if (serverInfo == null) {
        commandSender.sendMessage(new TextComponent(Messages.get(commandSender, "LOBBY_NOT_DEFINED")));
    } else {
        commandSender.sendMessage(new TextComponent(Messages.get(commandSender, "COMMAND_PLAYER")));
    }
}
 
Example #5
Source File: DexBackedDexFile.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #6
Source File: LineBotCallbackRequestParserTest.java    From line-bot-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallRequest() throws Exception {
    final InputStream resource = getClass().getClassLoader().getResourceAsStream("callback-request.json");
    final byte[] requestBody = ByteStreams.toByteArray(resource);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("X-Line-Signature", "SSSSIGNATURE");
    request.setContent(requestBody);

    doReturn(true).when(lineSignatureValidator).validateSignature(requestBody, "SSSSIGNATURE");

    final CallbackRequest callbackRequest = lineBotCallbackRequestParser.handle(request);

    assertThat(callbackRequest).isNotNull();

    final List<Event> result = callbackRequest.getEvents();

    final MessageEvent messageEvent = (MessageEvent) result.get(0);
    final TextMessageContent text = (TextMessageContent) messageEvent.getMessage();
    assertThat(text.getText()).isEqualTo("Hello, world");

    final String followedUserId = messageEvent.getSource().getUserId();
    assertThat(followedUserId).isEqualTo("u206d25c2ea6bd87c17655609a1c37cb8");
    assertThat(messageEvent.getTimestamp()).isEqualTo(Instant.parse("2016-05-07T13:57:59.859Z"));
}
 
Example #7
Source File: NIOSeekableLineReader.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
public InputStream doSeekLoad(long offset, int maxLength) throws IOException {
		
		if ((type == NIOType.MMAP) && (maxLength >= 0)) {
			ByteBuffer mapBuff = fc.map(MapMode.READ_ONLY, offset, maxLength);
			bbis = new ByteBufferBackedInputStream(mapBuff, offset);	
			return ByteStreams.limit(bbis, maxLength);
			
		} else {
			fcis = new FileChannelInputStream(fc, offset, maxLength);
			return fcis;
//			if (maxLength > 0) {
//				return new LimitInputStream(fcis, maxLength);
//			} else {
//				return fcis;
//			}
		}
    }
 
Example #8
Source File: ResourceString.java    From android-chunk-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
 * Strings are prefixed by 2 values. The first is the number of characters in the string.
 * The second is the encoding length (number of bytes in the string).
 *
 * <p>Here's an example UTF-8-encoded string of ab©:
 * <pre>03 04 61 62 C2 A9 00</pre>
 *
 * @param str The string to be encoded.
 * @param type The encoding type that the {@link ResourceString} should be encoded in.
 * @return The encoded string.
 */
public static byte[] encodeString(String str, Type type) {
  byte[] bytes = str.getBytes(type.charset());
  // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
  ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
  encodeLength(output, str.length(), type);
  if (type == Type.UTF8) {  // Only UTF-8 strings have the encoding length.
    encodeLength(output, bytes.length, type);
  }
  output.write(bytes);
  // NULL-terminate the string
  if (type == Type.UTF8) {
    output.write(0);
  } else {
    output.writeShort(0);
  }
  return output.toByteArray();
}
 
Example #9
Source File: NullBlobStoreTest.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateBlobGetBlob() throws Exception {
    String blobName = createRandomBlobName();
    Blob blob = makeBlob(nullBlobStore, blobName);
    nullBlobStore.putBlob(containerName, blob);

    blob = nullBlobStore.getBlob(containerName, blobName);
    validateBlobMetadata(blob.getMetadata());

    // content differs, only compare length
    try (InputStream actual = blob.getPayload().openStream();
            InputStream expected = BYTE_SOURCE.openStream()) {
        long actualLength = ByteStreams.copy(actual,
                ByteStreams.nullOutputStream());
        long expectedLength = ByteStreams.copy(expected,
                ByteStreams.nullOutputStream());
        assertThat(actualLength).isEqualTo(expectedLength);
    }

    PageSet<? extends StorageMetadata> pageSet = nullBlobStore.list(
            containerName);
    assertThat(pageSet).hasSize(1);
    StorageMetadata sm = pageSet.iterator().next();
    assertThat(sm.getName()).isEqualTo(blobName);
    assertThat(sm.getSize()).isEqualTo(0);
}
 
Example #10
Source File: VectorIterator.java    From Indra with MIT License 6 votes vote down vote up
private void setCurrentContent() throws IOException {
    if (numberOfVectors > 0) {
        numberOfVectors--;

        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        byte b;
        while ((b = input.readByte()) != ' ') {
            out.writeByte(b);
        }

        String word = new String(out.toByteArray(), StandardCharsets.UTF_8);
        if (this.sparse) {
            this.currentVector = new TermVector(true, word, readSparseVector(this.dimensions));
        } else {
            this.currentVector = new TermVector(false, word, readDenseVector(this.dimensions));
        }

    } else {
        this.currentVector = null;
    }
}
 
Example #11
Source File: TestMemoryFlamdex.java    From imhotep with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntMaxValue() throws IOException {
    MemoryFlamdex fdx = new MemoryFlamdex().setNumDocs(1);
    IntFieldWriter ifw = fdx.getIntFieldWriter("if1");
    ifw.nextTerm(Integer.MAX_VALUE);
    ifw.nextDoc(0);
    ifw.close();
    fdx.close();

    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    fdx.write(out);

    MemoryFlamdex fdx2 = new MemoryFlamdex();
    fdx2.readFields(ByteStreams.newDataInput(out.toByteArray()));

    innerTestIntMaxValue(fdx2);

    innerTestIntMaxValue(MemoryFlamdex.streamer(ByteStreams.newDataInput(out.toByteArray())));
}
 
Example #12
Source File: XctoolRunTestsStep.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  try (OutputStream outputStream = filesystem.newFileOutputStream(outputPath);
      TeeInputStream stdoutWrapperStream =
          new TeeInputStream(launchedProcess.getStdout(), outputStream)) {
    if (stdoutReadingCallback.isPresent()) {
      // The caller is responsible for reading all the data, which TeeInputStream will
      // copy to outputStream.
      stdoutReadingCallback.get().readStdout(stdoutWrapperStream);
    } else {
      // Nobody's going to read from stdoutWrapperStream, so close it and copy
      // the process's stdout to outputPath directly.
      stdoutWrapperStream.close();
      ByteStreams.copy(launchedProcess.getStdout(), outputStream);
    }
  } catch (IOException e) {
    exception = Optional.of(e);
  }
}
 
Example #13
Source File: GZIPArchiveWriterTest.java    From BUbiNG with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, JSAPException {

	SimpleJSAP jsap = new SimpleJSAP(GZIPArchiveReader.class.getName(), "Writes some random records on disk.",
		new Parameter[] {
			new Switch("fully", 'f', "fully",
				"Whether to read fully the record (and do a minimal cosnsistency check)."),
			new UnflaggedOption("path", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, JSAP.NOT_GREEDY,
				"The path to read from."), });

	JSAPResult jsapResult = jsap.parse(args);
	if (jsap.messagePrinted())
	    System.exit(1);

	final boolean fully = jsapResult.getBoolean("fully");
	GZIPArchiveReader gzar = new GZIPArchiveReader(new FileInputStream(jsapResult.getString("path")));
	for (;;) {
	    ReadEntry e = gzar.getEntry();
	    if (e == null)
		break;
	    InputStream inflater = e.lazyInflater.get();
	    if (fully)
		ByteStreams.toByteArray(inflater);
	    e.lazyInflater.consume();
	    System.out.println(e);
	}
    }
 
Example #14
Source File: ResourceString.java    From apkfile with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
 * Strings are prefixed by 2 values. The first is the number of characters in the string.
 * The second is the encoding length (number of bytes in the string).
 *
 * <p>Here's an example UTF-8-encoded string of ab©:
 * <pre>03 04 61 62 C2 A9 00</pre>
 *
 * @param str  The string to be encoded.
 * @param type The encoding type that the {@link ResourceString} should be encoded in.
 * @return The encoded string.
 */
public static byte[] encodeString(String str, Type type) {
    byte[] bytes = str.getBytes(type.charset());
    // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
    ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
    encodeLength(output, str.length(), type);
    if (type == Type.UTF8) {  // Only UTF-8 strings have the encoding length.
        encodeLength(output, bytes.length, type);
    }
    output.write(bytes);
    // NULL-terminate the string
    if (type == Type.UTF8) {
        output.write(0);
    } else {
        output.writeShort(0);
    }
    return output.toByteArray();
}
 
Example #15
Source File: PartiesPacket.java    From Parties with GNU Affero General Public License v3.0 6 votes vote down vote up
public static PartiesPacket read(ADPPlugin plugin, byte[] bytes) {
	PartiesPacket ret = null;
	try {
		ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
		String foundVersion = input.readUTF();
		
		if (foundVersion.equals(plugin.getVersion())) {
			PartiesPacket packet = new PartiesPacket(foundVersion);
			packet.type = PacketType.valueOf(input.readUTF());
			packet.partyName = input.readUTF();
			packet.playerUuid = UUID.fromString(input.readUTF());
			packet.payload = input.readUTF();
			ret = packet;
		} else {
			plugin.getLoggerManager().printError(Constants.DEBUG_LOG_MESSAGING_FAILED_VERSION
					.replace("{current}", plugin.getVersion())
					.replace("{version}", foundVersion));
		}
	} catch (Exception ex) {
		plugin.getLoggerManager().printError(Constants.DEBUG_LOG_MESSAGING_FAILED_READ
				.replace("{message}", ex.getMessage()));
	}
	return ret;
}
 
Example #16
Source File: GZIPMemberSeriesTest.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
public void testDouble() throws IndexOutOfBoundsException, FileNotFoundException, IOException {

		InputStream is = getClass().getResourceAsStream("abcd.gz");
		byte abcd[] = ByteStreams.toByteArray(is);
		byte abcd2[] = ByteOp.append(abcd, abcd);
		ByteArrayInputStream bais = new ByteArrayInputStream(abcd2);
		Stream stream = new SimpleStream(bais);
		GZIPMemberSeries s = new GZIPMemberSeries(stream, "unk", 0);
		GZIPSeriesMember m = s.getNextMember();
		assertNotNull(m);
		assertEquals(0,m.getRecordStartOffset());
		assertEquals(10,m.getCompressedBytesRead());
		TestUtils.assertStreamEquals(m,"abcd".getBytes(IAUtils.UTF8));

		m = s.getNextMember();
		assertNotNull(m);
		assertEquals(abcd.length,m.getRecordStartOffset());
		assertEquals(10,m.getCompressedBytesRead());
		TestUtils.assertStreamEquals(m,"abcd".getBytes(IAUtils.UTF8));
		assertNull(s.getNextMember());
	}
 
Example #17
Source File: CloudDistributedTLCJob.java    From tlaplus with MIT License 6 votes vote down vote up
@Override
public void getJavaFlightRecording() throws IOException {
	// Get Java Flight Recording from remote machine and save if to a local file in
	// the current working directory. We call "cat" because sftclient#get fails with
	// the old net.schmizz.sshj and an update to the newer com.hierynomus seems 
	// awful lot of work.
	final ExecChannel channel = sshClient.execChannel("cat /mnt/tlc/tlc.jfr");
	final InputStream output = channel.getOutput();
	final String cwd = Paths.get(".").toAbsolutePath().normalize().toString() + File.separator;
	final File jfr = new File(cwd + "tlc-" + System.currentTimeMillis() + ".jfr");
	ByteStreams.copy(output, new FileOutputStream(jfr));
	if (jfr.length() == 0) {
		System.err.println("Received empty Java Flight recording. Not creating tlc.jfr file");
		jfr.delete();
	}
}
 
Example #18
Source File: DexBackedDexFile.java    From AppTroy with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #19
Source File: IoUtilTest.java    From OpenYOLO-Android with Apache License 2.0 6 votes vote down vote up
private byte[] writeAndRead(byte[] data, byte[] key) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CipherOutputStream outStream = IoUtil.encryptTo(baos, key);
    outStream.write(data);
    outStream.close();

    byte[] cipherText = baos.toByteArray();

    ByteArrayInputStream bais = new ByteArrayInputStream(cipherText);
    CipherInputStream inStream = IoUtil.decryptFrom(bais, key);

    byte[] result = ByteStreams.toByteArray(inStream);

    inStream.close();
    return result;
}
 
Example #20
Source File: ThesisSubmissionDA.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ActionForward uploadAbstract(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ThesisFileBean bean = getRenderedObject();
    RenderUtils.invalidateViewState();

    if (bean != null && bean.getFile() != null) {
        byte[] bytes = ByteStreams.toByteArray(bean.getFile());
        try {
            CreateThesisAbstractFile.runCreateThesisAbstractFile(getThesis(request), bytes, bean.getSimpleFileName(), null,
                    null, null);
        } catch (DomainException e) {
            addActionMessage("error", request, e.getKey(), e.getArgs());
            return prepareUploadAbstract(mapping, actionForm, request, response);
        }
    }

    return prepareThesisSubmission(mapping, actionForm, request, response);
}
 
Example #21
Source File: GZIPArchiveWriterTest.java    From BUbiNG with Apache License 2.0 6 votes vote down vote up
@Test
   public void readWrite() throws IOException {
List<byte[]> contents = writeArchive(ARCHIVE_PATH, ARCHIVE_SIZE, false);
FileInputStream fis = new FileInputStream(ARCHIVE_PATH);
GZIPArchiveReader gzar = new GZIPArchiveReader(fis);
GZIPArchive.ReadEntry re;
for (byte[] expected: contents) {
    re = gzar.getEntry();
    if (re == null) break;
    LazyInflater lin = re.lazyInflater;
    final byte[] actual = ByteStreams.toByteArray(lin.get());
    assertArrayEquals(expected, actual);
    lin.consume();
}
fis.close();
   }
 
Example #22
Source File: DarwinSandboxedSpawnRunner.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static boolean computeIsSupported() {
  List<String> args = new ArrayList<>();
  args.add(sandboxExecBinary);
  args.add("-p");
  args.add("(version 1) (allow default)");
  args.add("/usr/bin/true");

  ImmutableMap<String, String> env = ImmutableMap.of();
  File cwd = new File("/usr/bin");

  Command cmd = new Command(args.toArray(new String[0]), env, cwd);
  try {
    cmd.execute(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream());
  } catch (CommandException e) {
    return false;
  }

  return true;
}
 
Example #23
Source File: PluginMessageListener.java    From ChangeSkin with MIT License 6 votes vote down vote up
@EventHandler
public void onPluginMessage(PluginMessageEvent messageEvent) {
    String channel = messageEvent.getTag();
    if (messageEvent.isCancelled() || !channel.startsWith(plugin.getName().toLowerCase())) {
        return;
    }

    ByteArrayDataInput dataInput = ByteStreams.newDataInput(messageEvent.getData());

    ProxiedPlayer invoker = (ProxiedPlayer) messageEvent.getReceiver();
    if (channel.equals(permissionResultChannel)) {
        PermResultMessage message = new PermResultMessage();
        message.readFrom(dataInput);
        if (message.isAllowed()) {
            onPermissionSuccess(message, invoker);
        } else {
            plugin.sendMessage(invoker, "no-permission");
        }
    } else if (channel.equals(forwardCommandChannel)) {
        onCommandForward(invoker, dataInput);
    }
}
 
Example #24
Source File: SkyWarsReloaded.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
public void sendSWRMessage(Player player, String server, ArrayList<String> messages) {
	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	out.writeUTF("Forward"); 
	out.writeUTF(server);
	out.writeUTF("SWRMessaging");

	ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
	DataOutputStream msgout = new DataOutputStream(msgbytes);
	try {
		for (String msg: messages) {
			msgout.writeUTF(msg);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	out.writeShort(msgbytes.toByteArray().length);
	out.write(msgbytes.toByteArray());
	player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
}
 
Example #25
Source File: CompatDx.java    From bazel with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("JdkObsolete") // Uses Enumeration by design.
private void writeInputClassesToArchive(DiagnosticsHandler handler) throws IOException {
  // For each input archive file, add all class files within.
  for (Path input : inputs) {
    if (FileUtils.isArchive(input)) {
      try (ZipFile zipFile = new ZipFile(input.toFile(), UTF_8)) {
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
          ZipEntry entry = entries.nextElement();
          if (FileUtils.isClassFile(entry.getName())) {
            try (InputStream entryStream = zipFile.getInputStream(entry)) {
              byte[] bytes = ByteStreams.toByteArray(entryStream);
              writeClassFile(entry.getName(), ByteDataView.of(bytes), handler);
            }
          }
        }
      }
    }
  }
}
 
Example #26
Source File: SkyWarsReloaded.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
public void sendSWRMessage(Player player, String server, ArrayList<String> messages) {
	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	out.writeUTF("Forward"); 
	out.writeUTF(server);
	out.writeUTF("SWRMessaging");

	ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
	DataOutputStream msgout = new DataOutputStream(msgbytes);
	try {
		for (String msg: messages) {
			msgout.writeUTF(msg);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	out.writeShort(msgbytes.toByteArray().length);
	out.write(msgbytes.toByteArray());
	player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
}
 
Example #27
Source File: DexFileAggregatorTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultidex_underLimitWritesOneShard() throws Exception {
  DexFileAggregator dexer =
      new DexFileAggregator(
          new DxContext(),
          dest,
          newDirectExecutorService(),
          MultidexStrategy.BEST_EFFORT,
          /*forceJumbo=*/ false,
          DEX_LIMIT,
          WASTE,
          DexFileMergerTest.DEX_PREFIX);
  Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
  dexer.add(dex);
  dexer.add(dex2);
  verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class));
  dexer.close();
  verify(dest).addFile(any(ZipEntry.class), written.capture());
  assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2);
}
 
Example #28
Source File: URLFetcher.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Fetch the urlConnection stream by using the urlconnection and return its content To prevent DOS
 * attack, limit the amount of the collected data
 *
 * @param urlConnection the URL connection to fetch
 * @return the content of the file
 * @throws IOException if fetch error occurs
 */
@VisibleForTesting
String fetch(@NotNull URLConnection urlConnection) throws IOException {
  requireNonNull(urlConnection, "urlConnection parameter can't be null");
  final String value;
  try (InputStream inputStream = urlConnection.getInputStream();
      BufferedReader reader =
          new BufferedReader(
              new InputStreamReader(ByteStreams.limit(inputStream, getLimit()), UTF_8))) {
    value = reader.lines().collect(Collectors.joining("\n"));
  } catch (IOException e) {
    // we shouldn't fetch if check is done before
    LOG.debug("Invalid URL", e);
    throw e;
  }
  return value;
}
 
Example #29
Source File: ApplicationArchive.java    From onos with Apache License 2.0 5 votes vote down vote up
private boolean expandZippedApplication(InputStream stream, ApplicationDescription desc)
        throws IOException {
    boolean isSelfContained = false;
    ZipInputStream zis = new ZipInputStream(stream);
    ZipEntry entry;
    File appDir = new File(appsDir, desc.name());
    if (!FilePathValidator.validateFile(appDir, appsDir)) {
        throw new ApplicationException("Application attempting to create files outside the apps directory");
    }
    while ((entry = zis.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            byte[] data = ByteStreams.toByteArray(zis);
            zis.closeEntry();
            if (FilePathValidator.validateZipEntry(entry, appDir)) {
                File file = new File(appDir, entry.getName());
                if (isTopLevel(file)) {
                    createParentDirs(file);
                    write(data, file);
                } else {
                    isSelfContained = true;
                }
            } else {
                throw new ApplicationException("Application Zip archive is attempting to leave application root");
            }
        }
    }
    zis.close();
    return isSelfContained;
}
 
Example #30
Source File: DexBackedOdexFile.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}