java.nio.channels.NotYetConnectedException#java.nio.channels.Channels源码实例Demo

下面列出了java.nio.channels.NotYetConnectedException#java.nio.channels.Channels 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: deeplearning4j   文件: ConnectionCostsCompiler.java
@Override
public void compile() throws IOException {
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));

    dataOutput.writeInt(cardinality);
    dataOutput.writeInt(bufferSize * SHORT_BYTES);

    ByteBuffer byteBuffer = ByteBuffer.allocate(costs.array().length * SHORT_BYTES);

    for (short cost : this.costs.array()) {
        byteBuffer.putShort(cost);
    }

    WritableByteChannel channel = Channels.newChannel(dataOutput);

    byteBuffer.flip();
    channel.write(byteBuffer);
    dataOutput.close();
}
 
源代码2 项目: riiablo   文件: ClientNetworkReceiver.java
@Override
  protected void processSystem() {
    InputStream in = socket.getInputStream();
    try {
      if (in.available() > 0) {
        ReadableByteChannel channel = Channels.newChannel(in);
        buffer.clear();
        int i = channel.read(buffer);
        buffer.rewind().limit(i);
        D2GS d2gs = new D2GS();
        int p = 0;
        while (buffer.hasRemaining()) {
          int size = ByteBufferUtil.getSizePrefix(buffer);
          D2GS.getRootAsD2GS(ByteBufferUtil.removeSizePrefix(buffer), d2gs);
          if (DEBUG_PACKET) Gdx.app.debug(TAG, p++ + " packet type " + D2GSData.name(d2gs.dataType()) + ":" + size + "B");
          process(d2gs);
//          System.out.println(buffer.position() + "->" + (buffer.position() + size + 4));
          buffer.position(buffer.position() + size + 4); // advance position passed current packet + size prefix of next packet
        }
      }
    } catch (Throwable t) {
      Gdx.app.error(TAG, t.getMessage(), t);
    }
  }
 
源代码3 项目: CloverETL-Engine   文件: FixLenDataParser.java
@Override
public void setDataSource(Object inputDataSource) {
	if (releaseDataSource) releaseDataSource();
	byteBuffer.clear();
	byteBuffer.flip();
	decoder.reset();
	bytesProcessed = 0;

	if (inputDataSource == null) {
		eof = true;
	} else {
		eof = false;
		if (inputDataSource instanceof ReadableByteChannel) {
			inChannel = ((ReadableByteChannel)inputDataSource);
		} else {
			inChannel = Channels.newChannel((InputStream)inputDataSource);
		}
	}
}
 
