Java Code Examples for java.io.InputStream

The following are top voted examples for showing how to use java.io.InputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ViperBot   File: DownloadManager.java   View source code 7 votes vote down vote up
public File download(boolean delete, String link) throws IOException{
	URL url = new URL(link);
	URLConnection conn = url.openConnection();
	InputStream in = conn.getInputStream();
	File f = new File(defaultFolder, link);
	FileOutputStream out = new FileOutputStream(f);
	byte[] buffer = new byte[1024];
	int len;
	while((len = in.read(buffer)) > 0){
		out.write(buffer, 0, len);
	}
	in.close();
	out.close();
	if(delete) f.deleteOnExit();
	return f;
}
 
Example 2
Project: graphium   File: BaseSegmentXInfoService.java   View source code 6 votes vote down vote up
private void streamBaseXInfos(String graph, String version, InputStream inputStream, boolean isSegmentXInfo)
        throws XInfoNotSupportedException, GraphImportException, GraphStorageException, GraphNotExistsException {
    //First checkk if already another import is running. The singleton serverStatus has to be injected therefore
    if (!serverStatus.registerImport()) {
        throw new GraphImportException("Sorry, system is busy, a graph import is currently executed");
    }
    IBaseSegmentProducer<IBaseSegment> producer = null;
    try {

        BlockingQueue<IBaseSegment> segmentsQueue;

        segmentsQueue = new ArrayBlockingQueue<>(queueSize);

        producer = new BaseSegmentProducerImpl<>(inputFormat, inputStream, segmentsQueue);

        Thread producerThread = new Thread(producer, "basesegment-xinfo-parser-thread");
        producerThread.start();

        List<IBaseSegment> segments = new ArrayList<>();
        while (producerThread.isAlive() || !segmentsQueue.isEmpty()) {
            if (!segmentsQueue.isEmpty()) {
                segments.add(segmentsQueue.poll());
            }
            if (segments.size() >= this.batchSize) {
                this.writeSegments(segments,graph,version,isSegmentXInfo);
                segments.clear();
            }
        }
        this.writeSegments(segments,graph,version,isSegmentXInfo);
    } finally {
        serverStatus.unregisterImport();
        if (producer != null && producer.getException() != null) {
            throw new GraphImportException("Graph could not be imported",producer.getException());
        }
    }
}
 
Example 3
Project: 19porn   File: Codec.java   View source code 6 votes vote down vote up
private static String getMD5(final InputStream is, final int bufLen) {
    if (is == null || bufLen <= 0) {
        return null;
    }
    try {
        MessageDigest md = MessageDigest.getInstance(Algorithm.MD5.getType());
        StringBuilder md5Str = new StringBuilder(32);

        byte[] buf = new byte[bufLen];
        int readCount = 0;
        while ((readCount = is.read(buf)) != -1) {
            md.update(buf, 0, readCount);
        }

        byte[] hashValue = md.digest();

        for (int i = 0; i < hashValue.length; i++) {
            md5Str.append(Integer.toString((hashValue[i] & 0xff) + 0x100, 16).substring(1));
        }
        return md5Str.toString();
    } catch (Exception e) {
        return null;
    }
}
 
Example 4
Project: Image-Detection-Samples   File: Utils.java   View source code 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 5
Project: FastDFS_Client   File: StorageModifyCommandTest.java   View source code 6 votes vote down vote up
@Test
public void testStorageModifyCommand() throws IOException {
    String text = "Tobato is a good man. this is a test of StorageTruncateCommand.";
    InputStream firstIn = getTextInputStream(text);
    long firstSize = firstIn.available();
    // 上载文字
    System.out.println(firstSize);
    StorePath path = uploadInputStream(firstIn, "txt", firstSize, true);
    // 文件修改
    String Modifytext = "This is a test of StorageModifyCommand";
    InputStream modifyIn = getTextInputStream(Modifytext);
    long modifySize = modifyIn.available();
    // 观察运行效果:
    // fileOffset参数0 结果为 This is a test of StorageModifyCommandf
    // StorageTruncateCommand
    // fileOffset参数为20 结果为 Tobato is a good manThis is a test of
    // StorageModifyCommandmand
    StorageModifyCommand command = new StorageModifyCommand(path.getPath(), modifyIn, modifySize, 0);
    executeStoreCmd(command);
    LOGGER.debug("--文件修改处理成功--");
}
 
