/**
  * compare the byte-array representation of two TaskObjects.
  *
  * @param f TaskObject
  * @param s TaskObject
  * @return true if the two arguments have the same byte-array
  */
 private boolean sameBytes(TaskObject f, TaskObject s) {
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ObjectOutput out = null;
   try {
     out = new ObjectOutputStream(bos);
     out.writeObject(f);
     byte[] fs = bos.toByteArray();
     out.writeObject(s);
     byte[] ss = bos.toByteArray();
     if (fs.length != ss.length) return false;
     for (int i = 0; i < fs.length; i++) {
       if (fs[i] != ss[i]) return false;
     }
     return true;
   } catch (IOException e) {
     e.printStackTrace();
     return false;
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (IOException e) {
         // ignore
       }
       try {
         bos.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
 }
Пример #2
0
 /**
  * Load UTF8withBOM or any ansi text file.
  *
  * @param filename
  * @return
  * @throws java.io.IOException
  */
 public static String loadFileAsString(String filename) throws java.io.IOException {
   final int BUFLEN = 1024;
   BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN);
   try {
     ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN);
     byte[] bytes = new byte[BUFLEN];
     boolean isUTF8 = false;
     int read, count = 0;
     while ((read = is.read(bytes)) != -1) {
       if (count == 0
           && bytes[0] == (byte) 0xEF
           && bytes[1] == (byte) 0xBB
           && bytes[2] == (byte) 0xBF) {
         isUTF8 = true;
         baos.write(bytes, 3, read - 3); // drop UTF8 bom marker
       } else {
         baos.write(bytes, 0, read);
       }
       count += read;
     }
     return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray());
   } finally {
     try {
       is.close();
     } catch (Exception ex) {
     }
   }
 }
Пример #3
0
  /** execute method之后,取返回的inputstream */
  private static ByteArrayInputStream execute4InputStream(HttpClient client) throws Exception {
    int statusCode = client.getResponseCode();
    if (statusCode != HttpURLConnection.HTTP_OK) {
      throw new EnvException("Method failed: " + statusCode);
    }
    InputStream in = client.getResponseStream();
    if (in == null) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      Utils.copyBinaryTo(in, out);

      // 看一下传过来的byte[]是不是DesignProcessor.INVALID,如果是的话,就抛Exception
      byte[] bytes = out.toByteArray();
      // carl:格式一致传中文
      String message = new String(bytes, EncodeConstants.ENCODING_UTF_8);
      if (ComparatorUtils.equals(message, RemoteDeziConstants.NO_SUCH_RESOURCE)) {
        return null;
      } else if (ComparatorUtils.equals(message, RemoteDeziConstants.INVALID_USER)) {
        throw new EnvException(RemoteDeziConstants.INVALID_USER);
      } else if (ComparatorUtils.equals(message, RemoteDeziConstants.FILE_LOCKED)) {
        JOptionPane.showMessageDialog(null, Inter.getLocText("FR-Designer_file-is-locked"));
        return null;
      } else if (message.startsWith(RemoteDeziConstants.RUNTIME_ERROR_PREFIX)) {
      }
      return new ByteArrayInputStream(bytes);
    } finally {
      in.close();
      out.close();
      client.release();
    }
  }
Пример #4
0
  /** Internal method. Connect to searchd, send request, get response as DataInputStream. */
  private DataInputStream _DoRequest(int command, int version, ByteArrayOutputStream req) {
    /* connect */
    Socket sock = _Connect();
    if (sock == null) return null;

    /* send request */
    byte[] reqBytes = req.toByteArray();
    try {
      DataOutputStream sockDS = new DataOutputStream(sock.getOutputStream());
      sockDS.writeShort(command);
      sockDS.writeShort(version);
      sockDS.writeInt(reqBytes.length);
      sockDS.write(reqBytes);

    } catch (Exception e) {
      _error = "network error: " + e;
      _connerror = true;
      return null;
    }

    /* get response */
    byte[] response = _GetResponse(sock);
    if (response == null) return null;

    /* spawn that tampon */
    return new DataInputStream(new ByteArrayInputStream(response));
  }