private void downloadFile(String fileUrl, Path destination) {
  if (destination.toFile().exists()) {
    Log.info(String.format("Skipping download for %s at %s because destination already exists", fileUrl,
        destination.toString()));
    return;
  }

  try {
    URL archiveUrl = new URL(fileUrl);
    ReadableByteChannel rbc = Channels.newChannel(archiveUrl.openStream());
    FileOutputStream fos = new FileOutputStream(String.valueOf(destination));
    Log.info(String.format("Downloading %s at %s", fileUrl, destination.toString()));
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    Log.info(String.format("Download complete for %s at %s", fileUrl, destination.toString()));
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
源代码5 项目: CloverETL-Engine   文件: S3SeekableByteChannel.java
private void openChannel(long position) throws IOException {
	try {
		S3Object object = S3Utils.getObject(uri, connection.getService(), position);
		is = S3Utils.getObjectInputStream(object);
		channel = Channels.newChannel(is);
		// use getInstanceLength(), not getContentLength()!
		this.size = object.getObjectMetadata().getInstanceLength();
	} catch (IOException ioe) {
		try {
			closeChannel();
		} catch (Exception e2) {
			ioe.addSuppressed(e2);
		}
		throw ioe;
	}
}
 
源代码6 项目: java-tutorial   文件: MyClassLoader.java
/**
 * 读入.class的字节,因此要使用字节流
 * * 一个Buffer对象是固定数量的数据的容器。其作用是一个存储器,或者分段运输区,在这里数据可被存储并在之后用于检索。
 * * 尽管缓冲区作用于它们存储的原始数据类型,但缓冲区十分倾向于处理字节。
 * NIO里,一个通道(channel)可以表示任何可以读写的对象。它的作用是为文件和套接口提供抽象
 *
 * @param file
 * @return
 * @throws IOException
 */
private byte[] getClassBytes(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    //通道(Channel)是一种途径,借助该途径,可以用最小的总开销来访问操作系统本身的 I/O 服务。
    //缓冲区(Buffer)则是通道内部用来发送和接收数据的端点。通道channel充当连接I/O服务的导管
    FileChannel channel = inputStream.getChannel();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // WritableByteChannel 接口以提供 master( )方法
    WritableByteChannel byteChannel = Channels.newChannel(outputStream);
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    while (true) {
        int i = channel.read(buffer);
        if (i == 0 || i == -1) {
            break;
        }
        buffer.flip();  //写模式转换成读模式

        byteChannel.write(buffer);
        buffer.clear();
    }
    inputStream.close();
    return outputStream.toByteArray();
}
 
源代码7 项目: olingo-odata4   文件: DataRequest.java
private Object getRawValueFromClient() throws DeserializerException {
  InputStream input = getODataRequest().getBody();
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  if (input != null) {
    try {
      ByteBuffer inBuffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
      ReadableByteChannel ic = Channels.newChannel(input);
      WritableByteChannel oc = Channels.newChannel(buffer);
      while (ic.read(inBuffer) > 0) {
        inBuffer.flip();
        oc.write(inBuffer);
        inBuffer.rewind();
      }
      return buffer.toByteArray();
    } catch (IOException e) {
      throw new ODataRuntimeException("Error on reading content");
    }
  }
  return null;
}
 
源代码8 项目: j2objc   文件: ChannelsTest.java
public void testNewInputStreamReadableByteChannel() throws Exception {
    ByteBuffer readbcbuf = ByteBuffer.allocateDirect(this.testNum);
    byte[] readbuf = new byte[this.testNum];
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel readbc = this.fins.getChannel();
    assertEquals(this.fileSize, this.fins.available());
    assertTrue(readbc.isOpen());
    InputStream testins = Channels.newInputStream(readbc);
    // read in testins and fins use the same pointer
    testins.read(readbuf);
    assertEquals(this.fins.available(), this.fileSize - this.testNum);
    int readNum = readbc.read(readbcbuf);
    assertEquals(readNum, this.testNum);
    assertEquals(this.fins.available(), this.fileSize - this.testNum * 2);
    testins.read(readbuf);
    assertEquals(this.fins.available(), this.fileSize - this.testNum * 3);
    // readbc.close() affect testins
    readbc.close();
    assertFalse(readbc.isOpen());
    try {
        testins.read(readbuf);
        fail();
    } catch (ClosedChannelException e) {
        // correct
    }
}
 
源代码9 项目: CloverETL-Engine   文件: SQLScriptParserTest.java
private static List<String> parse(int expectedCount, String str, String delimiter, boolean backslashQuoteEscaping) throws IOException {
	Charset charset = Charset.defaultCharset();
	
	SQLScriptParser sqlScriptParser = new SQLScriptParser();
	sqlScriptParser.setBackslashQuoteEscaping(backslashQuoteEscaping);
	sqlScriptParser.setDelimiter(delimiter);
	
	sqlScriptParser.setStringInput(str);
	parse(expectedCount, sqlScriptParser);
	
	ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes(charset));
	sqlScriptParser.setStreamInput(stream, charset);
	parse(expectedCount, sqlScriptParser);
	
	ByteArrayInputStream streamForChannel = new ByteArrayInputStream(str.getBytes(charset));
	ReadableByteChannel channel = Channels.newChannel(streamForChannel);
	sqlScriptParser.setChannelInput(channel, charset);
	return parse(expectedCount, sqlScriptParser);
}
 
源代码10 项目: nd4j   文件: BaseLoader.java
/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
    if (blob == null)
        return null;
    try(InputStream is = blob.getBinaryStream()) {
        ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
        ReadableByteChannel readableByteChannel = Channels.newChannel(is);
        readableByteChannel.read(direct);
        Buffer byteBuffer = (Buffer) direct;
        byteBuffer.rewind();
        return BinarySerde.toArray(direct);
    } catch (Exception e) {
       throw new RuntimeException(e);
    }


}
 