Example 6
Project: Jupiter   File: Utils.java   View source code 5 votes vote down vote up
/**
* 
* @param file 書き込み先のファイルオブジェクト
* @param content 書き込む内容(InputStream)
* 
* <p>writeFile - Utils</p>
* 
* <p>contentで指定した内容をファイルに書き込みます。</p>
* 
* Jupiter by Jupiter Development Team
* 
*/

  public static void writeFile(File file, InputStream content) throws IOException {
      if (content == null) {
          throw new IllegalArgumentException("content must not be null");
      }
      if (!file.exists()) {
          file.createNewFile();
      }
      FileOutputStream stream = new FileOutputStream(file);
      byte[] buffer = new byte[1024];
      int length;
      while ((length = content.read(buffer)) != -1) {
          stream.write(buffer, 0, length);
      }
      stream.close();
      content.close();
  }
 
Example 7
Project: openjdk-jdk10   File: HTTPTestServer.java   View source code 5 votes vote down vote up
private synchronized Thread pipe(InputStream is, OutputStream os, char tag) {
    return new Thread("TunnelPipe("+tag+")") {
        @Override
        public void run() {
            try {
                try {
                    int c;
                    while ((c = is.read()) != -1) {
                        os.write(c);
                        os.flush();
                        // if DEBUG prints a + or a - for each transferred
                        // character.
                        if (DEBUG) System.out.print(tag);
                    }
                    is.close();
                } finally {
                    os.close();
                }
            } catch (IOException ex) {
                if (DEBUG) ex.printStackTrace(System.out);
            }
        }
    };
}
 
Example 8
Project: Cubes   File: AssetFinder.java   View source code 5 votes vote down vote up
private static InputStream getInputStream(URL jar) throws IOException {
  //to fix offset caused by launch4j
  InputStream is = jar.openStream();
  Log.debug("Scanning for jar...");
  long offset = 0;
  boolean found = false;
  try {
    while (true) {
      if (getUnsignedInt(is) == 0x04034b50L) {
        found = true;
        break;
      }
      offset += 4;
    }
  } catch (IOException ignored) {
  }
  is.close();

  InputStream finalIS = jar.openStream();
  if (!found) {
    Log.debug("Failed to find start");
  } else {
    Log.debug("Skipping " + offset + " bytes until start of jar [" + finalIS.skip(offset) + "]");
    if (finalIS.markSupported()) finalIS.mark(Integer.MAX_VALUE);
  }
  return finalIS;
}
 
Example 9
Project: bubichain-sdk-java   File: HttpKit.java   View source code 5 votes vote down vote up
/**
 *  鍙戦�丳ost璇锋眰
 * @param url
 * @param params
 * @return
 * @throws IOException 
 * @throws NoSuchProviderException 
 * @throws NoSuchAlgorithmException 
 * @throws KeyManagementException 
 */
public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
	StringBuffer bufferRes = null;
    TrustManager[] tm = { new MyX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
    sslContext.init(null, tm, new java.security.SecureRandom());
    // 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄  
    SSLSocketFactory ssf = sslContext.getSocketFactory();

    URL urlGet = new URL(url);
    HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
    // 杩炴帴瓒呮椂
    http.setConnectTimeout(50000);
    // 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
    http.setReadTimeout(50000);
    http.setRequestMethod("POST");
    http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    http.setSSLSocketFactory(ssf);
    http.setHostnameVerifier(new Verifier());
    http.setDoOutput(true);
    http.setDoInput(true);
    http.connect();

    OutputStream out = http.getOutputStream();
    out.write(params.getBytes("UTF-8"));
    out.flush();
    out.close();

    InputStream in = http.getInputStream();
    BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    String valueString = null;
    bufferRes = new StringBuffer();
    while ((valueString = read.readLine()) != null){
        bufferRes.append(valueString);
    }
    in.close();
    if (http != null) {
        // 鍏抽棴杩炴帴
        http.disconnect();
    }
    return bufferRes.toString();
}
 
Example 10
Project: APITools   File: SWTResourceManager.java   View source code 5 votes vote down vote up
/**
 * Returns an {@link Image} encoded by the specified {@link InputStream}.
 * 
 * @param stream
 *            the {@link InputStream} encoding the image data
 * @return the {@link Image} encoded by the specified input stream
 */
protected static Image getImage(InputStream stream) throws IOException {
	try {
		Display display = Display.getCurrent();
		ImageData data = new ImageData(stream);
		if (data.transparentPixel > 0) {
			return new Image(display, data, data.getTransparencyMask());
		}
		return new Image(display, data);
	} finally {
		stream.close();
	}
}
 
