public void test_getChannel() {
    // Test for method FileChannel FileInputStream.getChannel()
    FileChannel channel;
    byte[] buffer = new byte[100];
    byte[] stringBytes;
    final int offset = 5;
    boolean equal = true;

    try {
      FileInputStream fis = new FileInputStream(fileName);
      channel = fis.getChannel();
      assertNotNull(channel);
      assertTrue("Channel is closed.", channel.isOpen());

      // Check that the channel is associated with the input stream.
      channel.position(offset);
      fis.read(buffer, 0, 10);
      stringBytes = fileString.getBytes();
      for (int i = 0; i < 10; i++) {
        equal &= (buffer[i] == stringBytes[i + offset]);
      }
      assertTrue("Channel is not associated with this stream.", equal);

      fis.close();
      assertFalse("Channel has not been closed.", channel.isOpen());
    } catch (FileNotFoundException e) {
      fail("Could not find : " + fileName);
    } catch (IOException e) {
      fail("Exception during test : " + e.getMessage());
    }
  }
Esempio n. 2
0
 /**
  * Clean up any resources. Closes the channel.
  *
  * @throws IOException If errors occur while closing the channel.
  */
 public void close() throws IOException {
   if (channel != null && channel.isOpen()) {
     channel.close();
   }
   channel = null;
   header = null;
 }
Esempio n. 3
0
 @Override
 public boolean isClosed() {
   if (shpChannel != null) {
     return !shpChannel.isOpen();
   }
   return true;
 }
Esempio n. 4
0
 private FileChannel getFileChannel() {
   FileChannel fc = raf.getChannel();
   if (!fc.isOpen()) {
     throw new IllegalStateException(
         "Couldn't read piece from file, as it was closed: " + getMetaInfo().getName());
   }
   return fc;
 }
 public void close() {
   try {
     if (channel != null && channel.isOpen()) {
       channel.close();
       channel = null;
     }
   } catch (IOException ex) {
     Logger.getLogger(SocketChannelHandler.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
Esempio n. 6
0
 /** Close the underlying Channels. */
 @Override
 public void close() throws IOException {
   try {
     if (shpChannel != null && shpChannel.isOpen()) {
       shpChannel.close();
     }
   } finally {
     shx.close();
   }
   shpChannel = null;
   handler = null;
   shapeBuffer = null;
 }
Esempio n. 7
0
  public void run() {
    FileChannel channel = null;
    ByteBuffer buffer = null;
    buffer = ByteBuffer.allocate(262144);
    if (this.logListener != null) this.logListener.inicioLog(null);
    try {
      while (isContinuar()) {
        channel = new FileInputStream(this.arquivo).getChannel();
        if (channel.size() > this.bytesLidos) {
          int lidos = 0;
          while ((lidos = channel.read(buffer, this.bytesLidos)) != -1) {
            buffer.flip();
            byte[] b = new byte[lidos];
            buffer.get(b);
            if (this.logListener != null) this.logListener.log(new String(b));
            buffer.clear();
            this.bytesLidos += lidos;
          }
        } else if (channel.size() < this.bytesLidos) {
          this.bytesLidos = (this.arquivo.length() - 16384L);
          if (this.bytesLidos < 0L) this.bytesLidos = 0L;
          if (this.logListener != null)
            this.logListener.log(
                System.getProperty("line.separator")
                    + "********Parte do arquivo foi apagada. Reinicializando leitura********"
                    + System.getProperty("line.separator"));
        }

        try {
          Thread.sleep(500L);
        } catch (InterruptedException e) {
          e.printStackTrace();
          setContinuar(false);
        }
        channel.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      PrintStream s = new PrintStream(bos);
      e.printStackTrace(s);
      s.flush();
      JOptionPane.showMessageDialog(null, new ErroPainel(new String(bos.toByteArray())), "Erro", 0);
    } finally {
      if ((channel != null) && (!channel.isOpen()))
        try {
          channel.close();
        } catch (IOException localIOException1) {
        }
    }
  }
Esempio n. 8
0
  /**
   * Requests that the underlying log file is deleted.
   *
   * @throws IOException
   */
  public void delete() throws IOException {
    close(); // attempt to close will succeed if there are no registered accessors

    if (m_channel.isOpen())
      throw new IllegalStateException("Request to delete file with open readers and writers");

    if (m_haLogFile.exists()) {
      try {
        m_haLogFile.delete();
      } catch (SecurityException se) {
        log.warn("unable to delete file", se);
      }
    }
  }
Esempio n. 9
0
    public void run() {
      if (address == 0) return;

      try {
        unmap0(address, size);
        address = 0;

        if (channel.isOpen()) {
          channel.force(true);
          channel.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
Esempio n. 10
0
 /*
  * Used for unit tests
  */
 public boolean isOpen() {
   return m_channel != null && m_channel.isOpen();
 }
Esempio n. 11
0
  /**
   * Attempts to close the file; will only succeed if there are no readers and writers.
   *
   * @throws IOException
   */
  public void close() throws IOException {
    if (m_accessors == 0 && m_channel.isOpen()) {

      m_raf.close();
    }
  }
Esempio n. 12
0
 /** Is this file open? */
 public boolean isOpen() {
   synchronized (channel) {
     return channel.isOpen();
   }
 }
Esempio n. 13
0
  @Test
  public void testClose() throws IOException {
    FileChannel channel = channel(regularFile(0), READ, WRITE);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    assertTrue(channel.isOpen());
    channel.close();
    assertFalse(channel.isOpen());

    try {
      channel.position();
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.position(0);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.lock();
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.lock(0, 10, true);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.tryLock();
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.tryLock(0, 10, true);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.force(true);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.write(buffer("111"));
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.write(buffer("111"), 10);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.write(new ByteBuffer[] {buffer("111"), buffer("111")});
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.write(new ByteBuffer[] {buffer("111"), buffer("111")}, 0, 2);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.transferFrom(new ByteBufferChannel(bytes("1111")), 0, 4);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.truncate(0);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.read(buffer("111"));
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.read(buffer("111"), 10);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.read(new ByteBuffer[] {buffer("111"), buffer("111")});
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.read(new ByteBuffer[] {buffer("111"), buffer("111")}, 0, 2);
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      channel.transferTo(0, 10, new ByteBufferChannel(buffer("111")));
      fail();
    } catch (ClosedChannelException expected) {
    }

    executor.shutdown();
  }
 @Override
 public boolean isOpen() {
   return channel.isOpen();
 }