private void createExampleRepository(File dir, int numSlots) throws IOException { for (int i = 0; i < numSlots; i++) { File slotDir = new File(dir, i + "-" + (800000 + i)); slotDir.mkdirs(); File slotInfoFile = new File(slotDir, "slot.info"); IoUtil.save(slotInfoFile, "namedCurveSupported=true\n".getBytes()); } }
public static void save(String fileName, byte[] encoded) throws IOException { save(new File(expandFilepath(fileName)), encoded); }
public DigestDiffReporter(String reportDirname, byte[] caCertBytes) throws IOException { this.reportDirname = ParamUtil.requireNonBlank("reportDirname", reportDirname); File dir = new File(reportDirname); dir.mkdirs(); IoUtil.save(new File(dir, "ca.der"), caCertBytes); this.missingWriter = new BufferedWriter(new FileWriter(new File(dir, "missing"))); this.unexpectedWriter = new BufferedWriter(new FileWriter(new File(dir, "unexpected"))); this.diffWriter = new BufferedWriter(new FileWriter(new File(dir, "diff"))); this.goodWriter = new BufferedWriter(new FileWriter(new File(dir, "good"))); this.errorWriter = new BufferedWriter(new FileWriter(new File(dir, "error"))); start(); }
public static void save(String fileName, byte[] encoded) throws IOException { save(new File(expandFilepath(fileName)), encoded); }
public DigestDiffReporter(String reportDirname, byte[] caCertBytes) throws IOException { this.reportDirname = Args.notBlank(reportDirname, "reportDirname"); File dir = new File(reportDirname); dir.mkdirs(); IoUtil.save(new File(dir, "ca.der"), caCertBytes); String dirPath = dir.getPath(); this.missingWriter = Files.newBufferedWriter(Paths.get(dirPath, "missing")); this.unexpectedWriter = Files.newBufferedWriter(Paths.get(dirPath, "unexpected")); this.diffWriter = Files.newBufferedWriter(Paths.get(dirPath, "diff")); this.goodWriter = Files.newBufferedWriter(Paths.get(dirPath, "good")); this.errorWriter = Files.newBufferedWriter(Paths.get(dirPath, "error")); start(); }
private void replaceFile(File file, String oldText, String newText) throws Exception { BufferedReader reader = Files.newBufferedReader(file.toPath()); ByteArrayOutputStream writer = new ByteArrayOutputStream(); boolean changed = false; try { String line; while ((line = reader.readLine()) != null) { if (line.contains(oldText)) { changed = true; writer.write(line.replace(oldText, newText).getBytes()); } else { writer.write(line.getBytes()); } writer.write('\n'); } } finally { writer.close(); reader.close(); } if (changed) { File newFile = new File(file.getPath() + "-new"); byte[] newBytes = writer.toByteArray(); IoUtil.save(file, newBytes); newFile.renameTo(file); } }
private void replaceFile(File file, String oldText, String newText) throws Exception { BufferedReader reader = new BufferedReader(new FileReader(file)); ByteArrayOutputStream writer = new ByteArrayOutputStream(); boolean changed = false; try { String line; while ((line = reader.readLine()) != null) { if (line.contains(oldText)) { changed = true; writer.write(line.replace(oldText, newText).getBytes()); } else { writer.write(line.getBytes()); } writer.write('\n'); } } finally { writer.close(); reader.close(); } if (changed) { File newFile = new File(file.getPath() + "-new"); byte[] newBytes = writer.toByteArray(); IoUtil.save(file, newBytes); newFile.renameTo(file); } }
public CaManagerImpl() { this.datasourceFactory = new DataSourceFactory(); String calockId = null; File caLockFile = new File("calock"); if (caLockFile.exists()) { try { calockId = new String(IoUtil.read(caLockFile)); } catch (IOException ex) { LOG.error("could not read {}: {}", caLockFile.getName(), ex.getMessage()); } } if (calockId == null) { calockId = UUID.randomUUID().toString(); try { IoUtil.save(caLockFile, calockId.getBytes()); } catch (IOException ex) { LOG.error("could not save {}: {}", caLockFile.getName(), ex.getMessage()); } } String hostAddress = null; try { hostAddress = IoUtil.getHostAddress(); } catch (SocketException ex) { LOG.warn("could not get host address: {}", ex.getMessage()); } this.lockInstanceId = (hostAddress == null) ? calockId : hostAddress + "/" + calockId; this.restResponder = new RestResponder(this); } // constructor
protected FileOrValueType buildFileOrValue(String content, String fileName) throws IOException { if (content == null) { return null; } ParamUtil.requireNonNull("fileName", fileName); FileOrValueType ret = new FileOrValueType(); if (content.length() < 256) { ret.setValue(content); return ret; } File file = new File(baseDir, fileName); File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } IoUtil.save(file, content.getBytes("UTF-8")); ret.setFile(fileName); return ret; }
protected FileOrBinaryType buildFileOrBinary(byte[] content, String fileName) throws IOException { if (content == null) { return null; } ParamUtil.requireNonNull("fileName", fileName); FileOrBinaryType ret = new FileOrBinaryType(); if (content.length < 256) { ret.setBinary(content); return ret; } File file = new File(baseDir, fileName); File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } IoUtil.save(file, content); ret.setFile(fileName); return ret; }
protected FileOrValue buildFileOrValue(String content, String fileName) throws IOException { if (content == null) { return null; } Args.notNull(fileName, "fileName"); FileOrValue ret = new FileOrValue(); if (content.length() < 256) { ret.setValue(content); return ret; } File file = new File(baseDir, fileName); IoUtil.mkdirsParent(file.toPath()); IoUtil.save(file, content.getBytes("UTF-8")); ret.setFile(fileName); return ret; }
private void exportIssuer(OcspCertstore certstore) throws DataAccessException, IOException { System.out.println("exporting table ISSUER"); List<OcspCertstore.Issuer> issuers = new LinkedList<>(); certstore.setIssuers(issuers); final String sql = "SELECT ID,CERT,REV_INFO FROM ISSUER"; Statement stmt = null; ResultSet rs = null; try { stmt = createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("ID"); OcspCertstore.Issuer issuer = new OcspCertstore.Issuer(); issuer.setId(id); String certFileName = "issuer-conf/cert-issuer-" + id; IoUtil.save(new File(baseDir, certFileName), rs.getString("CERT").getBytes("UTF-8")); issuer.setCertFile(certFileName); issuer.setRevInfo(rs.getString("REV_INFO")); issuers.add(issuer); } } catch (SQLException ex) { throw translate(sql, ex); } finally { releaseResources(stmt, rs); } System.out.println(" exported table ISSUER"); } // method exportIssuer
protected FileOrBinary buildFileOrBinary(byte[] content, String fileName) throws IOException { if (content == null) { return null; } Args.notNull(fileName, "fileName"); FileOrBinary ret = new FileOrBinary(); if (content.length < 256) { ret.setBinary(content); return ret; } File file = new File(baseDir, fileName); IoUtil.mkdirsParent(file.toPath()); IoUtil.save(file, content); ret.setFile(fileName); return ret; }
@Override public void close() { closeWriter(missingWriter); closeWriter(unexpectedWriter); closeWriter(diffWriter); closeWriter(goodWriter); closeWriter(errorWriter); int sum = numGood.get() + numDiff.get() + numMissing.get() + numUnexpected.get() + numError.get(); Date now = new Date(); int durationSec = (int) ((now.getTime() - startTime.getTime()) / 1000); String speedStr = (durationSec > 0) ? StringUtil.formatAccount(sum / durationSec, false) + " /s" : "--"; String str = StringUtil.concatObjectsCap(200, " sum : ", StringUtil.formatAccount(sum, false), "\n good: ", StringUtil.formatAccount(numGood.get(), false), "\n diff: ", StringUtil.formatAccount(numDiff.get(), false), "\n missing: ", StringUtil.formatAccount(numMissing.get(), false), "\nunexpected: ", StringUtil.formatAccount(numUnexpected.get(), false), "\n error: ", StringUtil.formatAccount(numError.get(), false), "\n duration: ", StringUtil.formatTime(durationSec, false), "\nstart time: ", startTime, "\n end time: ", now, "\n speed: ", speedStr, "\n"); try { IoUtil.save(reportDirname + File.separator + "overview.txt", str.getBytes()); } catch (IOException ex) { System.out.println("Could not write overview.txt with following content\n" + str); } } // method close
private void savePkcs11Entry(File dir, byte[] id, String label, byte[] value) throws P11TokenException { Args.notNull(dir, "dir"); Args.notNull(id, "id"); Args.notBlank(label, "label"); Args.notNull(value, "value"); assertValidId(id); String hexId = hex(id); String str = StringUtil.concat(PROP_ID, "=", hexId, "\n", PROP_LABEL, "=", label, "\n", PROP_SHA1SUM, "=", HashAlgo.SHA1.hexHash(value), "\n"); try { IoUtil.save(new File(dir, hexId + INFO_FILE_SUFFIX), str.getBytes()); IoUtil.save(new File(dir, hexId + VALUE_FILE_SUFFIX), value); } catch (IOException ex) { throw new P11TokenException("could not save certificate"); } }
private void exportIssuer(CertstoreType certstore) throws DataAccessException, IOException { System.out.println("exporting table ISSUER"); Issuers issuers = new Issuers(); certstore.setIssuers(issuers); final String sql = "SELECT ID,CERT,REV_INFO FROM ISSUER"; Statement stmt = null; ResultSet rs = null; try { stmt = createStatement(); rs = stmt.executeQuery(sql); String issuerCertsDir = "issuer-conf"; new File(issuerCertsDir).mkdirs(); while (rs.next()) { int id = rs.getInt("ID"); IssuerType issuer = new IssuerType(); issuer.setId(id); String certFileName = issuerCertsDir + "/cert-issuer-" + id; IoUtil.save(new File(baseDir, certFileName), rs.getString("CERT").getBytes("UTF-8")); issuer.setCertFile(certFileName); issuer.setRevInfo(rs.getString("REV_INFO")); issuers.getIssuer().add(issuer); } } catch (SQLException ex) { throw translate(sql, ex); } finally { releaseResources(stmt, rs); } System.out.println(" exported table ISSUER"); } // method exportIssuer
public void close() { closeWriter(missingWriter); closeWriter(unexpectedWriter); closeWriter(diffWriter); closeWriter(goodWriter); closeWriter(errorWriter); int sum = numGood.get() + numDiff.get() + numMissing.get() + numUnexpected.get() + numError.get(); Date now = new Date(); int durationSec = (int) ((now.getTime() - startTime.getTime()) / 1000); String speedStr = (durationSec > 0) ? StringUtil.formatAccount(sum / durationSec, false) + " /s" : "--"; String str = StringUtil.concatObjectsCap(200, " sum : ", StringUtil.formatAccount(sum, false), "\n good: ", StringUtil.formatAccount(numGood.get(), false), "\n diff: ", StringUtil.formatAccount(numDiff.get(), false), "\n missing: ", StringUtil.formatAccount(numMissing.get(), false), "\nunexpected: ", StringUtil.formatAccount(numUnexpected.get(), false), "\n error: ", StringUtil.formatAccount(numError.get(), false), "\n duration: ", StringUtil.formatTime(durationSec, false), "\nstart time: ", startTime, "\n end time: ", now, "\n speed: ", speedStr, "\n"); try { IoUtil.save(reportDirname + File.separator + "overview.txt", str.getBytes()); } catch (IOException ex) { System.out.println("Could not write overview.txt with following content\n" + str); } } // method close
IoUtil.save(new File(pubKeyDir, hexId + INFO_FILE_SUFFIX), sb.toString().getBytes()); } catch (IOException ex) { throw new P11TokenException(ex.getMessage(), ex);