Example 11
Project: openjdk-jdk10   File: OpenWaveFile.java   View source code 5 votes vote down vote up
static void check(Object source) throws Exception {
     AudioFileFormat aff2 = null;
     if (source instanceof File) {
        aff2 = AudioSystem.getAudioFileFormat((File) source);
     }
     else if (source instanceof InputStream) {
        aff2 = AudioSystem.getAudioFileFormat((InputStream) source);
     }
     else if (source instanceof URL) {
        aff2 = AudioSystem.getAudioFileFormat((URL) source);
     } else throw new Exception("wrong source. Test FAILED");
     System.out.println("Got: "+aff2);
     if (aff2.getFormat().getSampleSizeInBits()==-1) {
        throw new Exception("wrong audio format. Test FAILED");
     }
}
 
Example 12
Project: jdk8u-jdk   File: ZipFileSystem.java   View source code 5 votes vote down vote up
private static void copyStream(InputStream is, OutputStream os)
    throws IOException
{
    byte[] copyBuf = new byte[8192];
    int n;
    while ((n = is.read(copyBuf)) != -1) {
        os.write(copyBuf, 0, n);
    }
}
 
Example 13
Project: ramus   File: AbstractEngine.java   View source code 5 votes vote down vote up
@Override
public Properties getProperties(String path) {
    InputStream is = getInputStream(path);
    Properties res = new Properties();
    if (is != null) {
        try {
            res.loadFromXML(is);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    return res;
}
 
Example 14
Project: OpenDiabetes   File: JDBCResultSet.java   View source code 5 votes vote down vote up
/**
 * <!-- start generic documentation -->
 * Retrieves the value of the designated column in the current row
 * of this <code>ResultSet</code> object as a  stream of
 * uninterpreted bytes. The value can then be read in chunks from the
 * stream. This method is particularly
 * suitable for retrieving large <code>LONGVARBINARY</code> values.
 *
 * <P><B>Note:</B> All the data in the returned stream must be
 * read prior to getting the value of any other column. The next
 * call to a getter method implicitly closes the stream.  Also, a
 * stream may return <code>0</code> when the method
 * <code>InputStream.available</code>
 * is called whether there is data available or not.
 * <!-- end generic documentation -->
 *
 * @param columnIndex the first column is 1, the second is 2, ...
 * @return a Java input stream that delivers the database column value
 *         as a stream of uninterpreted bytes;
 *         if the value is SQL <code>NULL</code>, the value returned is
 *         <code>null</code>
 * @exception SQLException if a database access error occurs or this method is
 *            called on a closed result set
 */
public java.io.InputStream getBinaryStream(
        int columnIndex) throws SQLException {

    checkColumn(columnIndex);

    Type   sourceType = resultMetaData.columnTypes[columnIndex - 1];
    Object o          = getColumnInType(columnIndex, sourceType);

    if (o == null) {
        return null;
    }

    if (o instanceof BlobDataID) {
        return ((BlobDataID) o).getBinaryStream(session);
    } else if (o instanceof Blob) {
        return ((Blob) o).getBinaryStream();
    } else if (o instanceof BinaryData) {
        byte[] b = getBytes(columnIndex);

        return new ByteArrayInputStream(b);
    }

    throw JDBCUtil.sqlException(ErrorCode.X_42561);
}
 
Example 15
Project: CustomWorldGen   File: TemplateManager.java   View source code 5 votes vote down vote up
/**
 * reads a template from an inputstream
 */
private void readTemplateFromStream(String id, InputStream stream) throws IOException
{
    NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(stream);
    Template template = new Template();
    template.read(nbttagcompound);
    this.templates.put(id, template);
}
 
Example 16
Project: junit-dataproviders   File: ExcelParametersProvider.java   View source code 5 votes vote down vote up
private Object[] paramsFromFile() {
	try {
		InputStream inputStream = createProperReader();
		try {
			return map(inputStream);
		} finally {
			inputStream.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException(
				"Could not successfully read parameters from file: " + filepath, e);
	}
}
 
Example 17
Project: hadoop   File: FSImageLoader.java   View source code 5 votes vote down vote up
private static byte[][] loadINodeSection(InputStream in)
        throws IOException {
  FsImageProto.INodeSection s = FsImageProto.INodeSection
      .parseDelimitedFrom(in);
  LOG.info("Loading " + s.getNumInodes() + " inodes.");
  final byte[][] inodes = new byte[(int) s.getNumInodes()][];

  for (int i = 0; i < s.getNumInodes(); ++i) {
    int size = CodedInputStream.readRawVarint32(in.read(), in);
    byte[] bytes = new byte[size];
    IOUtils.readFully(in, bytes, 0, size);
    inodes[i] = bytes;
  }
  LOG.debug("Sorting inodes");
  Arrays.sort(inodes, INODE_BYTES_COMPARATOR);
  LOG.debug("Finished sorting inodes");
  return inodes;
}
 
Example 18
Project: ARCLib   File: FileRepositoryTest.java   View source code 5 votes vote down vote up
@Test
public void deleteTest() throws IOException {
    String id;
    try (InputStream stream = resource("cz/inqool/uas/file/test.pdf")) {
        FileRef pdfFile = repository.create(stream, "test.pdf", "application/pdf", false);
        id = pdfFile.getId();
    }

    FileRef fileRef = repository.get(id);
    try (InputStream stream = resource("cz/inqool/uas/file/test.pdf"); InputStream stream2 = fileRef.getStream()) {
        IOUtils.contentEquals(stream, stream2);
    }

    repository.del(fileRef);

    assertThrown(() -> repository.get(id))
            .isInstanceOf(MissingObject.class);
}
 
Example 19
Project: mintleaf   File: SqlStreamReaderTest.java   View source code 5 votes vote down vote up
@Test
public void testSqlReaderReadTest() throws MintleafException {

    InputStream iStream = this.getClass().getResourceAsStream("/EmptyPackage.sql");
    SqlStreamReader reader = new SqlStreamReader(iStream);


    final StringBuilder actual = new StringBuilder();
    ReadListener listner = new EmptyPackageReadListner() {

        @Override
        public Object eachRow(int rowNum, Row row) throws MintleafException {
            actual.append(((ChangeSet) row).getChangeSetSource());
            return null;
        }


    };
    reader.setReadListener(listner);
    reader.read();

    StringBuilder expected = new StringBuilder();

    expected.append("create or replace package EmptyPackage\n");
    expected.append("as\n");
    expected.append("end EmptyPackage;");
    expected.append("create or replace\n");
    expected.append("package body EmptyPackage\n");
    expected.append("as\n");
    expected.append("end EmptyPackage;");

    assertEquals(expected.toString(), actual.toString());
}
 
Example 20
Project: OpenJSharp   File: MicToken_v2.java   View source code 5 votes vote down vote up
public void verify(InputStream data) throws GSSException {
    byte[] dataBytes = null;
    try {
        dataBytes = new byte[data.available()];
        data.read(dataBytes);
    } catch (IOException e) {
        // Error reading application data
        throw new GSSException(GSSException.BAD_MIC, -1,
            "Corrupt checksum or sequence number in MIC token");
    }
    verify(dataBytes, 0, dataBytes.length);
}
 
Example 21
Project: oscm-app   File: ApplicationBean.java   View source code 5 votes vote down vote up
/**
 * Read the build id and date from the ear manifest.
 */
private void initBuildIdAndDate() {
    if (buildId != null) {
        return;
    }
    buildId = "-1";
    buildDate = "";

    // read the implementation version property from the war manifest
    final InputStream in = FacesContext.getCurrentInstance()
            .getExternalContext()
            .getResourceAsStream("/META-INF/MANIFEST.MF");
    String str = null;
    if (in != null) {
        final Properties prop = PropertiesLoader.loadProperties(in);
        str = prop.getProperty("Implementation-Version");
    }

    if (str == null) {
        return;
    }

    // parse the implementation version
    final int sep = str.lastIndexOf("-");
    buildId = str.substring(0, sep);

    SimpleDateFormat inFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    SimpleDateFormat outFormat = new SimpleDateFormat("yyyy/MM/dd");
    try {
        buildDate = outFormat
                .format(inFormat.parse(str.substring(sep + 1)));
    } catch (ParseException e) {
        logger.error(e.getMessage());
    }

}
 
Example 22
Project: testcontainers-java-module-elasticsearch   File: ElasticsearchResource.java   View source code 5 votes vote down vote up
public ElasticsearchResource(String resourceName) {
    // Find the latest version this project was built with
    String propVersion;
    String propBaseUrl;
    String defaultBaseUrl = null;
    String defaultVersion = null;
    Properties props = new Properties();
    try {
        props.load(ElasticsearchResource.class.getResourceAsStream(FALLBACK_RESOURCE_NAME));
        defaultBaseUrl = props.getProperty("baseUrl");
        defaultVersion = props.getProperty("version");
    } catch (IOException ignored) {
        // This can normally never happen unless someone modifies the JAR file o_O
    }
    try {
        InputStream stream = ElasticsearchResource.class.getResourceAsStream(resourceName);
        if (stream != null) {
            props.load(stream);
            propBaseUrl = props.getProperty("baseUrl", defaultBaseUrl);
            propVersion = props.getProperty("version", defaultVersion);
        } else {
            propBaseUrl = defaultBaseUrl;
            propVersion = defaultVersion;
        }
    } catch (IOException e) {
        // We might get that exception if the user provides a badly formatted property file
        propBaseUrl = null;
        propVersion = null;
    }
    baseUrl = propBaseUrl;
    version = propVersion;
}
 
Example 23
Project: nifi-nars   File: PutScp.java   View source code 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession processSession) {
    List<FlowFile> flowFiles = processSession.get(batchSize);
    if (flowFiles.isEmpty()) {
        return;
    }

    Session jschSession = null;
    Channel channel = null;
    try {
        jschSession = openSession(context);
        final String remotePath = context.getProperty(REMOTE_PATH).evaluateAttributeExpressions().getValue();
        channel = openExecChannel(context, jschSession, "scp -r -d -t " + remotePath);

        InputStream channelIn = channel.getInputStream();
        OutputStream channelOut = channel.getOutputStream();

        channel.connect();
        waitForAck(channelIn);

        ListIterator<FlowFile> fileIt = flowFiles.listIterator();
        while (fileIt.hasNext()) {
            final FlowFile flowFile = fileIt.next();

            // conditionally reject files that are zero bytes or less
            if (context.getProperty(REJECT_ZERO_BYTE).asBoolean() && flowFile.getSize() == 0) {
                logger.warn("Rejecting {} because it is zero bytes", new Object[]{flowFile});
                processSession.transfer(processSession.penalize(flowFile), REL_REJECT);
                fileIt.remove();
                continue;
            }

            final String filename = flowFile.getAttribute(CoreAttributes.FILENAME.key());
            final String permissions = context.getProperty(PERMISSIONS).evaluateAttributeExpressions(flowFile).getValue();

            // destination path + filename
            // final String fullPath = buildFullPath(context, flowFile, filename);

            processSession.read(flowFile, new InputStreamCallback() {
                @Override
                public void process(final InputStream flowFileIn) throws IOException {
                    // send "C0644 filesize filename", where filename should not include '/'
                    StringBuilder command = new StringBuilder("C").append(permissions).append(' ');
                    command.append(flowFile.getSize()).append(' ');
                    command.append(filename).append('\n');

                    channelOut.write(command.toString().getBytes(StandardCharsets.UTF_8));
                    channelOut.flush();
                    waitForAck(channelIn);

                    IOUtils.copy(flowFileIn, channelOut);
                    channelOut.flush();
                    sendAck(channelOut);
                    waitForAck(channelIn);
                }
            });

            processSession.transfer(flowFile, REL_SUCCESS);
            processSession.getProvenanceReporter().send(flowFile, remotePath);
            fileIt.remove();
            if (logger.isDebugEnabled()) {
                logger.debug("Sent {} to remote host", new Object[]{flowFile});
            }
        }

    } catch (JSchException | IOException ex) {
        context.yield();
        logger.error("Unable to create session to remote host due to {}", new Object[]{ex}, ex);
        processSession.transfer(flowFiles, REL_FAILURE);

    } finally {
        if (channel != null) {
            channel.disconnect();
        }
        if (jschSession != null) {
            jschSession.disconnect();
        }
    }
}
 
Example 24
Project: tomcat7   File: TestChunkedInputFilter.java   View source code 5 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter pw = resp.getWriter();
    // Headers not visible yet, body not processed
    dumpHeader("x-trailer1", req, pw);
    dumpHeader("x-trailer2", req, pw);

    // Read the body - quick and dirty
    InputStream is = req.getInputStream();
    int count = 0;
    try {
        while (is.read() > -1) {
            count++;
        }
    } catch (IOException ioe) {
        exceptionDuringRead = true;
        if (!expectPass) { // as expected
            log(ioe.toString());
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        throw ioe;
    }

    pw.write(Integer.valueOf(count).toString());

    // Headers should be visible now
    dumpHeader("x-trailer1", req, pw);
    dumpHeader("x-trailer2", req, pw);
}
 
Example 25
Project: geomapapp   File: TableDB.java   View source code 5 votes vote down vote up
void read( String urlString ) throws IOException {
	URL url = URLFactory.url(urlString);
	InputStream input = urlString.endsWith(".gz")
		? new GZIPInputStream( url.openStream() )
		: url.openStream();
		
	BufferedReader in = new BufferedReader(
		new InputStreamReader( input));
	read( in );
}
 
Example 26
Project: dooo   File: EsClientManager.java   View source code 5 votes vote down vote up
/**
 * 初始化索引
 *
 * @throws Exception
 */
private static void initIndex() throws Exception {
    String indice = esprop.getIndice();
    IndicesAdminClient c = client.admin().indices();
    //创建一个空的
    boolean a = c.prepareExists(indice).get().isExists();
    LOGGER.info("index {} isExists  {}",indice, a);
    if (!c.prepareExists(indice).get().isExists()) {
        CreateIndexResponse createIndexResponse =c.prepareCreate(indice).get();
        LOGGER.info("create index {}", createIndexResponse);
    }
    for (IndexType type : IndexType.values()) {
        TypesExistsResponse typesExistsResponse = c.typesExists(new TypesExistsRequest(new String[]{indice}, type.getDataName())).get();
        if (typesExistsResponse.isExists()) {
            continue;
        }
        String esMapper = type.getMapper();
        InputStream in = EsClientManager.class.getResourceAsStream(esMapper);
        String mappingStr = IOUtils.toString(in).trim();
        IOUtils.closeQuietly(in);
        c.preparePutMapping(indice).setType(type.getDataName()).setSource(mappingStr).get();
    }
}
 
Example 27
Project: Cable-Android   File: PersistentBlobProvider.java   View source code 5 votes vote down vote up
private Uri create(@NonNull  MasterSecret masterSecret,
                   @NonNull  InputStream input,
                             long id,
                   @NonNull  String mimeType,
                   @Nullable String fileName,
                   @Nullable Long fileSize)
{
  persistToDisk(masterSecret, id, input);
  final Uri uniqueUri = CONTENT_URI.buildUpon()
                                   .appendPath(mimeType)
                                   .appendPath(getEncryptedFileName(masterSecret, fileName))
                                   .appendEncodedPath(String.valueOf(fileSize))
                                   .appendEncodedPath(String.valueOf(System.currentTimeMillis()))
                                   .build();
  return ContentUris.withAppendedId(uniqueUri, id);
}
 
Example 28
Project: rlc-analyser   File: RlcAnalyser.java   View source code 5 votes vote down vote up
/**
 * This method takes a potential RLC file and unpacks it as a ZIP file. Then
 * filters through the entries in the ZIP file to find the XML file for
 * processing. XML is then parsed through XStream and stored in an engine
 * for data collection.
 *
 * @param file
 *            - file for processing
 * @throws FileNotFoundException
 * @throws IOException
 */
private void analyseRlc(final File file) throws FileNotFoundException, IOException, RuntimeException {
	final InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
	final ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
	ZipArchiveEntry entry;
	try {
		while ((entry = zipInputStream.getNextZipEntry()) != null) {
			// Find the XML configuration file and ignore the rest
			if (!entry.isDirectory()) {
				final String entryName = entry.getName().trim().toLowerCase();
				final int directoryIndex = entryName.indexOf('/');

				if (entryName.length() > 4 && entryName.endsWith(".xml") && directoryIndex <= 0) {
					if (!zipInputStream.canReadEntryData(entry)) {
						throw new RuntimeException("RLC file could not be processed.");
					}
					logger.info("[PROCESSING] : " + entryName);

					final Engine engine = parser.parse(zipInputStream);

					if (engine == null) {
						// Null returned do not add to engine list
						logger.error("[ERROR] : Could not process " + entryName);
					} else {
						this.fileCounter++; // increment file count.
						// file count used as a way to name files and distinguish RLC's with same names.
						// Name of a file: FileCoutner + ":" + <FileName>. n is the file count.
						engine.setFileName(this.fileCounter + ":" + file.getName());
						this.engineList.add(engine);
						this.dataCollector.collectData(engine);
					}
				}
			}
		}
	} finally {
		zipInputStream.close();
	}
}
 
Example 29
Project: RootPGPExplorer   File: MyPGPUtil.java   View source code 5 votes vote down vote up
/**
 * A simple routine that opens a key ring file and loads the first available key
 * suitable for signature generation.
 *
 * @param input stream to read the secret key ring collection from.
 * @return a secret key.
 * @throws IOException on a problem with using the input stream.
 * @throws PGPException if there is an issue parsing the input stream.
 */
public static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException
{
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator());

    //
    // we just loop through the collection till we find a key suitable for encryption, in the real
    // world you would probably want to be a bit smarter about this.
    //

    Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext())
    {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing)keyRingIter.next();

        Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext())
        {
            PGPSecretKey key = (PGPSecretKey)keyIter.next();

            if (key.isSigningKey())
            {
                return key;
            }
        }
    }

    throw new IllegalArgumentException("Can't find signing key in key ring.");
}
 