源代码11 项目: CloverETL-Engine   文件: DelimitedDataParser.java
@Override
public void setDataSource(Object inputDataSource) {
	if (releaseDataSource)	releaseDataSource();

	decoder.reset();// reset CharsetDecoder
	dataBuffer.clear();
       dataBuffer.flip();
	charBuffer.clear();
	charBuffer.flip();
	recordCounter = 1;// reset record counter
	bytesProcessed = 0;

	if (inputDataSource == null) {
		isEof = true;
	} else {
		isEof = false;
		if (inputDataSource instanceof ReadableByteChannel) {
			reader = ((ReadableByteChannel)inputDataSource);
		} else {
			reader = Channels.newChannel((InputStream)inputDataSource);
		}
	}
}
 
源代码12 项目: product-microgateway   文件: TestInterceptor.java
public String getByteChannel(Request request) {
    try {
        ByteChannel byteChannel = request.getByteChannel();
        InputStream in = Channels.newInputStream(byteChannel);
        StringBuilder textBuilder = new StringBuilder();
        try (Reader reader = new BufferedReader(
                new InputStreamReader(in, Charset.forName(StandardCharsets.UTF_8.name())))) {
            int c = 0;
            while ((c = reader.read()) != -1) {
                textBuilder.append((char) c);
            }
            return textBuilder.toString();
        }
    } catch (InterceptorException | IOException e) {
        log.error("Error while reading the the byte channel", e);
    }
    return "";
}
 
源代码13 项目: tus-java-server   文件: DiskStorageService.java
@Override
public InputStream getUploadedBytes(UploadId id) throws IOException, UploadNotFoundException {
    InputStream inputStream = null;
    UploadInfo uploadInfo = getUploadInfo(id);
    if (UploadType.CONCATENATED.equals(uploadInfo.getUploadType()) && uploadConcatenationService != null) {
        inputStream = uploadConcatenationService.getConcatenatedBytes(uploadInfo);

    } else {
        Path bytesPath = getBytesPath(id);
        //If bytesPath is not null, we know this is a valid Upload URI
        if (bytesPath != null) {
            inputStream = Channels.newInputStream(FileChannel.open(bytesPath, READ));
        }
    }

    return inputStream;
}
 
源代码14 项目: FairEmail   文件: FileUtil.java
/**
 * Copies data from the input channel to the output file channel.
 *
 * @param input  the input channel to copy.
 * @param output the output channel to copy.
 * @throws IOException if there is an I/O error.
 */
@SuppressLint("LambdaLast")
public static void copy(@NonNull ReadableByteChannel input, @NonNull FileChannel output)
        throws IOException {
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            output.transferFrom(input, 0, Long.MAX_VALUE);
        } else {
            InputStream inputStream = Channels.newInputStream(input);
            OutputStream outputStream = Channels.newOutputStream(output);
            int length;
            byte[] buffer = new byte[1024 * 4];
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        }
        output.force(false);
    } finally {
        input.close();
        output.close();
    }
}
 
源代码15 项目: JavaFM   文件: ML100kRatingPredictionExample.java
public static SimpleListWiseFMData getRecommendationDataset(String file) throws IOException {
    SimpleListWiseFMData dataset = new SimpleListWiseFMData(NUM_USERS + NUM_ITEMS);

    if (!new File(file).exists()) {
        URL url = new URL("http://files.grouplens.org/datasets/movielens/ml-100k/" + file);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    }

    InputStream is = new FileInputStream(file);

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
        reader.lines().forEach(line -> {
            String[] tokens = line.split("\t");
            int u = parseInt(tokens[0]) - 1;
            int i = parseInt(tokens[1]) - 1 + NUM_USERS;
            double r = parseDouble(tokens[2]);

            dataset.add(new FMInstance(r, new int[]{u, i}, new double[]{1.0, 1.0}), u);
        });
    }

    return dataset;
}
 