Пример #5
0
 /**
  * Loads a resource from the classloader (e.g. the .jar file) into a byte array.
  *
  * @param cl Classloader resource comes from
  * @param sPath Path of resource (must begin with /)
  * @return Byte array containing file in memory
  * @throws IOException If it doesn't exist or there is any other error loading
  */
 public static byte[] loadResource(ClassLoader cl, String sPath) throws IOException {
   if (!sPath.startsWith("/"))
     throw new FileNotFoundException("Resource must be absolute path: " + sPath);
   sPath = sPath.substring(1);
   URL u = cl.getResource(sPath);
   if (u == null) throw new FileNotFoundException("Resource not found: " + sPath);
   URLConnection uc = u.openConnection();
   uc.connect();
   int iLength = uc.getContentLength();
   if (iLength > -1) // Sometimes getContentLength returns -1, sometimes it doesn't
   {
     byte[] abContent = new byte[iLength];
     InputStream is = uc.getInputStream();
     int iStart = 0;
     while (true) {
       int iRead = is.read(abContent, iStart, abContent.length - iStart);
       if (iRead == 0) throw new IOException("Resource length mismatch");
       iStart += iRead;
       if (iStart == abContent.length) break;
     }
     is.close();
     return abContent;
   } else {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     copy(uc.getInputStream(), baos, false);
     return baos.toByteArray();
   }
 }
  /** @throws IOException If failed. */
  private void initFavicon() throws IOException {
    assert favicon == null;

    InputStream in = getClass().getResourceAsStream("favicon.ico");

    if (in != null) {
      BufferedInputStream bis = new BufferedInputStream(in);

      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      try {
        byte[] buf = new byte[2048];

        while (true) {
          int n = bis.read(buf);

          if (n == -1) break;

          bos.write(buf, 0, n);
        }

        favicon = bos.toByteArray();
      } finally {
        U.closeQuiet(bis);
      }
    }
  }
  private Icon makeIcon(final String gifFile) throws IOException {
    /* Copy resource into a byte array.  This is
     * necessary because several browsers consider
     * Class.getResource a security risk because it
     * can be used to load additional classes.
     * Class.getResourceAsStream just returns raw
     * bytes, which we can convert to an image.
     */
    InputStream resource = MyImageView.class.getResourceAsStream(gifFile);

    if (resource == null) {
      System.err.println(MyImageView.class.getName() + "/" + gifFile + " not found!!.");
      return null;
    }
    BufferedInputStream in = new BufferedInputStream(resource);
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int n;
    while ((n = in.read(buffer)) > 0) {
      out.write(buffer, 0, n);
    }
    in.close();
    out.flush();

    buffer = out.toByteArray();
    if (buffer.length == 0) {
      System.err.println("warning: " + gifFile + " is zero-length");
      return null;
    }
    return new ImageIcon(buffer);
  }
Пример #8
0
  /**
   * 将用户反馈发送至服务器
   *
   * @param feedBackInfo 用户反馈
   * @return 发送成功返回true
   * @throws Exception 异常
   */
  public static boolean sendFeedBack(FeedBackInfo feedBackInfo) throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    // 把tableData写成xml文件到out
    DavXMLUtils.writeXMLFeedBackInfo(feedBackInfo, out);
    InputStream input = postBytes2ServerB(out.toByteArray());
    return input != null;
  }
Пример #9
0
 /** Reads the chunk of data to be imported from the request's input stream. */
 private byte[] readChunk(HttpServletRequest req, int chunkSize) throws IOException {
   ServletInputStream requestStream = req.getInputStream();
   String contentType = req.getHeader(ProtocolConstants.HEADER_CONTENT_TYPE);
   if (contentType.startsWith("multipart")) // $NON-NLS-1$
   return readMultiPartChunk(requestStream, contentType);
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream(chunkSize);
   IOUtilities.pipe(requestStream, outputStream, false, false);
   return outputStream.toByteArray();
 }
Пример #10
0
 static byte[] Cmd_Challenge() throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(74);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("CHALLENGE");
   dataoutputstream.writeByte(0);
   return bytearrayoutputstream.toByteArray();
 }
  public static int sizeInBytes(Object obj) throws IOException {
    ByteArrayOutputStream byteObject = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteObject);
    objectOutputStream.writeObject(obj);
    objectOutputStream.flush();
    objectOutputStream.close();
    byteObject.close();

    return byteObject.toByteArray().length;
  }