Example 30
Project: incubator-netbeans   File: Env.java   View source code 5 votes vote down vote up
/** Creates a new instance of Env */
Env(CommandLine cmd, InputStream is, OutputStream os, OutputStream err, File currentDir) {
    this.is = is;
    this.os = os instanceof PrintStream ? (PrintStream)os : new PrintStream(os);
    this.err = err instanceof PrintStream ? (PrintStream)err : new PrintStream(err);
    this.currentDir = currentDir;
    this.cmd = cmd;
}
 
Example 31
Project: hadoop-oss   File: FsUrlConnection.java   View source code 5 votes vote down vote up
@Override
public InputStream getInputStream() throws IOException {
  if (is == null) {
    connect();
  }
  return is;
}
 
Example 32
Project: Blockly   File: BlocklyXmlHelper.java   View source code 5 votes vote down vote up
/**
 * Convenience function that creates a new {@link ArrayList}.
 * @param inputXml The input stream of XML from which to read.
 * @throws BlockLoadingException If any error occurs with the input. It may wrap an IOException
 *                               or XmlPullParserException as a root cause.
 */
public static List<Block> loadFromXml(InputStream inputXml, BlockFactory blockFactory)
        throws BlockLoadingException {
    List<Block> result = new ArrayList<>();
    loadBlocksFromXml(inputXml, null, blockFactory, result);
    return result;
}
 
