Java Code Examples for com.google.common.io.ByteStreams

The following examples show how to use com.google.common.io.ByteStreams. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: webarchive-commons   Source File: NIOSeekableLineReader.java    License: 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 2
@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 3
Source Project: BUbiNG   Source File: GZIPArchiveWriterTest.java    License: 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 4
Source Project: google-java-format   Source File: MainTest.java    License: 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 5
Source Project: HeyGirl   Source File: DexBackedDexFile.java    License: 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
@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 7
Source Project: tlaplus   Source File: CloudDistributedTLCJob.java    License: 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 8
Source Project: AppTroy   Source File: DexBackedDexFile.java    License: 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 9
Source Project: OpenYOLO-Android   Source File: IoUtilTest.java    License: 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 10
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 11
Source Project: ChangeSkin   Source File: PluginMessageListener.java    License: 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 12
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 13
Source Project: bazel   Source File: CompatDx.java    License: 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 14
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 15
Source Project: bazel   Source File: DexFileAggregatorTest.java    License: 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 16
Source Project: bazel   Source File: DarwinSandboxedSpawnRunner.java    License: 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 17
Source Project: s3proxy   Source File: NullBlobStoreTest.java    License: 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 18
Source Project: android-chunk-utils   Source File: ResourceString.java    License: 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 19
Source Project: che   Source File: URLFetcher.java    License: 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 20
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 21
Source Project: BUbiNG   Source File: GZIPArchiveWriterTest.java    License: 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 Project: webarchive-commons   Source File: GZIPMemberSeriesTest.java    License: 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 23
Source Project: Parties   Source File: PartiesPacket.java    License: 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 24
Source Project: apkfile   Source File: ResourceString.java    License: 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 25
Source Project: imhotep   Source File: SquallArchiveWriter.java    License: 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 26
Source Project: Indra   Source File: VectorIterator.java    License: 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 27
Source Project: imhotep   Source File: TestMemoryFlamdex.java    License: 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 28
Source Project: buck   Source File: XctoolRunTestsStep.java    License: 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 29
Source Project: selenium   Source File: Contents.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] bytes(Supplier<InputStream> supplier) {
  Require.nonNull("Supplier of input", supplier);

  try (InputStream is = supplier.get();
       ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    ByteStreams.copy(is, bos);
    return bos.toByteArray();
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example 30
Source Project: big-c   Source File: TestTools.java    License: Apache License 2.0 5 votes vote down vote up
private void checkOutput(String[] args, String pattern, PrintStream out,
    Class<?> clazz) {       
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  try {
    PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
    if (out == System.out) {
      System.setOut(new PrintStream(pipeOut));
    } else if (out == System.err) {
      System.setErr(new PrintStream(pipeOut));
    }

    if (clazz == DelegationTokenFetcher.class) {
      expectDelegationTokenFetcherExit(args);
    } else if (clazz == JMXGet.class) {
      expectJMXGetExit(args);
    } else if (clazz == DFSAdmin.class) {
      expectDfsAdminPrint(args);
    }
    pipeOut.close();
    ByteStreams.copy(pipeIn, outBytes);      
    pipeIn.close();
    assertTrue(new String(outBytes.toByteArray()).contains(pattern));            
  } catch (Exception ex) {
    fail("checkOutput error " + ex);
  }
}