源代码16 项目: onlineJavaIde   文件: ClassClassLoader.java
private byte[] getClassFileBytes(String classFile) throws Exception {
    //采用NIO读取
    FileInputStream fis = new FileInputStream(classFile);
    FileChannel fileC = fis.getChannel();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    WritableByteChannel outC = Channels.newChannel(baos);
    ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
    while (true) {
        int i = fileC.read(buffer);
        if (i == 0 || i == -1) {
            break;
        }
        buffer.flip();
        outC.write(buffer);
        buffer.clear();
    }
    fis.close();
    return baos.toByteArray();
}
 
源代码17 项目: DataflowTemplates   文件: SchemaUtils.java
/**
 * The {@link SchemaUtils#getGcsFileAsString(String)} reads a file from GCS and returns it as a
 * string.
 *
 * @param filePath path to file in GCS
 * @return contents of the file as a string
 * @throws IOException thrown if not able to read file
 */
public static String getGcsFileAsString(String filePath) {
  MatchResult result;
  try {
    result = FileSystems.match(filePath);
    checkArgument(
        result.status() == MatchResult.Status.OK && !result.metadata().isEmpty(),
        "Failed to match any files with the pattern: " + filePath);

    List<ResourceId> rId =
        result.metadata().stream()
            .map(MatchResult.Metadata::resourceId)
            .collect(Collectors.toList());

    checkArgument(rId.size() == 1, "Expected exactly 1 file, but got " + rId.size() + " files.");

    Reader reader =
        Channels.newReader(FileSystems.open(rId.get(0)), StandardCharsets.UTF_8.name());

    return CharStreams.toString(reader);

  } catch (IOException ioe) {
    LOG.error("File system i/o error: " + ioe.getMessage());
    throw new RuntimeException(ioe);
  }
}
 
源代码18 项目: docker-maven-plugin   文件: UnixSocket.java
@Override
public InputStream getInputStream() throws IOException {
    if (!channel.isOpen()) {
        throw new SocketException("Socket is closed");
    }

    if (!channel.isConnected()) {
        throw new SocketException("Socket is not connected");
    }

    if (inputShutdown) {
        throw new SocketException("Socket input is shutdown");
    }

    return new FilterInputStream(Channels.newInputStream(channel)) {
        @Override
        public void close() throws IOException {
            shutdownInput();
        }
    };
}
 
源代码19 项目: jkube   文件: UnixSocket.java
@Override
public InputStream getInputStream() throws IOException {
    if (!channel.isOpen()) {
        throw new SocketException("Socket is closed");
    }

    if (!channel.isConnected()) {
        throw new SocketException("Socket is not connected");
    }

    if (inputShutdown) {
        throw new SocketException("Socket input is shutdown");
    }

    return new FilterInputStream(Channels.newInputStream(channel)) {
        @Override
        public void close() throws IOException {
            shutdownInput();
        }
    };
}
 
源代码20 项目: olingo-odata4   文件: ODataNettyHandlerImpl.java
/** 
 * Copy OData content to netty content
 * @param input
 * @param response
 */
static void copyContent(final ReadableByteChannel input, final HttpResponse response) {
  try (WritableByteChannel output = Channels.newChannel(new ByteBufOutputStream(((HttpContent)response).content()))){
      ByteBuffer inBuffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);
      while (input.read(inBuffer) > 0) {
        inBuffer.flip();
        output.write(inBuffer);
        inBuffer.clear();
      }
      closeStream(output);
    } catch (IOException e) {
      throw new ODataRuntimeException("Error on reading request content", e);
    } finally {
      closeStream(input);
    }
}
 