Example 33
Project: Towan   File: CompositeImageData.java   View source code 5 votes vote down vote up
/**
 * @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, boolean, int[])
 */
public ByteBuffer loadImage(InputStream is, boolean flipped, boolean forceAlpha, int[] transparent) throws IOException {
	CompositeIOException exception = new CompositeIOException();
	ByteBuffer buffer = null;
	
	BufferedInputStream in = new BufferedInputStream(is, is.available());
	in.mark(is.available());
	
	// cycle through our source until one of them works
	for (int i=0;i<sources.size();i++) {
		in.reset();
		try {
			LoadableImageData data = (LoadableImageData) sources.get(i);
			
			buffer = data.loadImage(in, flipped, forceAlpha, transparent);
			picked = data;
			break;
		} catch (Exception e) {
			Log.warn(sources.get(i).getClass()+" failed to read the data", e);
			exception.addException(e);
		}
	}
	
	if (picked == null) {
		throw exception;
	}
	
	return buffer;
}
 
Example 34
Project: GitHub   File: Issue1462.java   View source code 5 votes vote down vote up
public ExtClassLoader() throws IOException {
    super(Thread.currentThread().getContextClassLoader());

    {
        byte[] bytes;
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ObjectA.clazz");
        bytes = IOUtils.toByteArray(is);
        is.close();

        super.defineClass("ObjectA", bytes, 0, bytes.length);
    }
}
 