Пример #12
0
 /**
  * Read an InputStream into a byte array.
  *
  * @param is InputStream
  * @return byte array
  */
 public static byte[] inputStreamToByteArray(InputStream is) {
   try {
     final ByteArrayOutputStream os = new ByteArrayOutputStream();
     copyStream(new BufferedInputStream(is), os);
     os.close();
     return os.toByteArray();
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
Пример #13
0
 static byte[] Cmd_Finish() throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(75);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("FINISH");
   dataoutputstream.writeByte(0);
   return bytearrayoutputstream.toByteArray();
 }
Пример #14
0
 static byte[] Cmd_Accept(int i) throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(75);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("ACCEPT");
   dataoutputstream.writeByte(0);
   writeInt(i, dataoutputstream);
   return bytearrayoutputstream.toByteArray();
 }
 private Class<?> getClassFromStream(
     final InputStream stream, final String classname, final File container)
     throws IOException, SecurityException {
   final ByteArrayOutputStream baos = new ByteArrayOutputStream();
   int bytesRead = -1;
   final byte[] buffer = new byte[8192];
   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
     baos.write(buffer, 0, bytesRead);
   }
   final byte[] classData = baos.toByteArray();
   return this.defineClassFromData(container, classData, classname);
 }
Пример #16
0
 static byte[] Cmd_Abort(String s) throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(75);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("ABORT");
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes(s);
   dataoutputstream.writeByte(0);
   return bytearrayoutputstream.toByteArray();
 }
Пример #17
0
 static byte[] Cmd_Block(byte abyte0[]) throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(75);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("BLOCK");
   dataoutputstream.writeByte(0);
   writeShort(abyte0.length, dataoutputstream);
   dataoutputstream.write(abyte0, 0, abyte0.length);
   return bytearrayoutputstream.toByteArray();
 }
Пример #18
0
 static byte[] Cmd_File(int i, byte abyte0[]) throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(75);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeBytes("FILE");
   dataoutputstream.writeByte(0);
   writeInt(i, dataoutputstream);
   dataoutputstream.write(abyte0, 0, abyte0.length);
   return bytearrayoutputstream.toByteArray();
 }
Пример #19
0
  public void run() {
    URL url;
    Base64Encoder base64 = new Base64Encoder();

    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      System.err.println("Invalid URL");
      return;
    }

    try {
      conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + base64.encode(user + ":" + pass));
      httpIn = new BufferedInputStream(conn.getInputStream(), 8192);
    } catch (IOException e) {
      System.err.println("Unable to connect: " + e.getMessage());
      return;
    }

    int prev = 0;
    int cur = 0;

    try {
      while (keepAlive && (cur = httpIn.read()) >= 0) {
        if (prev == 0xFF && cur == 0xD8) {
          jpgOut = new ByteArrayOutputStream(8192);
          jpgOut.write((byte) prev);
        }
        if (jpgOut != null) {
          jpgOut.write((byte) cur);
        }
        if (prev == 0xFF && cur == 0xD9) {
          synchronized (curFrame) {
            curFrame = jpgOut.toByteArray();
          }
          frameAvailable = true;
          jpgOut.close();
        }
        prev = cur;
      }
    } catch (IOException e) {
      System.err.println("I/O Error: " + e.getMessage());
    }
    try {
      jpgOut.close();
      httpIn.close();
    } catch (IOException e) {
      System.err.println("Error closing streams: " + e.getMessage());
    }
    conn.disconnect();
  }
Пример #20
0
  private byte[] readFully(InputStream istream) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int num = 0;

    while ((num = istream.read(buf)) != -1) {
      bout.write(buf, 0, num);
    }

    byte[] ret = bout.toByteArray();

    return ret;
  }
Пример #21
0
  public void run() {
    while (true) {
      try {
        ByteArrayOutputStream b_out = new ByteArrayOutputStream();
        ObjectOutputStream o_out = new ObjectOutputStream(b_out);
        o_out.writeObject(new BroadcastMessage(username));
        DatagramPacket packet =
            new DatagramPacket(b_out.toByteArray(), b_out.toByteArray().length, group, 6969);
        socket = new DatagramSocket(multicastPort);
        socket.send(packet);
        socket.close();

        // sleep for a while
        try {
          sleep(FIVE_SECONDS);
        } catch (InterruptedException e) {
        }
      } catch (IOException e) {
        e.printStackTrace();
        socket.close();
      }
    }
  }
Пример #22
0
  byte[] messageToBuffer(Message msg) throws Exception {
    ObjectOutputStream out;
    // BufferedOutputStream bos;

    out_stream.reset();
    // bos=new BufferedOutputStream(out_stream);
    out_stream.write(Version.version_id, 0, Version.version_id.length); // write the version
    // bos.write(Version.version_id, 0, Version.version_id.length); // write the version
    out = new ObjectOutputStream(out_stream);
    // out=new ObjectOutputStream(bos);
    msg.writeExternal(out);
    out.flush(); // needed if out buffers its output to out_stream
    return out_stream.toByteArray();
  }
