/** {@inheritDoc} */ @Override public void readFully(byte[] b, int off, int len) throws IOException { IO.readFully(fd, b, off, len); }
/** * Create a new sequence from a file. * <p> * The entire file contents are used. * * @param file * the text file. * @throws java.io.IOException * if Exceptions occur while reading the file */ public RawText(File file) throws IOException { this(IO.readFully(file)); }
/** * Read an entire local file into memory as a byte array. * * @param path * location of the file to read. * @return complete contents of the requested local file. * @throws java.io.FileNotFoundException * the file does not exist. * @throws java.io.IOException * the file exists, but its contents cannot be read. */ public static final byte[] readFully(File path) throws FileNotFoundException, IOException { return IO.readFully(path, Integer.MAX_VALUE); }
private static byte[] toByteArray(InputStream source, int upperSizeLimit) throws IOException { byte[] buffer = new byte[upperSizeLimit]; try { int read = IO.readFully(source, buffer, 0); if (read == upperSizeLimit) return buffer; else { byte[] copy = new byte[read]; System.arraycopy(buffer, 0, copy, 0, read); return copy; } } finally { source.close(); } } }
private byte[] inflateAndReturn(Source src, long inflatedSize) throws IOException { final byte[] dst = new byte[(int) inflatedSize]; try (InputStream inf = inflate(src, inflatedSize)) { IO.readFully(inf, dst, 0, dst.length); } return dst; }
private String readString(int len) throws IOException { final byte[] raw = new byte[len]; IO.readFully(rawIn, raw, 0, len); return RawParseUtils.decode(UTF_8, raw, 0, len); } }
byte[] toArray() throws IOException { try { if (length >= 0) { final byte[] r = new byte[(int) length]; IO.readFully(in, r, 0, r.length); return r; } final ByteArrayOutputStream r = new ByteArrayOutputStream(); final byte[] buf = new byte[2048]; int n; while ((n = in.read(buf)) >= 0) r.write(buf, 0, n); return r.toByteArray(); } finally { in.close(); } } }
private static String readFirstLine(File head) { try { final byte[] buf = IO.readFully(head, 4096); int n = buf.length; if (n == 0) return null; if (buf[n - 1] == '\n') n--; return RawParseUtils.decode(buf, 0, n); } catch (IOException e) { return null; } }
/** * Read a file from the git directory. * * @param filename * @return the raw contents or {@code null} if the file doesn't exist or is * empty * @throws IOException */ private byte[] readGitDirectoryFile(String filename) throws IOException { File file = new File(getDirectory(), filename); try { byte[] raw = IO.readFully(file); return raw.length > 0 ? raw : null; } catch (FileNotFoundException notFound) { if (file.exists()) { throw notFound; } return null; } }
private static String readFile(File directory, String fileName) throws IOException { byte[] content = IO.readFully(new File(directory, fileName)); // strip off the last LF int end = RawParseUtils.prevLF(content, content.length); return RawParseUtils.decode(content, 0, end + 1); }
private PersonIdent parseAuthor() throws IOException { File authorScriptFile = rebaseState.getFile(AUTHOR_SCRIPT); byte[] raw; try { raw = IO.readFully(authorScriptFile); } catch (FileNotFoundException notFound) { if (authorScriptFile.exists()) { throw notFound; } return null; } return parseAuthor(raw); }
@Override public byte[] toByteArray() throws IOException { if (onDiskFile == null) { return super.toByteArray(); } final long len = length(); if (Integer.MAX_VALUE < len) throw new OutOfMemoryError(JGitText.get().lengthExceedsMaximumArraySize); final byte[] out = new byte[(int) len]; try (FileInputStream in = new FileInputStream(onDiskFile)) { IO.readFully(in, out, 0, (int) len); } return out; }
/** {@inheritDoc} */ @Override @Nullable public String getGitwebDescription() throws IOException { String d; try { d = RawParseUtils.decode(IO.readFully(descriptionFile())); } catch (FileNotFoundException err) { return null; } if (d != null) { d = d.trim(); if (d.isEmpty() || UNNAMED.equals(d)) { return null; } } return d; }
/** * Read a non-null {@link ObjectId} from the stream. * * @param in * the input stream * @return the object id; never null * @throws IOException * there was an error reading the stream * @since 4.11 */ @NonNull public static ObjectId readWithoutMarker(InputStream in) throws IOException { final byte[] b = new byte[OBJECT_ID_LENGTH]; IO.readFully(in, b, 0, OBJECT_ID_LENGTH); return ObjectId.fromRaw(b); }
/** {@inheritDoc} */ @Override public List<ReflogEntry> getReverseEntries(int max) throws IOException { final byte[] log; try { log = IO.readFully(logName); } catch (FileNotFoundException e) { if (logName.exists()) { throw e; } return Collections.emptyList(); } int rs = RawParseUtils.prevLF(log, log.length); List<ReflogEntry> ret = new ArrayList<>(); while (rs >= 0 && max-- > 0) { rs = RawParseUtils.prevLF(log, rs); ReflogEntry entry = new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2); ret.add(entry); } return ret; } }
/** {@inheritDoc} */ @Override public ReflogEntry getReverseEntry(int number) throws IOException { if (number < 0) throw new IllegalArgumentException(); final byte[] log; try { log = IO.readFully(logName); } catch (FileNotFoundException e) { if (logName.exists()) { throw e; } return null; } int rs = RawParseUtils.prevLF(log, log.length); int current = 0; while (rs >= 0) { rs = RawParseUtils.prevLF(log, rs); if (number == current) return new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2); current++; } return null; }
private static File getSymRef(File workTree, File dotGit, FS fs) throws IOException { byte[] content = IO.readFully(dotGit); if (!isSymRef(content)) throw new IOException(MessageFormat.format( JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath())); int pathStart = 8; int lineEnd = RawParseUtils.nextLF(content, pathStart); while (content[lineEnd - 1] == '\n' || (content[lineEnd - 1] == '\r' && SystemReader.getInstance().isWindows())) lineEnd--; if (lineEnd == pathStart) throw new IOException(MessageFormat.format( JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath())); String gitdirPath = RawParseUtils.decode(content, pathStart, lineEnd); File gitdirFile = fs.resolve(workTree, gitdirPath); if (gitdirFile.isAbsolute()) return gitdirFile; else return new File(workTree, gitdirPath).getCanonicalFile(); }
/** * {@inheritDoc} * * @since 4.10 */ @Override protected byte[] readIncludedConfig(String relPath) throws ConfigInvalidException { final File file; if (relPath.startsWith("~/")) { //$NON-NLS-1$ file = fs.resolve(fs.userHome(), relPath.substring(2)); } else { file = fs.resolve(configFile.getParentFile(), relPath); } if (!file.exists()) { return null; } try { return IO.readFully(file); } catch (IOException ioe) { throw new ConfigInvalidException(MessageFormat .format(JGitText.get().cannotReadFile, relPath), ioe); } } }
@Nullable private String readCommitMsgFile(String msgFilename) throws IOException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); File mergeMsgFile = new File(getDirectory(), msgFilename); try { return RawParseUtils.decode(IO.readFully(mergeMsgFile)); } catch (FileNotFoundException e) { if (mergeMsgFile.exists()) { throw e; } // the file has disappeared in the meantime ignore it return null; } }
private void readSmartHeaders(InputStream in, String service) throws IOException { // A smart reply will have a '#' after the first 4 bytes, but // a dumb reply cannot contain a '#' until after byte 41. Do a // quick check to make sure its a smart reply before we parse // as a pkt-line stream. // final byte[] magic = new byte[5]; IO.readFully(in, magic, 0, magic.length); if (magic[4] != '#') { throw new TransportException(uri, MessageFormat.format( JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic))); } final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream( new ByteArrayInputStream(magic), in)); final String exp = "# service=" + service; //$NON-NLS-1$ final String act = pckIn.readString(); if (!exp.equals(act)) { throw new TransportException(uri, MessageFormat.format( JGitText.get().expectedGot, exp, act)); } while (pckIn.readString() != PacketLineIn.END) { // for now, ignore the remaining header lines } }