List of usage examples for org.eclipse.jgit.util IO readWholeStream
public static ByteBuffer readWholeStream(InputStream in, int sizeHint) throws IOException
From source file:com.google.gerrit.acceptance.HttpResponse.java
License:Apache License
public String getEntityContent() throws IOException { Preconditions.checkNotNull(response, "Response is not initialized."); Preconditions.checkNotNull(response.getEntity(), "Response.Entity is not initialized."); ByteBuffer buf = IO.readWholeStream(response.getEntity().getContent(), 1024); return RawParseUtils.decode(buf.array(), buf.arrayOffset(), buf.limit()).trim(); }
From source file:com.google.gerrit.pgm.init.InitUtil.java
License:Apache License
static void extract(final File dst, final Class<?> sibling, final String name) throws IOException { final InputStream in = open(sibling, name); if (in != null) { ByteBuffer buf = IO.readWholeStream(in, 8192); copy(dst, buf);/*from w w w . j a va 2 s . c o m*/ } }
From source file:com.google.gerrit.pgm.ProtoGen.java
License:Apache License
@Override public int run() throws Exception { LockFile lock = new LockFile(file.getAbsoluteFile(), FS.DETECTED); if (!lock.lock()) { throw die("Cannot lock " + file); }//from w w w . jav a 2 s . c om try { JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class); try (OutputStream o = lock.getOutputStream(); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, "UTF-8")))) { String header; try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) { ByteBuffer buf = IO.readWholeStream(in, 1024); int ptr = buf.arrayOffset() + buf.position(); int len = buf.remaining(); header = new String(buf.array(), ptr, len, "UTF-8"); } String version = com.google.gerrit.common.Version.getVersion(); out.write(header.replace("@@VERSION@@", version)); jsm.generateProto(out); out.flush(); } if (!lock.commit()) { throw die("Could not write to " + file); } } finally { lock.unlock(); } System.out.println("Created " + file.getPath()); return 0; }
From source file:com.google.gerrit.sshd.commands.BaseTestPrologCommand.java
License:Apache License
@Override protected final void run() throws UnloggedFailure { try {/*from w w w. jav a 2 s .c om*/ RevisionResource revision = revisions.parse( changes.parse(TopLevelResource.INSTANCE, IdString.fromUrl(changeId)), IdString.fromUrl("current")); if (useStdin) { ByteBuffer buf = IO.readWholeStream(in, 4096); input.rule = RawParseUtils.decode(buf.array(), buf.arrayOffset(), buf.limit()); } Object result = createView().apply(revision, input); OutputFormat.JSON.newGson().toJson(result, stdout); stdout.print('\n'); } catch (Exception e) { throw new UnloggedFailure("Processing of prolog script failed: " + e); } }
From source file:com.googlesource.gerrit.plugins.gitblit.StaticResourcesServlet.java
License:Apache License
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Extract the filename from the request String resourcePath = request.getPathInfo(); // Is already relative to /static! if (request.getRequestURI().endsWith("/clippy.swf")) { resourcePath = "/clippy.swf"; }//from w w w . ja v a 2 s . c o m if (Strings.isNullOrEmpty(resourcePath)) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Must not contain any navigation String[] segments = resourcePath.substring(1).split("/"); for (String segment : segments) { if (segment.equals("..")) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "Invalid path"); return; } } String fileName = segments[segments.length - 1]; if (Strings.isNullOrEmpty(fileName)) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Restrict to the few known subdirectories. if (segments.length > 1 && !ALLOWED_SUBDIRECTORIES.contains(segments[0])) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } else if (segments.length == 1 && !ALLOWED_FILE_NAMES.matcher(fileName).matches()) { // Only allow the known filetypes: we have only png, css, js, swf, and gitblit.properties. response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // We just happen to know that all our static data files are small, so it's OK to read them fully into memory byte[] bytes = null; try (InputStream data = getClass().getResourceAsStream(resourcePath)) { if (data == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } bytes = IO.readWholeStream(data, 0).array(); } if (bytes == null) { // Should not occur since we don't catch the possible IOException above. Nevertheless, let's be paranoid. bytes = new byte[0]; } String contentType = null; // Compare https://gerrit-review.googlesource.com/#/c/67000/ if (fileName.toLowerCase().endsWith(".js")) { contentType = "application/javascript"; } else if (fileName.toLowerCase().endsWith(".css")) { contentType = "text/css"; } else { MimeType mimeType = mimeDetector.getMimeType(fileName, bytes); contentType = mimeType != null ? mimeType.toString() : "application/octet-stream"; } response.setContentType(contentType); long lastModified = getLastModified(request); if (lastModified > 0) { response.setDateHeader("Last-Modified", lastModified); } response.setHeader("Content-Length", Integer.toString(bytes.length)); try (OutputStream out = response.getOutputStream()) { out.write(bytes, 0, bytes.length); } }
From source file:org.eclipse.egit.core.internal.storage.IndexFileRevisionTest.java
License:Open Source License
private static String readContents(IndexFileRevision revision) throws Exception { IStorage storage = revision.getStorage(null); InputStream in = storage.getContents(); ByteBuffer buffer = IO.readWholeStream(in, 10); return new String(buffer.array(), 0, buffer.limit(), "UTF-8"); }
From source file:org.eclipse.egit.ui.internal.commit.RepositoryCommitNote.java
License:Open Source License
/** * Get note text. This method open and read the note blob each time it is * called.//from w w w .jav a2 s . com * * @return note text or empty string if lookup failed. */ public String getNoteText() { try { ObjectLoader loader = commit.getRepository().open(note.getData(), Constants.OBJ_BLOB); byte[] contents; if (loader.isLarge()) contents = IO.readWholeStream(loader.openStream(), (int) loader.getSize()).array(); else contents = loader.getCachedBytes(); return new String(contents); } catch (IOException e) { Activator.logError("Error loading note text", e); //$NON-NLS-1$ } return ""; //$NON-NLS-1$ }
From source file:org.eclipse.mylyn.reviews.r4e.core.utils.IOUtils.java
License:Open Source License
/** * Used when the size of the stream is unknown, the caller shall close the input stream * //from w w w . jav a 2 s . c o m * @param in * @return * @throws IOException */ public static byte[] readFully(InputStream in) throws IOException { return IO.readWholeStream(in, 1).array(); }