源代码21 项目: lucene-solr   文件: CdcrTransactionLog.java
/**
 * Re-open the output stream of the tlog and position
 * the file pointer at the end of the file. It assumes
 * that the tlog is non-empty and that the tlog's header
 * has been already read.
 */
synchronized void reopenOutputStream() {
  try {
    if (debug) {
      log.debug("Re-opening tlog's output stream: {}", this);
    }

    raf = new RandomAccessFile(this.tlogFile, "rw");
    channel = raf.getChannel();
    long start = raf.length();
    raf.seek(start);
    os = Channels.newOutputStream(channel);
    fos = new FastOutputStream(os, new byte[65536], 0);
    fos.setWritten(start);    // reflect that we aren't starting at the beginning
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }
}
 
源代码22 项目: CloverETL-Engine   文件: JsonExtract.java
/**
 * Switch to the next source file.
 * 
 * @return
 * @throws JetelException
 */
private boolean nextSource() throws JetelException {
	ReadableByteChannel stream = null;
	while (readableChannelIterator.hasNext()) {
		autoFilling.resetSourceCounter();
		autoFilling.resetGlobalSourceCounter();
		stream = readableChannelIterator.nextChannel();
		if (stream == null)
			continue; // if record no record found
		autoFilling.setFilename(readableChannelIterator.getCurrentFileName());
		long fileSize = 0;
		Date fileTimestamp = null;
		if (autoFilling.getFilename() != null
				&& !readableChannelIterator.isGraphDependentSource()) {
			try {
				File tmpFile = FileUtils.getJavaFile(getGraph().getRuntimeContext().getContextURL(), autoFilling.getFilename());
				long timestamp = tmpFile.lastModified();
				fileTimestamp = timestamp == 0 ? null : new Date(timestamp);
				fileSize = tmpFile.length();
			} catch (Exception e) {
				//do nothing - the url is not regular file
			}
		}
		autoFilling.setFileSize(fileSize);
		autoFilling.setFileTimestamp(fileTimestamp);				
		m_inputSource = new InputSource(Channels.newReader(stream, charset));
		return true;
	}
	readableChannelIterator.blankRead();
	return false;
}
 
源代码23 项目: SpringBoot2.0   文件: MavenWrapperDownloader.java
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
源代码24 项目: CloverETL-Engine   文件: SFTPOperationHandler.java
@Override
public ReadableByteChannel read() throws IOException {
	SFTPSession session = null;
	try {
		session = connect(uri);
		return Channels.newChannel(new SFTPInputStream(session.channel.get(getPath(uri)), session));
	} catch (SftpException e) {
		disconnectQuietly(session);
		throw new IOException(e);
	}
}
 
源代码25 项目: blog-tutorials   文件: MavenWrapperDownloader.java
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
  URL website = new URL(urlString);
  ReadableByteChannel rbc;
  rbc = Channels.newChannel(website.openStream());
  FileOutputStream fos = new FileOutputStream(destination);
  fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  fos.close();
  rbc.close();
}
 
@Override
protected void doSerialize(ArrowRecordBatch arrowRecordBatch, JsonGenerator jgen, SerializerProvider provider)
        throws IOException
{
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MessageSerializer.serialize(new WriteChannel(Channels.newChannel(out)), arrowRecordBatch);
        jgen.writeBinary(out.toByteArray());
    }
    finally {
        arrowRecordBatch.close();
    }
}
 
源代码27 项目: SpringBootLearn   文件: MavenWrapperDownloader.java
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
public ReadableByteChannel getReadableChannel() throws UnsupportedEncodingException {
	byte[] byteArray;
	if (StringUtils.isEmpty(charset)) {
		byteArray = data.getBytes(Defaults.DataFormatter.DEFAULT_CHARSET_ENCODER);
	} else {
		byteArray = data.getBytes(charset);
	}
	
	return Channels.newChannel(new ByteArrayInputStream(byteArray));
}