/** * @see #writeSignedVarLong(long, DataOutput) */ public static void writeSignedVarInt(int value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarInt((value << 1) ^ (value >> 31), out); }
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readSignedVarLong(DataInput) */ public static int readSignedVarInt(DataInput in) throws IOException { int raw = readUnsignedVarInt(in); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> * Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently * encode signed values. If values are known to be nonnegative, * {@link #writeUnsignedVarLong(long, DataOutput)} should be used. * * @param value value to encode * @param out to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static void writeSignedVarLong(long value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarLong((value << 1) ^ (value >> 63), out); }
@Test public void testDeleteRecursively() throws IOException { File tempDir = getTestTempDir(); assertTrue(tempDir.exists()); File subFile1 = new File(tempDir, "subFile1"); Files.write(SOME_BYTES, subFile1); assertTrue(subFile1.exists()); File subDir1 = new File(tempDir, "subDir1"); subDir1.mkdirs(); assertTrue(subDir1.exists()); File subFile2 = new File(subDir1, "subFile2"); Files.write(SOME_BYTES, subFile2); assertTrue(subFile2.exists()); File subDir2 = new File(subDir1, "subDir2"); subDir2.mkdirs(); assertTrue(subDir2.exists()); IOUtils.deleteRecursively(tempDir); assertFalse(tempDir.exists()); assertFalse(subFile1.exists()); assertFalse(subDir1.exists()); assertFalse(subFile2.exists()); assertFalse(subDir2.exists()); }
/** * @see #buildGZIPWriter(OutputStream) */ public static Writer buildGZIPWriter(File file) throws IOException { return buildGZIPWriter(new FileOutputStream(file, false)); }
@Override public synchronized void close() { if (!closed) { closed = true; if (tomcat != null) { try { tomcat.stop(); tomcat.destroy(); } catch (LifecycleException le) { log.warn("Unexpected error while stopping", le); } if (!IOUtils.deleteRecursively(noSuchBaseDir)) { log.info("Could not delete {}", noSuchBaseDir); } } } }
@Test public void testCopyStream() throws IOException { File tempDir = getTestTempDir(); File subFile1 = new File(tempDir, "subFile1"); Files.write(SOME_BYTES, subFile1); File subFile2 = new File(tempDir, "subFile2"); IOUtils.copyURLToFile(subFile1.toURI().toURL(), subFile2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Files.copy(subFile2, baos); assertArrayEquals(SOME_BYTES, baos.toByteArray()); }
private static void appendCommonQueryParams(int howMany, boolean considerKnownItems, String[] rescorerParams, StringBuilder urlPath) { urlPath.append("?howMany=").append(howMany); if (considerKnownItems) { urlPath.append("&considerKnownItems=true"); } if (rescorerParams != null) { for (String rescorerParam : rescorerParams) { urlPath.append("&rescorerParams=").append(IOUtils.urlEncode(rescorerParam)); } } }
/** * @param file file, possibly compressed, to open * @return {@link Reader} on uncompressed contents * @throws IOException if the stream can't be opened or is invalid or can't be read * @see #openMaybeDecompressing(File) */ public static Reader openReaderMaybeDecompressing(File file) throws IOException { return new InputStreamReader(openMaybeDecompressing(file), Charsets.UTF_8); }
/** * @see #buildGZIPOutputStream(OutputStream) */ public static GZIPOutputStream buildGZIPOutputStream(File file) throws IOException { return buildGZIPOutputStream(new FileOutputStream(file)); }
/** * @param in to read bytes from * @return decode value * @throws IOException if {@link DataInput} throws {@link IOException} * @throws IllegalArgumentException if variable-length value does not terminate * after 9 bytes have been read * @see #writeSignedVarLong(long, DataOutput) */ public static long readSignedVarLong(DataInput in) throws IOException { long raw = readUnsignedVarLong(in); // This undoes the trick in writeSignedVarLong() long temp = (((raw << 63) >> 63) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1L << 63)); }
/** * @param url URL whose contents are to be read and copied * @param file file to write contents to * @throws IOException if the URL can't be read or the file can't be written */ public static void copyURLToFile(URL url, File file) throws IOException { InputStream in = url.openStream(); try { copyStreamToFile(in, file); } finally { in.close(); } }
/** * @param f file to read {@code GenerationSerializer} from * @return {@link Generation} it serializes */ public static Generation readGeneration(File f) throws IOException { return IOUtils.readObjectFromFile(f, GenerationSerializer.class).getGeneration(); }
/** * @param generation {@link Generation} to serialize * @param f file to serialize a {@code GenerationSerializer} to */ public static void writeGeneration(Generation generation, File f) throws IOException { IOUtils.writeObjectToFile(f, new GenerationSerializer(generation)); }
private static void consumeIDs(URLConnection connection, FastIDSet result) throws IOException { BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { String line; while ((line = reader.readLine()) != null) { result.add(Long.parseLong(line)); } } finally { reader.close(); } }
@After public void tearDown() throws Exception { IOUtils.deleteRecursively(testTempDir); }
/** * Like {@link #setItemTag(String, long, float)}, but allows caller to specify the user for * which the request is being made. This information does not directly affect the computation, * but affects <em>routing</em> of the request in a distributed context. This is always recommended * when there is a user in whose context the request is being made, as it will ensure that the * request can take into account all the latest information from the user, including a very new * item like {@code itemID}. */ public void setItemTag(String tag, long itemID, float value, Long contextUserID) throws TasteException { Preconditions.checkNotNull(tag); Preconditions.checkArgument(!tag.isEmpty()); long idToPartitionOn = contextUserID == null ? itemID : contextUserID; doSetOrRemove("/tag/item/" + itemID + '/' + IOUtils.urlEncode(tag), idToPartitionOn, value, true); }
/** * @return object of type T that was serialized into the given file */ public static <T extends Serializable> T readObjectFromFile(File f, Class<T> clazz) throws IOException { ObjectInputStream in = new ObjectInputStream(openMaybeDecompressing(f)); try { @SuppressWarnings("unchecked") T result = (T) in.readObject(); return result; } catch (ClassNotFoundException cnfe) { throw new IllegalStateException(cnfe); } finally { in.close(); } }
/** * @param delegate {@link OutputStream} to wrap * @return the result of {@link #buildGZIPOutputStream(OutputStream)} as a {@link Writer} that encodes * using UTF-8 encoding */ public static Writer buildGZIPWriter(OutputStream delegate) throws IOException { return new OutputStreamWriter(buildGZIPOutputStream(delegate), Charsets.UTF_8); }
@Override public void setUserTag(long userID, String tag, float value) throws TasteException { Preconditions.checkNotNull(tag); Preconditions.checkArgument(!tag.isEmpty()); doSetOrRemove("/tag/user/" + userID + '/' + IOUtils.urlEncode(tag), userID, value, true); }