Example 35
Project: cyberduck   File: SFTPWriteFeatureTest.java   View source code 5 votes vote down vote up
@Test
public void testWrite() throws Exception {
    final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch", new Credentials(
            System.getProperties().getProperty("sftp.user"), System.getProperties().getProperty("sftp.password")
    ));
    final SFTPSession session = new SFTPSession(host);
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final TransferStatus status = new TransferStatus();
    final int length = 1048576;
    final byte[] content = RandomUtils.nextBytes(length);
    status.setLength(content.length);
    final Path home = new SFTPHomeDirectoryService(session).find();
    final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
    final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final CryptoVault cryptomator = new CryptoVault(vault, new DisabledPasswordStore());
    cryptomator.create(session, null, new VaultCredentials("test"));
    session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
    final CryptoWriteFeature<Void> writer = new CryptoWriteFeature<>(session, new SFTPWriteFeature(session), cryptomator);
    final Cryptor cryptor = cryptomator.getCryptor();
    final FileHeader header = cryptor.fileHeaderCryptor().create();
    status.setHeader(cryptor.fileHeaderCryptor().encryptHeader(header));
    status.setNonces(new RandomNonceGenerator());
    status.setChecksum(writer.checksum(test).compute(new ByteArrayInputStream(content), status));
    final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    out.close();
    assertTrue(new CryptoFindFeature(session, new SFTPFindFeature(session), cryptomator).find(test));
    Assert.assertEquals(content.length, new CryptoListService(session, session, cryptomator).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes().getSize());
    Assert.assertEquals(content.length, writer.append(test, status.getLength(), PathCache.empty()).size, 0L);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
    final InputStream in = new CryptoReadFeature(session, new SFTPReadFeature(session), cryptomator).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    new StreamCopier(status, status).transfer(in, buffer);
    assertArrayEquals(content, buffer.toByteArray());
    new CryptoDeleteFeature(session, new SFTPDeleteFeature(session), cryptomator).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 36
