public synchronized InputStream getBlockInputStream(Block b, long seekOffset) throws IOException {

    File blockFile = getBlockFile(b);
    RandomAccessFile blockInFile = new RandomAccessFile(blockFile, "r");
    if (seekOffset > 0) {
      blockInFile.seek(seekOffset);
    }
    return new FileInputStream(blockInFile.getFD());
  }
  public OutputStream getOutputStream() throws IOException {
    if (!PMS.get().isWindows()) {
      LOGGER.trace("Opening file " + linuxPipeName + " for writing...");
      RandomAccessFile raf = new RandomAccessFile(linuxPipeName, "rw");

      return new FileOutputStream(raf.getFD());
    }

    return mk.getWritable();
  }
  public InputStream getInputStream() throws IOException {
    if (!PMS.get().isWindows()) {
      LOGGER.trace("Opening file " + linuxPipeName + " for reading...");
      RandomAccessFile raf = new RandomAccessFile(linuxPipeName, "r");

      return new FileInputStream(raf.getFD());
    }

    return mk.getReadable();
  }
  public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile("classes/test/FileDescriptors.java", "r");
    testDescriptorValidity(raf, raf.getFD());

    FileInputStream fs1 = new FileInputStream("classes/test/FileDescriptors.java");
    testDescriptorValidity(fs1, fs1.getFD());

    File temp = File.createTempFile("Doppio-FileDescriptorsTest", ".txt");
    FileOutputStream fs2 = new FileOutputStream(temp);
    testDescriptorValidity(fs2, fs2.getFD());
    temp.delete(); // we don't support deleteOnExit either
  }
  /** Returns handles to the block file and its metadata file */
  public synchronized BlockInputStreams getTmpInputStreams(Block b, long blkOffset, long ckoff)
      throws IOException {

    DatanodeBlockInfo info = volumeMap.get(b);
    if (info == null) {
      throw new IOException("Block " + b + " does not exist in volumeMap.");
    }
    FSVolume v = info.getVolume();
    File blockFile = v.getTmpFile(b);
    RandomAccessFile blockInFile = new RandomAccessFile(blockFile, "r");
    if (blkOffset > 0) {
      blockInFile.seek(blkOffset);
    }
    File metaFile = getMetaFile(blockFile, b);
    RandomAccessFile metaInFile = new RandomAccessFile(metaFile, "r");
    if (ckoff > 0) {
      metaInFile.seek(ckoff);
    }
    return new BlockInputStreams(
        new FileInputStream(blockInFile.getFD()), new FileInputStream(metaInFile.getFD()));
  }
Exemple #6
0
 /** Synchs the file. */
 public void synch() throws IOException {
   synchronized (data) {
     try {
       data.getFD().sync();
     } catch (SyncFailedException e) {
       // A SyncFailedException seems to occur on some specific OS under
       // JDK 1.4.x.  We ignore the exception which reduces the robustness
       // of the journal file for the OS where this problem occurs.
       // Unfortunately there's no sane way to handle this excption when it
       // does occur.
     }
   }
 }
    @Override
    public void parseBody(Map<String, String> files) throws IOException, ResponseException {
      RandomAccessFile randomAccessFile = null;
      BufferedReader in = null;
      try {

        randomAccessFile = getTmpBucket();

        long size;
        if (headers.containsKey("content-length")) {
          size = Integer.parseInt(headers.get("content-length"));
        } else if (splitbyte < rlen) {
          size = rlen - splitbyte;
        } else {
          size = 0;
        }

        // Now read all the body and write it to f
        byte[] buf = new byte[512];
        while (rlen >= 0 && size > 0) {
          rlen = inputStream.read(buf, 0, (int) Math.min(size, 512));
          size -= rlen;
          if (rlen > 0) {
            randomAccessFile.write(buf, 0, rlen);
          }
        }

        // Get the raw body as a byte []
        ByteBuffer fbuf =
            randomAccessFile
                .getChannel()
                .map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
        randomAccessFile.seek(0);

        // Create a BufferedReader for easily reading it as string.
        InputStream bin = new FileInputStream(randomAccessFile.getFD());
        in = new BufferedReader(new InputStreamReader(bin));

        // If the method is POST, there may be parameters
        // in data section, too, read it:
        if (Method.POST.equals(method)) {
          String contentType = "";
          String contentTypeHeader = headers.get("content-type");

          StringTokenizer st = null;
          if (contentTypeHeader != null) {
            st = new StringTokenizer(contentTypeHeader, ",; ");
            if (st.hasMoreTokens()) {
              contentType = st.nextToken();
            }
          }

          if ("multipart/form-data".equalsIgnoreCase(contentType)) {
            // Handle multipart/form-data
            if (!st.hasMoreTokens()) {
              throw new ResponseException(
                  Response.Status.BAD_REQUEST,
                  "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
            }

            String boundaryStartString = "boundary=";
            int boundaryContentStart =
                contentTypeHeader.indexOf(boundaryStartString) + boundaryStartString.length();
            String boundary =
                contentTypeHeader.substring(boundaryContentStart, contentTypeHeader.length());
            if (boundary.startsWith("\"") && boundary.endsWith("\"")) {
              boundary = boundary.substring(1, boundary.length() - 1);
            }

            decodeMultipartData(boundary, fbuf, in, parms, files);
          } else {
            String postLine = "";
            StringBuilder postLineBuffer = new StringBuilder();
            char pbuf[] = new char[512];
            int read = in.read(pbuf);
            while (read >= 0 && !postLine.endsWith("\r\n")) {
              postLine = String.valueOf(pbuf, 0, read);
              postLineBuffer.append(postLine);
              read = in.read(pbuf);
            }
            postLine = postLineBuffer.toString().trim();
            // Handle application/x-www-form-urlencoded
            if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType)) {
              decodeParms(postLine, parms);
            } else if (postLine.length() != 0) {
              // Special case for raw POST data => create a
              // special files entry "postData" with raw content
              // data
              files.put("postData", postLine);
            }
          }
        } else if (Method.PUT.equals(method)) {
          files.put("content", saveTmpFile(fbuf, 0, fbuf.limit()));
        }
      } finally {
        safeClose(randomAccessFile);
        safeClose(in);
      }
    }