小编Man*_*uah的帖子

java中的线程安全InputStream

如何创建一个Thread安全的InputStream.在多线程操作中,inputStream数据被破坏,所以如何使我的inputStream线程安全.将使用以下代码工作

public class SynchronizedInputStream  extends InputStream{

    private InputStream in;

    private SynchronizedInputStream( InputStream in ) {
        this.in = in;
    }

    /* ... method for every InputStream type to use */
    public  static InputStream createInputStream( InputStream in) {
        return new SynchronizedInputStream( in);
    }

    public static InputStream createPushBackInputStream(InputStream in,int BUFSIZE){
        return new SynchronizedInputStream(new PushbackInputStream(in,BUFSIZE));
    }

    /* Wrap all InputStream methods Used */

    public int read(){
        synchronized (this) {
            try {
                return in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    } …
Run Code Online (Sandbox Code Playgroud)

java multithreading inputstream

11
推荐指数
2
解决办法
1万
查看次数

读取NANOHTTPD的InputStream会产生Socket TimeOut异常

我试图使用以下代码从IHTTPSession.getInputStream()读取InputStream,但每次都给出Socket TimeOut异常.

private String readInStream(InputStream in){

        StringBuffer outBuffer=new StringBuffer();
        BufferedInputStream bis=new BufferedInputStream(in);
        try {
            while(bis.available()>0){
                int ch= bis.read();
                outBuffer.append((char)ch);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
        return outBuffer.toString();
    }
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下方法,但出现了同样的异常

private String readInStream(InputStream in){
        String line="";
        StringBuffer outBuffer=new StringBuffer();

        BufferedReader rd=new BufferedReader(new InputStreamReader(in));

        try {
            while((line=rd.readLine()) != null){
                outBuffer.append(line);
            }
        } catch (IOException e) {
            Log.e("IOException", "IOException in readInStream:");
            e.printStackTrace();
        }

        Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
        return outBuffer.toString();
    }
Run Code Online (Sandbox Code Playgroud)

inputstream bufferedinputstream bufferedreader nanohttpd

7
推荐指数
1
解决办法
866
查看次数

使用available()时无法获取整个数据

我有时候在读取inputStream时没有获得整个数据(收到somtime完整数据).

 private String readInputStream(InputStream in) {
            PushbackInputStream inputStream = (PushbackInputStream) in;
            StringBuffer outputBuffer = null;
            try {
                int size = inputStream.available();
                    outputBuffer = new StringBuffer(size);
                    // append the data into the stringBuilder
                    for (int j = 0; j < size; j++) {
                        int ch = inputStream.read();
                        outputBuffer.append((char) ch);
                    }
            } catch (IOException ioe) {
                Log.e("error", "IOException: " +
                        ioe.getMessage());
            }
            if (outputBuffer != null) {
                return outputBuffer.toString();
        }
Run Code Online (Sandbox Code Playgroud)

我应该读取输入流,直到inputStream.available()为零..?inputStream中的数据是大的.Plz建议使用示例代码的一些替代

java inputstream

5
推荐指数
1
解决办法
74
查看次数

生成JUnit测试报告

是否有可能在eclipse(kepler)的html中有JUnit Test报告数据,它显示了许多测试用例和传递的数量,测试用例的意图等等.

eclipse junit

5
推荐指数
1
解决办法
9447
查看次数

保存EMF模型

我是新手EMF并试图保存EMF模型如下: -

public void saveData(File file, Device device) throws IOException {  

        final ResourceSet resourceSet = new ResourceSetImpl();

        // Use XMI resource
        System.out.println("file path in  saveData " +file.getPath());          
        Resource xmiResource = resourceSet.createResource(URI.createFileURI(file.getPath() + ".xmi"));
        xmiResource.getContents().add(device);
        xmiResource.save(null);

        // Use XML resource instead
        Resource xmlResource = resourceSet.createResource(URI.createFileURI(file.getPath() + ".xml"));
        xmlResource.getContents().add(device);
        xmlResource.save(null);

        }
Run Code Online (Sandbox Code Playgroud)

但是在指定的路径中没有创建文件.装载代码是: -

public Device loadData(String fileName) {


        final ResourceSet resourceSet = new ResourceSetImpl();


            // Use XMI resource

            Resource xmiResource;
            System.out.println("filename" + fileName);
            try {
            xmiResource = resourceSet.getResource(URI.createFileURI(fileName + ".xmi"),true);

                xmiResource.load(null);
            } …
Run Code Online (Sandbox Code Playgroud)

java emf

4
推荐指数
1
解决办法
2645
查看次数

如何使用JAX-RS 2.0客户端API发布原始数据

我有一个原始的inputStream,HttpServletRequest我需要使用JAX-RS客户端将收到的整个inputStream以及头文件发送到另一个servlet(实际上).

Client client = ClientBuilder.newClient();

WebTarget reTarget = client.target("http://localhost:8100/Server");
Invocation retargetInvocation = reTarget.request().??
Response.Response response = retargetInvocation.invoke();
Run Code Online (Sandbox Code Playgroud)

我应该如何为post请求进行调用Invocation retargetInvocation = reTarget.request().post(Entity<T>).inputStream可以包含任何Raw数据

jax-rs jersey-client jersey-2.0

4
推荐指数
1
解决办法
5253
查看次数

将字节数组转换为对象会增加大小吗?

我的代码如下..

private Object populateObj(InputStream inputStream) {
        int i=0;
        Object resObject = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int next = inputStream.read();
            while (next > -1) {
                i++;
                bos.write(next);
                next = inputStream.read();
            }
            bos.flush();
            byte[] result = bos.toByteArray();
            System.out.println("Size of byte array 1: "+result.length);
            System.out.println("Result is []");
            for(int j=0;j<result.length;j++){
                System.out.println(result[j]);
            }
            resObject = result;
            byte[]  result2=toByteArray(resObject);
            System.out.println("Size of byte array 2 : "+result2.length);
            System.out.println("Result2 is []");
            for(int j=0;j<result.length;j++){
                System.out.println(result2[j]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("size of …
Run Code Online (Sandbox Code Playgroud)

java arrays bytearray

3
推荐指数
1
解决办法
684
查看次数

配置错误。连接到数据库时找不到类 [org.apache.derby.jdbc.EmbeddedDriver]

我正在使用命令mvn exec:javacmd.My Persistence.xml运行我的应用程序属性是:

<properties>
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:derby:DB;create=true" />
        <property name="javax.persistence.jdbc.user" value="test" />
        <property name="javax.persistence.jdbc.password" value="test" />

        <!-- EclipseLink should create the database schema automatically -->
        <property name="eclipselink.ddl-generation" value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode"
            value="database" />
        <property name="connection.autocommit" value="false"/>
    </properties>
Run Code Online (Sandbox Code Playgroud)

我的 pom.xml 依赖项和构建属性是:-

 <dependencies>
        <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>
    <dependency>
        <groupId>org.glassfish.jersey.connectors</groupId>
        <artifactId>jersey-grizzly-connector</artifactId>
        <version>2.16</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.16</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-servlet</artifactId>
        <version>2.16</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-framework</artifactId>
        <version>2.3.18</version>
    </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId> …
Run Code Online (Sandbox Code Playgroud)

java eclipse jpa derby maven

2
推荐指数
1
解决办法
2951
查看次数