Project: dota   File: HttpUtils.java   View source code 5 votes vote down vote up
private static void closeStream(InputStream is, FileOutputStream fos) {
    try {
        if (is != null) is.close();
        if (fos != null) fos.close();
    } catch (IOException e) {
        logger.error(ExceptionUtils.getTraceInfo(e));
    }
}
 
Example 37
Project: ditb   File: AddColumnFamilyProcedure.java   View source code 5 votes vote down vote up
@Override
public void deserializeStateData(final InputStream stream) throws IOException {
  super.deserializeStateData(stream);

  MasterProcedureProtos.AddColumnFamilyStateData addCFMsg =
      MasterProcedureProtos.AddColumnFamilyStateData.parseDelimitedFrom(stream);
  user = MasterProcedureUtil.toUserInfo(addCFMsg.getUserInfo());
  tableName = ProtobufUtil.toTableName(addCFMsg.getTableName());
  cfDescriptor = HColumnDescriptor.convert(addCFMsg.getColumnfamilySchema());
  if (addCFMsg.hasUnmodifiedTableSchema()) {
    unmodifiedHTableDescriptor = HTableDescriptor.convert(addCFMsg.getUnmodifiedTableSchema());
  }
}
 
Example 38
Project: nifi-registry   File: TestFileSystemFlowPersistenceProvider.java   View source code 5 votes vote down vote up
private void verifySnapshot(final File flowStorageDir, final String bucketId, final String flowId, final int version,
                            final String contentString) throws IOException {
    // verify the correct snapshot file was created
    final File flowSnapshotFile = new File(flowStorageDir,
            bucketId + "/" + flowId + "/" + version + "/" + version + FileSystemFlowPersistenceProvider.SNAPSHOT_EXTENSION);
    Assert.assertTrue(flowSnapshotFile.exists());

    try (InputStream in = new FileInputStream(flowSnapshotFile)) {
        Assert.assertEquals(contentString, IOUtils.toString(in, StandardCharsets.UTF_8));
    }
}
 
Example 39
Project: apache-tomcat-7.0.73-with-comment   File: WARDirContext.java   View source code 5 votes vote down vote up
/**
 * Content accessor.
 * 
 * @return InputStream
 */
@Override
public InputStream streamContent()
    throws IOException {
    try {
        if (binaryContent == null) {
            InputStream is = base.getInputStream(entry);
            inputStream = is;
            return is;
        }
    } catch (ZipException e) {
        throw new IOException(e.getMessage(), e);
    }
    return super.streamContent();
}
 
Example 40
Project: fuck_zookeeper   File: IOUtils.java   View source code 5 votes vote down vote up
/**
 * Copies from one stream to another.
 * 
 * @param in
 *            InputStrem to read from
 * @param out
 *            OutputStream to write to
 * @param buffSize
 *            the size of the buffer
 */
public static void copyBytes(InputStream in, OutputStream out, int buffSize)
        throws IOException {
    PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null;
    byte buf[] = new byte[buffSize];
    int bytesRead = in.read(buf);
    while (bytesRead >= 0) {
        out.write(buf, 0, bytesRead);
        if ((ps != null) && ps.checkError()) {
            throw new IOException("Unable to write to output stream.");
        }
        bytesRead = in.read(buf);
    }
}