回到关于在套接字编程中使用C文件流的问题。我正在阅读它,看到评论不一-有人说这是不可靠的(即泄漏抽象?)。
有没有人对在套接字编程中使用C文件流有一种看法?
请看看我做了什么
private InputSource getContent(String fName) throws SAXException, IOException, ServiceException {
// Some code here
if(currentNodeRef != null)
{
ContentReader reader = contentService.getReader(currentNodeRef,ContentModel.PROP_CONTENT);
InputStream inputStream = null;
try
{
inputStream = new BufferedInputStream(reader.getContentInputStream(),16384);
return new InputSource(inputStream);
}
finally
{
if(inputStream!=null)
try
{
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return new InputSource();
}
Run Code Online (Sandbox Code Playgroud)
在我的parseDocument方法中,我调用了上面的方法.
parseDocRoot(getContent(fName),path);
Run Code Online (Sandbox Code Playgroud)
在parseDocRoot中
public void parseDocRoot (InputSource ins, String path) throws SAXException, IOException,
ParserConfigurationException, ServiceException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); …Run Code Online (Sandbox Code Playgroud) 我想在objective-c中读取一个大文件,并在iphone开发中使用我使用了以下代码
NSData *data = [[NSData alloc] initWithContentsOfFile:file];
unsigned char *readData[2000];
[data getBytes:readData range:NSMakeRange(1000,3000)]
Run Code Online (Sandbox Code Playgroud)
这可能会导致iphone应用程序崩溃,因为文件的大小很大.
我试图使用NSInputStream,但我找不到任何方法中的range参数,任何人都可以提供帮助吗?
谢谢,最诚挚的问候蒂米
我将在下面留下我原来的问题.我的问题是为什么以下行产生异常
(你可以在http://www.ideone.com/rfQLE上看到它运行)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CSQuick
{
class Program
{
static void Main(string[] args)
{
using (var ff = new MemoryStream())
using (var f = new StreamWriter(ff))
{
f.WriteLine("Hi");
using (TextReader ts = new StreamReader(ff))
{
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
来自ideone网站的例外(可能使用mono?我正在运行MSVS 2010 C#导致异常)
Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed.
at System.IO.MemoryStream.CheckIfClosedThrowDisposed () [0x00000] in <filename unknown>:0
at System.IO.MemoryStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] …Run Code Online (Sandbox Code Playgroud) 这个问题与我关于这个问题的新发现密切相关.
有没有什么办法来保存的数据流中的数据php://memory或php://temp手柄之间?我读过(我无法获取的某个地方)上述流的后续开放清除了现有数据.
$mem1 = fopen('php://memory', 'r+');
fwrite($mem1, 'hello world');
rewind($mem1);
fpassthru($mem1); // "hello world"
$mem2 = fopen('php://memory', 'r+');
rewind($mem2);
fpassthru($mem2); // empty
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,无论如何,在创建新句柄时,是否还要强制现有数据保持在流中?
(后者调用fpassthru()当然会转储,hello world因为这是可能的)
用于写入文件字符串和byte []数组的流类是什么?如果文件不存在,则需要打开文件以追加或创建新文件.
using (Stream s = new Stream("application.log")
{
s.Write("message")
s.Write(new byte[] { 1, 2, 3, 4, 5 });
}
Run Code Online (Sandbox Code Playgroud) 我有一个家庭作业,通过重定向标准I/O来创建一个带有客户端/服务器TCP套接字对的简单数据传输机制.我实际上有它工作,但当我尝试传输大文件(比如~5g)时,速度会急剧减慢.我正在使用BufferedInputStream和BufferedOutputStream,我想也许我可以在那里进行一些优化.我的服务器的代码是:
private static final int BUF_SIZE = 2047;
public static void main(String[] args) throws IOException{
/*
* Attempt to parse command line arguments.
* @require args[0] is an int
*/
int port = 0;
try {
port = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
System.err.println("Port must be an integer in range 0 - 65535.");
System.exit(-1);
}
/*
* Bind server socket to specified port number and wait for request.
* @require port >= 0 && port <= 65535
*/
ServerSocket welcomeSocket …Run Code Online (Sandbox Code Playgroud) 我正在实现一个接受图像流的wcf服务.但是当我运行它时,我正在获得异常.因为它试图在流完成之前获取流的长度.所以我想做的是缓冲流直到完成.但我无法找到任何如何做到这一点的例子......
有人可以帮忙吗?
我的代码到目前为止:
public String uploadUserImage(Stream stream)
{
Stream fs = stream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);// this causes exception
File.WriteAllBytes(filepath, bytes);
}
Run Code Online (Sandbox Code Playgroud) 如果这是一个好或坏的想法,我只是在徘徊:
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(in);
DataInputStream dis = new DataInputStream(in);
Run Code Online (Sandbox Code Playgroud)
现在我想从BufferedReader中读取.如果某个命令(只是一个字符串)到了,我想继续从DataInputStream中读取.
这有用吗?如果是的话,它被认为是好的还是坏的做法?
我知道输入流在Groovy中的这种块的末尾自动关闭:
def exec = ""
System.in.withReader {
println "input: "
exec = it.readLine()
}
Run Code Online (Sandbox Code Playgroud)
但如果我想做类似的事情,有没有办法重新打开流:
def exec = ""
while(!exec.equals("q")) {
System.in.withReader {
println "input: "
exec = it.readLine()
}
if(!exec.equals("q")) {
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我在第二次执行while循环时遇到此错误:
Exception in thread "main" java.io.IOException: Stream closed
Run Code Online (Sandbox Code Playgroud)
那么实现这一目标的最佳方法是什么?
谢谢.