Пример #23
0
 static byte[] Cmd_AccessGranted(int i, byte byte0) throws Exception {
   ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
   DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
   writeInt(-1, dataoutputstream);
   dataoutputstream.writeByte(78);
   dataoutputstream.writeByte(0);
   writeInt(i, dataoutputstream);
   dataoutputstream.writeByte(byte0);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeByte(0);
   dataoutputstream.writeByte(0);
   return bytearrayoutputstream.toByteArray();
 }
Пример #24
0
 /** TIFF Adobe ZIP support contributed by Jason Newton. */
 public byte[] zipUncompress(byte[] input) {
   ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
   byte[] buffer = new byte[1024];
   Inflater decompressor = new Inflater();
   decompressor.setInput(input);
   try {
     while (!decompressor.finished()) {
       int rlen = decompressor.inflate(buffer);
       imageBuffer.write(buffer, 0, rlen);
     }
   } catch (DataFormatException e) {
     IJ.log(e.toString());
   }
   decompressor.end();
   return imageBuffer.toByteArray();
 }
 private Class findRawClass(String className) throws ClassNotFoundException {
   try {
     String resourcePath = className.replace('.', '/') + ".class";
     InputStream resourceStream = getResourceAsStream(resourcePath);
     ByteArrayOutputStream rawByteStream = new ByteArrayOutputStream();
     byte[] buf = new byte[4096];
     int bytesread = 0;
     while ((bytesread = resourceStream.read(buf)) >= 0) {
       rawByteStream.write(buf, 0, bytesread);
     }
     resourceStream.close();
     byte[] rawBytes = rawByteStream.toByteArray();
     return super.defineClass(className, rawBytes, 0, rawBytes.length);
   } catch (Exception exc) {
     throw new ClassNotFoundException(className, exc);
   }
 }
Пример #26
0
 /** Object: size = variable */
 public static byte[] objectToArray(Object s) {
   try {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ObjectOutputStream oos = new ObjectOutputStream(baos);
     oos.writeObject(s);
     oos.close();
     baos.close();
     byte[] sb = baos.toByteArray();
     byte[] out = new byte[sb.length + 4];
     integerToArray(sb.length, out, 0);
     System.arraycopy(sb, 0, out, 4, sb.length);
     return out;
   } catch (IOException e) {
     e.printStackTrace();
     throw new RuntimeException("unable to serialize packet", e);
   }
 }
Пример #27
0
 /**
  * Checks for the next item.
  *
  * @return result of check
  * @throws IOException I/O exception
  */
 public boolean more() throws IOException {
   if (cache == null) {
     out.write(4);
     send(id);
     cache = new ArrayList<byte[]>();
     final ByteArrayOutputStream os = new ByteArrayOutputStream();
     while (in.read() > 0) {
       receive(in, os);
       cache.add(os.toByteArray());
       os.reset();
     }
     if (!ok()) throw new IOException(receive());
     pos = 0;
   }
   if (pos < cache.size()) return true;
   cache = null;
   return false;
 }
Пример #28
0
  /**
   * Zips byte array.
   *
   * @param input Input bytes.
   * @param initBufSize Initial buffer size.
   * @return Zipped byte array.
   * @throws IOException If failed.
   */
  public static byte[] zipBytes(byte[] input, int initBufSize) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(initBufSize);

    try (ZipOutputStream zos = new ZipOutputStream(bos)) {
      ZipEntry entry = new ZipEntry("");

      try {
        entry.setSize(input.length);

        zos.putNextEntry(entry);

        zos.write(input);
      } finally {
        zos.closeEntry();
      }
    }

    return bos.toByteArray();
  }
Пример #29
0
  public static byte[] loadResourceIntoByteArray(String resourcePath) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    InputStream inputStream = null;

    try {
      inputStream = Skygge.class.getResourceAsStream(resourcePath);
      byte[] byteChunk = new byte[8192];
      int n;

      while ((n = inputStream.read(byteChunk)) > 0) {
        outputStream.write(byteChunk, 0, n);
      }
    } finally {
      if (inputStream != null) {
        inputStream.close();
      }
    }
    return outputStream.toByteArray();
  }
Пример #30
0
  public static byte[] loadUrlIntoByteArray(String urlString) throws IOException {
    URL url = new URL(urlString);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    InputStream inputStream = null;

    try {
      inputStream = url.openStream();
      byte[] byteChunk = new byte[8192];
      int n;

      while ((n = inputStream.read(byteChunk)) > 0) {
        outputStream.write(byteChunk, 0, n);
      }
    } finally {
      if (inputStream != null) {
        inputStream.close();
      }
    }
    return outputStream.toByteArray();
  }